Merge pull request #235 from mc-zone/master
Add chunk hash to `assets.chunks`
This commit is contained in:
commit
4a69c9a013
1
index.js
1
index.js
|
|
@ -378,6 +378,7 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function (compilation, chu
|
|||
var entry = chunkFiles[0];
|
||||
assets.chunks[chunkName].size = chunk.size;
|
||||
assets.chunks[chunkName].entry = entry;
|
||||
assets.chunks[chunkName].hash = chunk.hash;
|
||||
assets.js.push(entry);
|
||||
|
||||
// Gather all css files
|
||||
|
|
|
|||
|
|
@ -38,11 +38,19 @@ function testHtmlPlugin (webpackConfig, expectedResults, outputFile, done, expec
|
|||
return done();
|
||||
}
|
||||
var htmlContent = fs.readFileSync(path.join(OUTPUT_DIR, outputFile)).toString();
|
||||
|
||||
var chunksInfo;
|
||||
for (var i = 0; i < expectedResults.length; i++) {
|
||||
var expectedResult = expectedResults[i];
|
||||
if (expectedResult instanceof RegExp) {
|
||||
expect(htmlContent).toMatch(expectedResult);
|
||||
} else if (typeof expectedResult === 'object') {
|
||||
if (expectedResult.type === 'chunkhash') {
|
||||
if (!chunksInfo) {
|
||||
chunksInfo = getChunksInfoFromStats(stats);
|
||||
}
|
||||
var chunkhash = chunksInfo[expectedResult.chunkName].hash;
|
||||
expect(htmlContent).toContain(expectedResult.containStr.replace('%chunkhash%', chunkhash));
|
||||
}
|
||||
} else {
|
||||
expect(htmlContent).toContain(expectedResult.replace('%hash%', stats.hash));
|
||||
}
|
||||
|
|
@ -51,6 +59,19 @@ function testHtmlPlugin (webpackConfig, expectedResults, outputFile, done, expec
|
|||
});
|
||||
}
|
||||
|
||||
function getChunksInfoFromStats (stats) {
|
||||
var chunks = stats.compilation.getStats().toJson().chunks;
|
||||
var chunksInfo = {};
|
||||
for (var i = 0; i < chunks.length; i++) {
|
||||
var chunk = chunks[i];
|
||||
var chunkName = chunk.names[0];
|
||||
if (chunkName) {
|
||||
chunksInfo[chunkName] = chunk;
|
||||
}
|
||||
}
|
||||
return chunksInfo;
|
||||
}
|
||||
|
||||
describe('HtmlWebpackPlugin', function () {
|
||||
beforeEach(function (done) {
|
||||
rm_rf(OUTPUT_DIR, done);
|
||||
|
|
@ -258,6 +279,26 @@ describe('HtmlWebpackPlugin', function () {
|
|||
}, ['<script src="app_bundle.js"'], null, done);
|
||||
});
|
||||
|
||||
it('allows you to use chunkhash with asset into a given html file', function (done) {
|
||||
testHtmlPlugin({
|
||||
entry: {
|
||||
app: path.join(__dirname, 'fixtures/index.js')
|
||||
},
|
||||
output: {
|
||||
path: OUTPUT_DIR,
|
||||
filename: '[name]_bundle.js'
|
||||
},
|
||||
plugins: [new HtmlWebpackPlugin({
|
||||
inject: false,
|
||||
template: path.join(__dirname, 'fixtures/webpackconfig.html')
|
||||
})]
|
||||
}, [{
|
||||
type: 'chunkhash',
|
||||
chunkName: 'app',
|
||||
containStr: '<script src="app_bundle.js?%chunkhash%"'
|
||||
}], null, done);
|
||||
});
|
||||
|
||||
it('allows you to disable injection', function (done) {
|
||||
testHtmlPlugin({
|
||||
entry: {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,6 @@
|
|||
</head>
|
||||
<body>
|
||||
<p>Public path is <%= webpackConfig.output.publicPath %></p>
|
||||
<script src="<%= htmlWebpackPlugin.files.chunks.app.entry %>"></script>
|
||||
<script src="<%= htmlWebpackPlugin.files.chunks.app.entry %>?<%= htmlWebpackPlugin.files.chunks.app.hash %>"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
Loading…
Reference in New Issue