johnagan / clean-webpack-plugin

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

Bug report or enhancement request:reassign "this.outputPath" in "handleInitial" #184

Closed Sociosarbis closed 3 years ago

Sociosarbis commented 3 years ago

Issue description or question

I read the source code and found "this.outputPath" is assigned only once in "apply" method(besides the intialization in constuctor).

I think it would not cause any problem in mose cases.But when I feed webpack with a array structured config it broke, as "output.path" in each subconfig is not the same and "compiler.options.output.path" would change in this case.

Although maybe my webpack config is not a appropriate one, if "compiler.options.output.path" changed(via other plugins,array structured config with different output path,etc.)in bundling, the plugin will not work as expected(remove files under output.path).

To solve this problem, just add this line in handleInitial

this.outputPath = compilation.outputOptions.path;

Webpack Config

// just for demonstration
webpack([
  merge(BASE_CONFIG, { 
    entry: { 
      a: './a.js'
    },
    output: {
      path: path.join(__dirname, 'dist/a'),
      publicPath: '/a'
    },
    plugins: [
      new HtmlWebpackPlugin({
        template: path.join(__dirname, 'template/a.html'),
        filename: 'a.html'
      })
    ],
  }),
  merge(BASE_CONFIG, {
    entry: {
      b: './b.js'
    },
    output: {
      path: path.join(__dirname, 'dist/b'),
      publicPath: 'b'
    },
    plugins: [
      new HtmlWebpackPlugin({
      template: path.join(__dirname, 'template/b.html'),
      filename: 'b.html'
    })
    ]
  })
]);

Environment

  System:
    OS: Windows 10 10.0.18362
    CPU: (8) x64 Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
    Memory: 2.06 GB / 7.90 GB
  Binaries:
    Node: 12.14.0 - D:\nodejs\node.EXE
    Yarn: 1.12.3 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.13.4 - D:\nodejs\npm.CMD
  npmPackages:
    clean-webpack-plugin: ^3.0.0 => 3.0.0
    webpack: ^4.42.1 => 4.43.0
Sociosarbis commented 3 years ago

I read the comment "Only happens once" .That means the plugin is designed to apply only once and the issue for output path changed in bundling should not happen.