>[success] # plugins -- CleanWebpackPlugin(5.x) 1. 在 `webpack5.x` 已经内置只需要设置`output.clean` ~~~ module.exports = { //... output: { clean: true, // 在生成文件之前清空 output 目录 }, }; ~~~ ~~~ module.exports = { //... output: { clean: { dry: true, // 打印而不是删除应该移除的静态资源 }, }, }; ~~~ ~~~ module.exports = { //... output: { clean: { keep: /ignored\/dir\//, // 保留 'ignored/dir' 下的静态资源 }, }, }; // 或者 module.exports = { //... output: { clean: { keep(asset) { return asset.includes('ignored/dir'); }, }, }, }; ~~~ 你也可以使用钩子函数: ~~~ webpack.CleanPlugin.getCompilationHooks(compilation).keep.tap( 'Test', (asset) => { if (/ignored\/dir\//.test(asset)) return true; } ); ~~~