Skip to content

Commit

Permalink
feat: added v5 compilation support and deleted depreciation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
bartdominiak committed Aug 30, 2020
1 parent 6b0711e commit 4ae7be8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/child-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,18 @@ class HtmlWebpackChildCompiler {
* @returns Array<string>
*/
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) => {
Expand Down

0 comments on commit 4ae7be8

Please sign in to comment.