为什么我的 Webpack-cleanup-plugin 会删除我的排除文件?

Why does my Webpack-cleanup-plugin delete my Exclude files?

背景: 一个简单的一页react app + redux + express (为了上传到heroku).
我已经在 public 文件夹中有 index.html 和图像文件夹。我想 运行 webpack 生成我在 index.html 中使用的 styles.css 和 bundle.js。我想在每次 运行 构建时使用 webpack-clean-up-plugin 删除多余的先前文件,但我不希望它影响 index.html 和图像文件夹等内容。
根据文档: *选项 如果你想在输出路径中保留一些文件,例如从其他一些插件生成的 stats.json 文件,使用排除数组选项。它像在 minimatch 中一样接受 globbing。

// 不要删除stats.jsonimportant.json以及folder

中的所有内容
new WebpackCleanupPlugin({
  exclude: ["stats.json", "important.js", "folder/**/*"],
})

目标:到运行 webpack.prod.js所以最后public文件夹有

Webpack.config.prod.js:

const { CleanWebpackPlugin } = require('clean-webpack-plugin')  
output: {
        path: path.join(__dirname, 'public'),
        filename: 'bundle.js'
    }, 
    plugins: [
        new MiniCssExtractPlugin({ 
            filename: "styles.css", 
            chunkFilename: "[id].css"}),
         new CleanWebpackPlugin({
         exclude: ["index.html", "images/*"],
        })
      ],  

当前结果:
每次我 npm 运行

"build:prod": "webpack --config webpack.config.prod.js --mode production",

我会删除我的 index.html 和图像文件夹。那我哪里写错了?

这可能是一个路径problem.you应该用'__dirname'解决它

无论如何,看了更多之后我决定不再使用这个插件了。因为人们一直在重复我遇到的类似问题。并且作者在2018年3月22日说他没有时间再维护我们应该关闭这个插件。 view this on github

您可以使用 remove-files-webpack-plugin.

配置:

plugins: [
  new RemovePlugin({
    before: {
      // expects what your output folder is `dist`.
      test: [
        {
          folder: './dist',
          method: () => true
        }
      ],
      exclude: [
        './dist/index.html',
        './dist/images'
      ]
    }
  })
]

注意:我是这个插件的创建者。

而不是使用 exlude
使用这个选项

cleanOnceBeforeBuildPatterns: ["**/*", "!stats.json","!important.js", "!folder/**/*"]

这对我有用:)。 示例:

new CleanWebpackPlugin({
    root: process.cwd(),
    verbose: true,
    dry: false,
    cleanOnceBeforeBuildPatterns: ["**/*", "!stats.json","!important.js", "!folder/**/*"],
})

显然,未明确引用的文件被视为陈旧文件并被删除。这对我有用,可以跳过 CopyWebpackPlugin

复制的清理文件
new CleanWebpackPlugin({
   cleanStaleWebpackAssets: false,
}),