johnagan / clean-webpack-plugin

A webpack plugin to remove your build folder(s) before building
MIT License
1.96k stars 135 forks source link

Does the order of clean-webpack-plugin matter? #200

Open intrepid-ishan opened 3 years ago

intrepid-ishan commented 3 years ago

Does the order of CleanWebpackPlugin matter?

In my project, I am running plugins in the below order, as you can see CleanWebpackPlugin is the last item in the array. Will that impact my build?

plugins: [
  new MomentLocalesPlugin(), 
  new webpack.DefinePlugin(),
  new HtmlWebpackPlugin(),
  new CopyWebpackPlugin(),
  new CleanWebpackPlugin()
]
ankurk91 commented 3 years ago

No, it does not

axibyte commented 2 years ago

In my case the order was important and CleanWebpackPlugin should always be the first plugin to declare.

Here is the issue: the CleanWebpackPlugin uses the emit hook to perform the initial clean stage. If any other plugin uses the same hook "emit" to write files to the output directory, the file will be deleted if the handler of the emit event of this particular plugin was called first.

Example of such plugin is the "Chunks2JsonPlugin".

Placing the Chunks2JsonPlugin before the CleanWebpackPlugin will remove the file generated by the Chunks2JsonPlugin