Skip to content

Commit 4ae7be8

Browse files
authoredAug 30, 2020
feat: added v5 compilation support and deleted depreciation warnings
#1454
1 parent 6b0711e commit 4ae7be8

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed
 

Diff for: ‎index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const getHtmlWebpackPluginHooks = require('./lib/hooks.js').getHtmlWebpackPlugin
2727
const fsStatAsync = promisify(fs.stat);
2828
const fsReadFileAsync = promisify(fs.readFile);
2929

30+
const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]);
31+
3032
class HtmlWebpackPlugin {
3133
/**
3234
* @param {HtmlWebpackOptions} [options]
@@ -149,12 +151,16 @@ class HtmlWebpackPlugin {
149151
compilation.errors.push(prettyError(templateResult.error, compiler.context).toString());
150152
}
151153

152-
const childCompilationOutputName = compilation.mainTemplate.getAssetPath(this.options.filename, 'compiledEntry' in templateResult ? {
154+
const compiledEntries = 'compiledEntry' in templateResult ? {
153155
hash: templateResult.compiledEntry.hash,
154156
chunk: templateResult.compiledEntry.entry
155157
} : {
156158
hash: templateResult.mainCompilationHash
157-
});
159+
};
160+
161+
const childCompilationOutputName = webpackMajorVersion === 4
162+
? compilation.mainTemplate.getAssetPath(this.options.filename, compiledEntries)
163+
: compilation.getAssetPath(this.options.filename, compiledEntries);
158164

159165
// If the child compilation was not executed during a previous main compile run
160166
// it is a cached result
@@ -529,7 +535,10 @@ class HtmlWebpackPlugin {
529535
* if a path publicPath is set in the current webpack config use it otherwise
530536
* fallback to a relative path
531537
*/
532-
const webpackPublicPath = compilation.mainTemplate.getPublicPath({ hash: compilationHash });
538+
const webpackPublicPath = webpackMajorVersion === 4
539+
? compilation.mainTemplate.getPublicPath({ hash: compilationHash })
540+
: compilation.getAssetPath(compilation.outputOptions.publicPath, { hash: compilationHash });
541+
533542
const isPublicPathDefined = webpackPublicPath.trim() !== '';
534543
let publicPath = isPublicPathDefined
535544
// If a hard coded public path exists use it

Diff for: ‎lib/child-compiler.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,18 @@ class HtmlWebpackChildCompiler {
168168
* @returns Array<string>
169169
*/
170170
function extractHelperFilesFromCompilation (mainCompilation, childCompilation, filename, childEntryChunks) {
171+
const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]);
172+
171173
const helperAssetNames = childEntryChunks.map((entryChunk, index) => {
172-
return mainCompilation.mainTemplate.getAssetPath(filename, {
174+
const entryConfig = {
173175
hash: childCompilation.hash,
174176
chunk: entryChunk,
175177
name: `HtmlWebpackPlugin_${index}`
176-
});
178+
};
179+
180+
return webpackMajorVersion === 4
181+
? mainCompilation.mainTemplate.getAssetPath(filename, entryConfig)
182+
: mainCompilation.getAssetPath(filename, entryConfig);
177183
});
178184

179185
helperAssetNames.forEach((helperFileName) => {

0 commit comments

Comments
 (0)
Please sign in to comment.