Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added v5 compilation support and deleted depreciation warnings #1454

Merged
merged 3 commits into from
Aug 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 === 5
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would invert the logic to support future versions too (and as it is done in other parts of the codebase)

= webpackMajorVersion === 4 ? ... : ...

Copy link
Contributor Author

@bartdominiak bartdominiak Jun 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first thought too, but we'll lose backward compatibility with v3 and lower, and I'm not 100% that we should support them also? And also this Compilation syntax is different only in webpack v5.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first thought too, but we'll lose backward compatibility with v3 and lower, and I'm not 100% that we should support them also? And also this Compilation syntax is different only in webpack v5.

Compatibility is stated by:

  "peerDependencies": {
    "webpack": ">=4.0.0 < 6.0.0"
  },

"webpack": ">=4.0.0 < 6.0.0"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've one more idea to pick major v5 and higher like:

webpackMajorVersion >= 5 ? ... : ...

What do you think?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was more looking to how it was written in file-watcher-api.js.

module.exports = webpackMajorVersion === 4

? compilation.getAssetPath(this.options.filename, compiledEntries)
: compilation.mainTemplate.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 realtive path
*/
const webpackPublicPath = compilation.mainTemplate.getPublicPath({ hash: compilationHash });
const webpackPublicPath = webpackMajorVersion === 5
? compilation.getAssetPath(compilation.outputOptions.publicPath, { hash: compilationHash })
: compilation.mainTemplate.getPublicPath({ 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 @@ -159,12 +159,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 === 5
? mainCompilation.getAssetPath(filename, entryConfig)
: mainCompilation.mainTemplate.getAssetPath(filename, entryConfig);
});

helperAssetNames.forEach((helperFileName) => {
Expand Down