From 4ae7be8243461ce4537d98047abf0fa517dd0308 Mon Sep 17 00:00:00 2001 From: Bartosz Dominiak Date: Sun, 30 Aug 2020 14:28:37 +0200 Subject: [PATCH] feat: added v5 compilation support and deleted depreciation warnings #1454 --- index.js | 15 ++++++++++++--- lib/child-compiler.js | 10 ++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 60b0ca7e..b735787b 100644 --- a/index.js +++ b/index.js @@ -27,6 +27,8 @@ const getHtmlWebpackPluginHooks = require('./lib/hooks.js').getHtmlWebpackPlugin const fsStatAsync = promisify(fs.stat); const fsReadFileAsync = promisify(fs.readFile); +const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]); + class HtmlWebpackPlugin { /** * @param {HtmlWebpackOptions} [options] @@ -149,12 +151,16 @@ class HtmlWebpackPlugin { compilation.errors.push(prettyError(templateResult.error, compiler.context).toString()); } - const childCompilationOutputName = compilation.mainTemplate.getAssetPath(this.options.filename, 'compiledEntry' in templateResult ? { + const compiledEntries = 'compiledEntry' in templateResult ? { hash: templateResult.compiledEntry.hash, chunk: templateResult.compiledEntry.entry } : { hash: templateResult.mainCompilationHash - }); + }; + + const childCompilationOutputName = webpackMajorVersion === 4 + ? compilation.mainTemplate.getAssetPath(this.options.filename, compiledEntries) + : compilation.getAssetPath(this.options.filename, compiledEntries); // If the child compilation was not executed during a previous main compile run // it is a cached result @@ -529,7 +535,10 @@ class HtmlWebpackPlugin { * if a path publicPath is set in the current webpack config use it otherwise * fallback to a relative path */ - const webpackPublicPath = compilation.mainTemplate.getPublicPath({ hash: compilationHash }); + const webpackPublicPath = webpackMajorVersion === 4 + ? compilation.mainTemplate.getPublicPath({ hash: compilationHash }) + : compilation.getAssetPath(compilation.outputOptions.publicPath, { hash: compilationHash }); + const isPublicPathDefined = webpackPublicPath.trim() !== ''; let publicPath = isPublicPathDefined // If a hard coded public path exists use it diff --git a/lib/child-compiler.js b/lib/child-compiler.js index 88180a0a..63057062 100644 --- a/lib/child-compiler.js +++ b/lib/child-compiler.js @@ -168,12 +168,18 @@ class HtmlWebpackChildCompiler { * @returns Array */ function extractHelperFilesFromCompilation (mainCompilation, childCompilation, filename, childEntryChunks) { + const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]); + const helperAssetNames = childEntryChunks.map((entryChunk, index) => { - return mainCompilation.mainTemplate.getAssetPath(filename, { + const entryConfig = { hash: childCompilation.hash, chunk: entryChunk, name: `HtmlWebpackPlugin_${index}` - }); + }; + + return webpackMajorVersion === 4 + ? mainCompilation.mainTemplate.getAssetPath(filename, entryConfig) + : mainCompilation.getAssetPath(filename, entryConfig); }); helperAssetNames.forEach((helperFileName) => {