webpack - 为什么我的 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 所以最后公共文件夹有
- 索引.html
- 捆绑.js
- bundle.js.map
- 样式.css
- 样式.css.map
- 图片(文件夹)
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 文件夹。那我哪里写错了?
解决方案
而不是使用排除
使用此选项
cleanOnceBeforeBuildPatterns: ["**/*", "!stats.json","!important.js", "!folder/**/*"]
这对我有用:)。例子:
new CleanWebpackPlugin({
root: process.cwd(),
verbose: true,
dry: false,
cleanOnceBeforeBuildPatterns: ["**/*", "!stats.json","!important.js", "!folder/**/*"],
})
推荐阅读
- angular - 在全局路由监听器中访问路由数据
- python - 在 Python 中聚类集群或合并集群以减少组数(Python)
- javascript - 如何从 Vue 中的 prop 对象访问单个元素
- python - 获取子标签值等于某物的html标签文本
- grails - 刷新 grails 服务类
- angular - NGRX 选择器在键值对象更改时触发
- azure - 使用 DevOps 在来自不同订阅的 azure key Vault 之间移动秘密
- php - 如何清除我的 csv 文件但使用 PHP cron 作业保留标题?
- oracle-apex - Oracle Apex 中应用程序级别的会话超时
- flutter - 当键盘出现时,AlertDialog 上的 Flutter RenderFlex 溢出错误