webpack-dynamic-hash
Advanced tools
Comparing version 1.0.2 to 1.0.4
61
index.js
@@ -5,13 +5,58 @@ /** | ||
*/ | ||
function HelloWorldPlugin(options) { | ||
function WebpackDynamicHash(options) { | ||
// Setup the plugin instance with options... | ||
console.log('hello') | ||
} | ||
//HelloWorldPlugin.prototype.apply = function(compiler) { | ||
// compiler.plugin('done', function() { | ||
// console.log('Hello World!'); | ||
// }); | ||
//}; | ||
WebpackDynamicHash.prototype.apply = function(compiler) { | ||
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 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 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); | ||
}; | ||
appHash('std-app'); | ||
appHash('licai-app'); | ||
changeFile('build/js/std-init.js', '?apphash', '?csshash'); | ||
changeFile('build/js/licai-init.js', '?apphash', '?csshash'); | ||
}); | ||
}; | ||
module.exports = HelloWorldPlugin; | ||
module.exports = WebpackDynamicHash; |
{ | ||
"name": "webpack-dynamic-hash", | ||
"version": "1.0.2", | ||
"version": "1.0.4", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
3035
4
58
2