johnagan / clean-webpack-plugin

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

Unintentional deleting of modules which "html-webpack-externals-plugin" handled #120

Closed kogai closed 5 years ago

kogai commented 5 years ago

Issue description or question

If webpack is running by watch mode, clean-webpack-plugin seems to remove modules which html-webpack-externals-plugin provided (of course it doesn't applied any changes) as stale one.

I'd created a minimum reproduction environment here.

And I guess it causes from Stats which webpack provides to plugin doesn't includes files html-webpack-externals-plugin provides.

https://github.com/johnagan/clean-webpack-plugin/blob/master/src/clean-webpack-plugin.ts#L186-L194

https://github.com/johnagan/clean-webpack-plugin/blob/master/src/clean-webpack-plugin.ts#L232-L245

In my situation, setting cleanStaleWebpackAssets as true to clean-webpack-plugin could resolves such problem actually.

However, it seems unintentional behavior of the plugin.

Webpack Config

const webpack = require("webpack");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const HtmlWebpackExternalsPlugin = require("html-webpack-externals-plugin");

module.exports = {
  entry: "./app.js",
  mode: "development",
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: "template.html"
    }),
    new HtmlWebpackExternalsPlugin({
      externals: [
        {
          module: "react",
          global: "React",
          entry: `umd/react.development.js`
        }
      ]
    })
  ]
};

Environment

Run: npx envinfo --system --binaries --npmPackages clean-webpack-plugin,webpack

  System:
    OS: macOS 10.14
    CPU: (4) x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
    Memory: 141.90 MB / 16.00 GB
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 10.15.3 - ~/.nodenv/shims/node
    Yarn: 1.9.4 - ~/.nodenv/shims/yarn
    npm: 6.4.1 - ~/.nodenv/shims/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  npmPackages:
    clean-webpack-plugin: ^2.0.1 => 2.0.1
    webpack: ^4.29.6 => 4.29.6
chrisblossom commented 5 years ago

Thanks for the example! Just as you said, it is because html-webpack-externals-plugin is removing the files from webpack's asset list.

You can either set cleanStaleWebpackAssets: false or:

new CleanWebpackPlugin({
    cleanAfterEveryBuildPatterns: ['!vendor/**/*'],
}),
kogai commented 5 years ago

OK, I got it. Thanks 👍