Background: a simple one page react app + redux + express (in order to upload to heroku).
I already have index.html and images folder in the public folder. I want to run webpack to generate the styles.css and bundle.js that I used in my index.html. I want to use webpack-clean-up-plugin to remove the extra previous files every time I run build, but I don't want that to affect things like index.html and images folder.
According to the documentation:
*Options
If you want to keep some files in the output path, e.g. a stats.json file generated from some other plugins, use the exclude Array option. It accepts globbing as in minimatch.
// Do not delete stats.json
, important.json
, and everything in folder
new WebpackCleanupPlugin({
exclude: ["stats.json", "important.js", "folder/**/*"],
})
Goal: to run webpack.prod.js so in the end the public folder has
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/*"],
})
],
Current result:
every time I npm run
"build:prod": "webpack --config webpack.config.prod.js --mode production",
I would get my index.html and images folder deleted. So where did I write wrong?
Instead of using exlude
use this option
cleanOnceBeforeBuildPatterns: ["**/*", "!stats.json","!important.js", "!folder/**/*"]
this works for me :) . Example:
new CleanWebpackPlugin({
root: process.cwd(),
verbose: true,
dry: false,
cleanOnceBeforeBuildPatterns: ["**/*", "!stats.json","!important.js", "!folder/**/*"],
})
Anyways, after reading more I decided not to use this plugin any more. Because people have been repeating similar issues as I have. And the author said on Mar 22 2018 that he doesn't have time to maintain any more that we should dismiss this plugin. view this on github
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With