Skip to content

Commit

Permalink
fix: emit files on every build
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jan 20, 2021
1 parent 2b31686 commit 6b3d087
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ class HtmlWebpackPlugin {
options.meta = Object.assign({}, options.meta, defaultMeta, userOptions.meta);
}

// entryName to fileName conversion
const filenameFunction = typeof options.filename === 'function'
? options.filename
// entryName to fileName conversion function
const userOptionFilename = userOptions.filename || defaultOptions.filename;
const filenameFunction = typeof userOptionFilename === 'function'
? userOptionFilename
// Replace '[name]' with entry name
: (entryName) => options.filename.replace(/\[name\]/g, entryName);
: (entryName) => userOptionFilename.replace(/\[name\]/g, entryName);

/** output filenames for the given entry names */
const outputFileNames = new Set(Object.keys(compiler.options.entry).map(filenameFunction));
Expand Down Expand Up @@ -153,6 +154,12 @@ function hookIntoCompiler (compiler, options, plugin) {
// Instance variables to keep caching information
// for multiple builds
let assetJson;
/**
* store the previous generated asset to emit them even if the content did not change
* to support watch mode for third party plugins like the clean-webpack-plugin or the compression plugin
* @type {Array<{html: string, name: string}>}
*/
let previousEmittedAssets = [];

options.template = getFullTemplatePath(options.template, compiler.context);

Expand Down Expand Up @@ -245,8 +252,12 @@ function hookIntoCompiler (compiler, options, plugin) {
// If the template and the assets did not change we don't have to emit the html
const newAssetJson = JSON.stringify(getAssetFiles(assets));
if (isCompilationCached && options.cache && assetJson === newAssetJson) {
previousEmittedAssets.forEach(({ name, html }) => {
compilation.emitAsset(name, new webpack.sources.RawSource(html, false));
});
return callback();
} else {
previousEmittedAssets = [];
assetJson = newAssetJson;
}

Expand Down Expand Up @@ -349,6 +360,7 @@ function hookIntoCompiler (compiler, options, plugin) {
});
// Add the evaluated html code to the webpack assets
compilation.emitAsset(finalOutputName, new webpack.sources.RawSource(html, false));
previousEmittedAssets.push({ name: finalOutputName, html });
return finalOutputName;
})
.then((finalOutputName) => getHtmlWebpackPluginHooks(compilation).afterEmit.promise({
Expand Down

0 comments on commit 6b3d087

Please sign in to comment.