webpack-dynamic-hash
Advanced tools
Comparing version 1.0.6 to 1.0.7
135
index.js
@@ -7,62 +7,101 @@ /** | ||
function WebpackDynamicHash(options, arr) { | ||
function WebpackDynamicHash(options, init, app, lib) { | ||
this.__dirname = options; | ||
this.arr = arr; | ||
this.init = init; | ||
this.app = app; | ||
this.lib = lib; | ||
} | ||
WebpackDynamicHash.prototype.apply = function(compiler) { | ||
var __dirname = this.__dirname; | ||
var arr = this.arr; | ||
compiler.plugin('done', function(stats) { | ||
var fs = require('fs'); | ||
var json = stats.toJson(); //webpack build的输出文件 | ||
var commonshash = ''; //commons.js的 hash | ||
var apphash = ''; //stdapp.js的 hash | ||
var csshash = ''; //css.js的 hash | ||
// node 命令中包涵 -dev | ||
var isDev = (function () { | ||
var argv = process.argv; | ||
for (var i = 0, l = argv.length; i < l; i += 1) { | ||
if (argv[i] === '-dev') { | ||
return true; | ||
var fs = require('fs'); | ||
try { | ||
var __dirname = this.__dirname; | ||
var init = this.init; | ||
var app = this.app; | ||
var lib = this.lib; | ||
//如果存在build目录则继续 | ||
var foo = function (stats) { | ||
var json = stats.toJson(); //webpack build的输出文件 | ||
var libHash = ''; //commons.js的 hash | ||
var appHash = ''; //stdapp.js的 hash | ||
var cssHash = ''; //css.js的 hash | ||
var initHash = ''; //init.js的 hash | ||
// node 命令中包涵 -dev | ||
var isDev = (function () { | ||
var argv = process.argv; | ||
for (var i = 0, l = argv.length; i < l; i += 1) { | ||
if (argv[i] === '-dev') { | ||
return true; | ||
} | ||
} | ||
})(); | ||
var libStats = json.assetsByChunkName['lib']; | ||
var initStats = json.assetsByChunkName['init']; | ||
if (libStats) { | ||
// -dev commons输出的是数组 | ||
if (isDev) { | ||
libHash = '-' + libStats[0].split('.js')[0].split('-')[1]; | ||
initHash = '-' + initStats[0].split('.js')[0].split('-')[1]; | ||
} else { | ||
libHash = '-' + libStats.split('.js')[0].split('-')[1]; | ||
initHash = '-' + initStats.split('.js')[0].split('-')[1]; | ||
} | ||
} | ||
})(); | ||
var commonsStats = json.assetsByChunkName['commons']; | ||
if (commonsStats) { | ||
// -dev commons输出的是数组 | ||
if (isDev) { | ||
commonshash = '?' + commonsStats[0].split('?')[1]; | ||
} else { | ||
commonshash = '?' + commonsStats.split('?')[1]; | ||
} | ||
} | ||
var appHash = function (appname) { | ||
var appStats = json.assetsByChunkName[appname]; | ||
var appStatsLength = appStats.length; | ||
if (appStatsLength) { | ||
for (var i = 0; i < appStatsLength; i += 1) { | ||
if (appStats[i].indexOf(appname + '.js') > -1 && appStats[i].indexOf(appname + '.js.map') === -1) { | ||
apphash = '?' + appStats[i].split('?')[1]; | ||
} else if (appStats[i].indexOf(appname + '.css') > -1 && appStats[i].indexOf(appname + '.css.map') === -1) { | ||
csshash = '?' + appStats[i].split('?')[1]; | ||
var getAppHash = function (appname) { | ||
var appStats = json.assetsByChunkName[appname]; | ||
var appStatsLength = appStats.length; | ||
if (appStatsLength) { | ||
for (var i = 0; i < appStatsLength; i += 1) { | ||
if (appStats[i].indexOf('.js') > -1 && appStats[i].indexOf('.js.map') === -1) { | ||
appHash = '-' + appStats[i].split('.js')[0].split('-')[1]; | ||
} else if (appStats[i].indexOf('.css') > -1 && appStats[i].indexOf('.css.map') === -1) { | ||
cssHash = '-' + appStats[i].split('.css')[0].split('-')[1]; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
var changeFile = function (init, app, css) { | ||
var text = fs.readFileSync(path.join(__dirname, init), 'utf8'); | ||
var out = text.replace('?commonshash', commonshash); | ||
out = out.replace(app, apphash); | ||
out = out.replace(css, csshash); | ||
fs.writeFileSync(path.join(__dirname, init), out); | ||
}; | ||
}; | ||
var changeFile = function (init) { | ||
//read init.js | ||
var text = fs.readFileSync(path.join(__dirname, init), 'utf8'); | ||
// .js'$ | ||
var out = text.replace(/(lib-)(.*)(\.js)/g, "lib" + libHash + ".js"); | ||
out = out.replace(/(app-)(.*)(\.js)/g, "app" + appHash + ".js"); | ||
out = out.replace(/(app-)(.*)(\.c)/g, "app" + cssHash + ".c"); | ||
fs.writeFileSync(path.join(__dirname, init), out); | ||
}; | ||
arr.map(function(v, i) { | ||
appHash(v + '-app'); | ||
changeFile('build/js/' + v + '-init.js', '?apphash', '?csshash'); | ||
getAppHash(app); | ||
fs.exists(path.join(__dirname, 'build/js/' + init + '.js'), function(result) { | ||
if (!result) { | ||
fs.rename('build/js/' + init + initHash + '.js', 'build/js/' + init + '.js', function (err) { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
changeFile('build/js/' + init + '.js'); | ||
} | ||
}) | ||
} else { | ||
changeFile('build/js/' + init + '.js'); | ||
} | ||
}) | ||
} | ||
fs.exists(path.join(__dirname, 'build'), function(result) { | ||
compiler.plugin('done', function (stats) { | ||
if (result) { | ||
foo(stats) | ||
} else { | ||
setTimeout(function () { | ||
foo(stats) | ||
}, 0) | ||
} | ||
}); | ||
}) | ||
}); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}; | ||
module.exports = WebpackDynamicHash; |
{ | ||
"name": "webpack-dynamic-hash", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
4689
98
1