Main problem why this issue was created (and first note about invalid usage):
A lot of plugins use emit hook for emitting assets, but it is invalid.
Why:
emit hook is last so you need setup right order (example imagemin-webpack-plugin and copy-webpack-plugin use emit hook so if you put imagemin-webpack-plugin before copy-webpack-plugin assets from copy-webpack-plugin can't be optimized). There are a lot of other example (i can provide if we need this).
TerserWebpackPlugin (and other minimizers) uses optimizeChunkAssets hook, so all assets written in emit event is not compressed too.
In the plans for webpack@5 use freeze on assets after compilation to decrease memory usage. Also we maybe introduce new hooks for plugin like manifest-webpack-plugin.
The additionalAssets hook was specially made for adding assets.
Feature to document
Section - https://webpack.js.org/concepts/plugins
Reasons:
Main problem why this issue was created (and first note about invalid usage):
A lot of plugins use
emit
hook for emitting assets, but it is invalid. Why:emit
hook is last so you need setup right order (exampleimagemin-webpack-plugin
andcopy-webpack-plugin
useemit
hook so if you putimagemin-webpack-plugin
beforecopy-webpack-plugin
assets fromcopy-webpack-plugin
can't be optimized). There are a lot of other example (i can provide if we need this).TerserWebpackPlugin
(and other minimizers) usesoptimizeChunkAssets
hook, so all assets written inemit
event is not compressed too.webpack@5
usefreeze
on assets after compilation to decreasememory
usage. Also we maybe introduce new hooks for plugin likemanifest-webpack-plugin
.additionalAssets
hook was specially made for addingassets
.Right solution is using
additionalAssets
hook (https://github.com/webpack/webpack/blob/master/lib/Compilation.js#L352). Some plugins inwebpack-contrib
andwebpack
repo will be rewritten onadditionalAssets
.Feel free to feedback.
Author(s)
no matter
Additional information
No