jantimon / html-webpack-plugin

Simplifies creation of HTML files to serve your webpack bundles
MIT License
10.71k stars 1.31k forks source link

Requires plugins from unrelated version of webpack when two versions are present in a project #1596

Closed boutell closed 3 years ago

boutell commented 3 years ago

This issue was previously reported in #1451 and closed as an upstream issue. However, I reported it as an upstream issue and was told that starting with webpack 5 there's a better way for plugins to access the version of webpack that instantiated them. So please review below, including the response from the webpack developers.

What is the current behavior?

If a project and one of its npm dependencies, let's call it nifty-cms, each depend on two different versions of webpack, i.e. 5 (project level) and 4 (nifty-cms), then npm and yarn will both do the reasonable thing and install webpack twice, with node_modules/nifty-cms/node_nodules containing webpack 4 and node_modules/webpack containing webpack 5. So far so good.

However, if nifty-cms also depends on html-webpack-plugin and the project does not, then html-webpack-plugin will be installed as node_modules/html-webpack-plugin. Again, so far so good. Hoisting is normal.

But here's where we get in trouble: unfortunately, when webpack 4 in nifty-cms loads html-webpack-plugin, html-webpack-plugin then tries to require various files back from webpack itself, for instance:

./index.js:const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]);
./lib/child-compiler.js:const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');

At this point, npm (or yarn) sees that html-webpack-plugin was loaded from node_modules/html-webpack-plugin and loads node_modules/webpack.

But, this is not the version of webpack that instantiated the plugin. So we will get the wrong information about the version of webpack that instantiated us, and potentially incorrect and incompatible plugins as well (see the second require line).

https://github.com/webpack/webpack/issues/12606#issuecomment-774283510

So in a nutshell:

boutell commented 3 years ago

... Except you're doing a new major version that only supports webpack 5, which means this issue should no longer be in play. Never mind. :homer-simpson-shrubbery: