webpack-contrib / compression-webpack-plugin

Prepare compressed versions of assets to serve them with Content-Encoding
MIT License
1.4k stars 108 forks source link

Allow usage of 6.x version without md4 #329

Closed olof-nord closed 1 year ago

olof-nord commented 1 year ago

Feature Proposal

Feature Use Case

With Node.js v17+ removing the md4 hashing algorithm, the usage of the CompressionPlugin unfortunately breaks the build.

If Webpack 4 is detected, the Node.js createHash function is unfortunately called assuming that "md4" is available.

if (CompressionPlugin.isWebpack4()) {
    cacheData.cacheKeys = {
      nodeVersion: process.version,
      // eslint-disable-next-line global-require
      "compression-webpack-plugin": require("../package.json").version,
      algorithm: this.algorithm,
      originalAlgorithm: this.options.algorithm,
      compressionOptions: this.options.compressionOptions,
      name,
      contentHash: crypto.createHash("md4").update(input).digest("hex"),
    };
} 

https://github.com/webpack-contrib/compression-webpack-plugin/blob/v6.1.1/src/index.js#L232

If a project is not able to upgrade from Webpack 4, and thus cannot move to compression-webpack-plugin v7+, then it is stuck with Node.js 16 or forced to configure the --openssl-legacy-provider Node.js flag.

I suggest to release a 6.2.0 version of the library, and allow the hashing algorithm to optionally be provided.

new CompressionPlugin({
        test: /\.js(\?.*)?$/i,
        hashType: 'sha256',
}),

Please paste the results of npx webpack-cli info here, and mention other relevant information

➜  webpack4-node18-demo git:(main) ✗ npx webpack-cli info        
The command moved into a separate package: @webpack-cli/info
Would you like to install info? (That will run npm install -D @webpack-cli/info) (yes/NO) : yes
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: webpack4-node18-demo@1.0.0
npm ERR! Found: webpack@4.46.0
npm ERR! node_modules/webpack
npm ERR!   webpack@"^4.46.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer webpack@"5.x.x" from @webpack-cli/info@2.0.0
npm ERR! node_modules/@webpack-cli/info
npm ERR!   dev @webpack-cli/info@"*" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! See /home/olof/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/olof/.npm/_logs/2022-11-17T20_41_50_732Z-debug-0.log
undefined
➜  webpack4-node18-demo git:(main) ✗ 

Node.js 18.12.1 webpack 4.46.0 compression-webpack-plugin 6.1.1

This issue is related to this PR which is trying to update mastodon to use Node.js v18. https://github.com/mastodon/mastodon/pull/20586

alexander-akait commented 1 year ago

Why don't update to webpack5? Is is deprecated

olof-nord commented 1 year ago

Very valid point, I should have clarified.

The project is configuring webpack with a ruby gem, webpacker, which unfortunately only supports webpack 4.

Webpacker has been retired - we were investigating possibilities of a solution which does not involve replacing it.

alexander-akait commented 1 year ago

If it is deprecated you can get all webpack configuration and runner from this package and move to own, so you will have access to update deps and start to migrate

olof-nord commented 1 year ago

appreciating your input!