iampava / imagemin-webp-webpack-plugin

Webpack plugin which converts images to the WebP format while also keeping the original files.
MIT License
113 stars 27 forks source link

Replace PROCESS_ASSETS_STAGE_SUMMARIZE with PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE #62

Open phil-w opened 2 years ago

phil-w commented 2 years ago

As per issue #38, current use of this with the latest tools ( Webpack 5.65.0 and imagemin-web-webpack-plugin 3.3.6) throws a deprecation warning from Webpack: _BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation. Do changes to assets earlier, e. g. in Compilation.hooks.processAssets. Make sure to select an appropriate stage from Compilation.PROCESS_ASSETSSTAGE*._

Looking at plugin.js here, in "hookPlugin" there is the code as below (lines 111 etc). So imagemin-web-webpack-plugin is using PROCESS_ASSETS_STAGE_SUMMARIZE... I guess they want something "earlier", and in any case like the earlier "PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE " sounds like the right place.

I'll see if I can learn enough GitHub to do it myself, but otherwise, can that be done please?

if (compiler.hooks && compiler.hooks.thisCompilation && compiler.hooks.processAssets) {
        // webpack 5.x
        compiler.hooks.thisCompilation.tap('ImageminWebpWebpackPlugin', compilation => {
            compilation.hooks.processAssets.tapAsync({
                name: 'ImageminWebpWebpackPlugin',
                stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
            }, (assets, cb) => onEmit(compilation, cb));
        })
    }
Tofandel commented 2 years ago

+1 for this, this is also how I had done it in an old fork for webpack 5 support

https://github.com/Tofandel/imagemin-webp-webpack-plugin/blob/master/plugin.js

Though you should use PROCESS_ASSETS_STAGE_OPTIMIZE