Skip to content

Commit e684134

Browse files
committed
refactor: Change the structure of the internal assets object
Adapt structure in preparation for the webpack 4 entry api BREAKING CHANGE: The assets object which is used for the template parameters and inside hooks was changed. The chunks property was removed and the js and css property was converted from a string into an object `{ entryName: string, path: string}`
1 parent 2206129 commit e684134

File tree

6 files changed

+43
-23
lines changed

6 files changed

+43
-23
lines changed

examples/inline/template.jade

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ html
55
title #{htmlWebpackPlugin.options.title}
66
body
77
each cssFile in htmlWebpackPlugin.files.css
8-
style !{compilation.assets[cssFile.substr(htmlWebpackPlugin.files.publicPath.length)].source()}
8+
style !{compilation.assets[cssFile.path.substr(htmlWebpackPlugin.files.publicPath.length)].source()}
99
each jsFile in htmlWebpackPlugin.files.js
10-
script !{compilation.assets[jsFile.substr(htmlWebpackPlugin.files.publicPath.length)].source()}
10+
script !{compilation.assets[jsFile.path.substr(htmlWebpackPlugin.files.publicPath.length)].source()}

index.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,18 @@ class HtmlWebpackPlugin {
434434
return assets.js.length && assets.js.every(name => /\.hot-update\.js$/.test(name));
435435
}
436436

437+
/**
438+
*
439+
* @param {WebpackCompilation} compilation
440+
* @param {any} chunks
441+
* @returns {{
442+
publicPath: string,
443+
js: Array<{entryName: string, path: string}>,
444+
css: Array<{entryName: string, path: string}>,
445+
manifest: string,
446+
favicon?: string
447+
}}
448+
*/
437449
htmlWebpackPluginAssets (compilation, chunks) {
438450
const compilationHash = compilation.hash;
439451

@@ -449,11 +461,18 @@ class HtmlWebpackPlugin {
449461
publicPath += '/';
450462
}
451463

464+
/**
465+
* @type {{
466+
publicPath: string,
467+
js: Array<{entryName: string, path: string}>,
468+
css: Array<{entryName: string, path: string}>,
469+
manifest: string,
470+
favicon?: string
471+
}}
472+
*/
452473
const assets = {
453474
// The public path
454475
publicPath: publicPath,
455-
// Will contain all js & css files by chunk
456-
chunks: {},
457476
// Will contain all js files
458477
js: [],
459478
// Will contain all css files
@@ -465,15 +484,12 @@ class HtmlWebpackPlugin {
465484
// Append a hash for cache busting
466485
if (this.options.hash) {
467486
assets.manifest = this.appendHash(assets.manifest, compilationHash);
468-
assets.favicon = this.appendHash(assets.favicon, compilationHash);
469487
}
470488

471489
for (let i = 0; i < chunks.length; i++) {
472490
const chunk = chunks[i];
473491
const chunkName = chunk.names[0];
474492

475-
assets.chunks[chunkName] = {};
476-
477493
// Prepend the public path to all chunk files
478494
let chunkFiles = [].concat(chunk.files).map(chunkFile => publicPath + chunkFile);
479495

@@ -486,16 +502,20 @@ class HtmlWebpackPlugin {
486502
// or when one chunk hosts js and css simultaneously
487503
const js = chunkFiles.find(chunkFile => /.js($|\?)/.test(chunkFile));
488504
if (js) {
489-
assets.chunks[chunkName].size = chunk.size;
490-
assets.chunks[chunkName].entry = js;
491-
assets.chunks[chunkName].hash = chunk.hash;
492-
assets.js.push(js);
505+
assets.js.push({
506+
entryName: chunkName,
507+
path: js
508+
});
493509
}
494510

495511
// Gather all css files
496512
const css = chunkFiles.filter(chunkFile => /.css($|\?)/.test(chunkFile));
497-
assets.chunks[chunkName].css = css;
498-
assets.css = assets.css.concat(css);
513+
css.forEach((cssPath) => {
514+
assets.css.push({
515+
entryName: chunkName,
516+
path: cssPath
517+
});
518+
});
499519
}
500520

501521
// Duplicate css assets can occur on occasion if more than one chunk
@@ -552,19 +572,19 @@ class HtmlWebpackPlugin {
552572
*/
553573
generateHtmlTagObjects (assets) {
554574
// Turn script files into script tags
555-
const scripts = assets.js.map(scriptPath => ({
575+
const scripts = assets.js.map(scriptAsset => ({
556576
tagName: 'script',
557577
voidTag: false,
558578
attributes: {
559-
src: scriptPath
579+
src: scriptAsset.path
560580
}
561581
}));
562582
// Turn css files into link tags
563-
const styles = assets.css.map(stylePath => ({
583+
const styles = assets.css.map(styleAsset => ({
564584
tagName: 'link',
565585
voidTag: true,
566586
attributes: {
567-
href: stylePath,
587+
href: styleAsset.path,
568588
rel: 'stylesheet'
569589
}
570590
}));

spec/BasicSpec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ describe('HtmlWebpackPlugin', function () {
376376
}, [{
377377
type: 'chunkhash',
378378
chunkName: 'app',
379-
containStr: '<script src="app_bundle.js?%chunkhash%"'
379+
containStr: '<script src="app_bundle.js'
380380
}], null, done);
381381
});
382382

@@ -1195,7 +1195,7 @@ describe('HtmlWebpackPlugin', function () {
11951195
compiler.plugin('compilation', function (compilation) {
11961196
tapCompilationEvent(compilation, 'html-webpack-plugin-before-html-processing', function (object, callback) {
11971197
eventFired = true;
1198-
object.assets.js.push('funky-script.js');
1198+
object.assets.js.push({path: 'funky-script.js'});
11991199
object.html += 'Injected by plugin';
12001200
callback();
12011201
});
@@ -1230,7 +1230,7 @@ describe('HtmlWebpackPlugin', function () {
12301230
compiler.plugin('compilation', function (compilation) {
12311231
tapCompilationEvent(compilation, 'html-webpack-plugin-before-html-generation', function (object, callback) {
12321232
eventFired = true;
1233-
object.assets.js.push('funky-script.js');
1233+
object.assets.js.push({path: 'funky-script.js'});
12341234
callback();
12351235
});
12361236
});

spec/fixtures/template.jade

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ html
66
body
77
p Some unique text
88
each jsFile in htmlWebpackPlugin.files.js
9-
script(src!=jsFile)
9+
script(src!=jsFile.path)

spec/fixtures/test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
</head>
77
<body>
88
<p>Some unique text</p>
9-
<script src="<%=htmlWebpackPlugin.files.chunks.app.entry%>"></script>
9+
<script src="<%=htmlWebpackPlugin.files.js[0].path%>"></script>
1010
</body>
1111
</html>

spec/fixtures/webpackconfig.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
</head>
77
<body>
88
<p>Public path is <%= webpackConfig.output.publicPath %></p>
9-
<script src="<%= htmlWebpackPlugin.files.chunks.app.entry %>?<%= htmlWebpackPlugin.files.chunks.app.hash %>"></script>
9+
<script src="<%= htmlWebpackPlugin.files.js[0].path %></script>
1010
</body>
1111
</html>

0 commit comments

Comments
 (0)