Support sourcemaps, which generate multiple output bundles for each entry

This commit is contained in:
Charles Blaxland 2014-08-12 22:28:49 +10:00
parent c26597403c
commit 23a0d9c338
2 changed files with 30 additions and 10 deletions

View File

@ -27,7 +27,14 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginJson = function(webpackStatsJson) {
var json = {};
json.assets = {};
for (var chunk in webpackStatsJson.assetsByChunkName) {
json.assets[chunk] = webpackStatsJson.assetsByChunkName[chunk];
var chunkValue = webpackStatsJson.assetsByChunkName[chunk];
// Webpack outputs an array for each chunk when using sourcemaps
if (chunkValue instanceof Array) {
// Is the main bundle always the first element?
chunkValue = chunkValue[0];
}
json.assets[chunk] = chunkValue;
}
return json;

View File

@ -6,10 +6,8 @@ var HtmlWebpackPlugin = require('../index.js');
var OUTPUT_DIR = path.join(__dirname, '..', 'dist');
function testHtmlPlugin(webpackConfig, htmlPluginOptions, expectedResults, done) {
function testHtmlPlugin(webpackConfig, expectedResults, done) {
var outputHtmlFile = path.join(OUTPUT_DIR, 'index.html');
var htmlPlugin = htmlPluginOptions ? new HtmlWebpackPlugin(htmlPluginOptions) : new HtmlWebpackPlugin();
webpackConfig.plugins = [htmlPlugin];
webpack(webpackConfig, function(err, stats) {
expect(err).toBeFalsy();
expect(stats.hasErrors()).toBe(false);
@ -32,8 +30,9 @@ describe('HtmlWebpackPlugin', function() {
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
}
}, null, ['<script src="index_bundle.js"'], done);
},
plugins: [new HtmlWebpackPlugin()]
}, ['<script src="index_bundle.js"'], done);
});
@ -46,8 +45,9 @@ describe('HtmlWebpackPlugin', function() {
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
}
}, null, ['<script src="util_bundle.js"', '<script src="app_bundle.js"'], done);
},
plugins: [new HtmlWebpackPlugin()]
}, ['<script src="util_bundle.js"', '<script src="app_bundle.js"'], done);
});
it('allows you to specify your own HTML template', function(done) {
@ -58,9 +58,22 @@ describe('HtmlWebpackPlugin', function() {
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
}
},
plugins: [new HtmlWebpackPlugin({template: path.join(__dirname, 'fixtures', 'test.html')})]
},
{template: path.join(__dirname, 'fixtures', 'test.html')},
['<script src="app_bundle.js"', 'Some unique text'], done);
});
it('works with source maps', function(done) {
testHtmlPlugin({
devtool: 'sourcemap',
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [new HtmlWebpackPlugin()]
}, ['<script src="index_bundle.js"'], done);
});
});