作用
- 由于修改代码导致最后生产的文件会不同,所以需要每次生成文件前清空dist目录,这样防止无用文件越来越多或者避免手动清除。
使用
- 安装:
npm i clean-webpack-plugin -D
- 进行配置,用法很简单,直接导入再new即可。
const {CleanWebpackPlugin}=require('clean-webpack-plugin');
plugins:[
new HtmlWebpackPlugin({
template:path.resolve(__dirname,'../public/index.html'),
filename:'index.html',
minify:!isdev && {
removeAttributeQuotes:true,
collapseWhitespace:true
}
}),
new CleanWebpackPlugin()
]
- 然后进行验证,在dist目录下新建一些乱七八糟的文件,然后重新build,会发现那些文件全部消失。
- 这个插件里可以有些设置,有个cleanOnceBeforePattern配置,配置满足其正则即可,默认不填就是这个配置是[**/*]配置。