首页 > 解决方案 > 为什么我的 Webpack-cleanup-plugin 会删除我的排除文件?

问题描述

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

// 不要删除stats.json, important.json, 和里面的所有东西folder

new WebpackCleanupPlugin({
  exclude: ["stats.json", "important.js", "folder/**/*"],
})

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

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 run

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

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

标签: webpackwebpack-plugin

解决方案


而不是使用排除
使用此选项

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

这对我有用:)。例子:

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

推荐阅读