This commit is contained in:
mc-zone 2016-02-26 20:57:49 +08:00
parent 0b4d7383ea
commit 313a83115a
2 changed files with 43 additions and 2 deletions

View File

@ -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: {

View File

@ -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>