clean-webpack-plugin 配置问题

来源:4-3 Webpack 和 Code Splitting(1)

慕沐7276372

2019-03-22

我在github 上查看clean-webpack-plugin 插件的文档 发现文档更新了 里面说 root 这个配置参数以后会不用了 给了新的配置参数
文档链接

new CleanWebpackPlugin({
    // Simulate the removal of files
    //
    // default: false
    dry: true,

    // Write Logs to Console
    // (Always enabled when dry is true)
    //
    // default: false
    verbose: true,

    // Automatically remove all unused webpack assets on rebuild
    //
    // default: true
    cleanStaleWebpackAssets: false,

    // Do not allow removal of current webpack assets
    //
    // default: true
    protectWebpackAssets: false,

    // **WARNING**
    //
    // Notes for the below options:
    //
    // They are unsafe...so test initially with dry: true.
    //
    // Relative to webpack's output.path directory.
    // If outside of webpack's output.path directory,
    //    use full path. path.join(process.cwd(), 'build/**/*')
    //
    // These options extend del's pattern matching API.
    // See https://github.com/sindresorhus/del#patterns
    //    for pattern matching documentation

    // Removes files once prior to Webpack compilation
    //   Not included in rebuilds (watch mode)
    //
    // Use !negative patterns to exclude files
    //
    // default: ['**/*']
    cleanOnceBeforeBuildPatterns: ['**/*', '!static-files*'],
    cleanOnceBeforeBuildPatterns: [], // disables cleanOnceBeforeBuildPatterns

    // Removes files after every build (including watch mode) that match this pattern.
    // Used for files that are not created directly by Webpack.
    //
    // Use !negative patterns to exclude files
    //
    // default: disabled
    cleanAfterEveryBuildPatterns: ['static*.*', '!static1.js'],

    // Allow clean patterns outside of process.cwd()
    //
    // requires dry option to be explicitly set
    //
    // default: false
    dangerouslyAllowCleanPatternsOutsideProject: true,
    dry: true,
});

其中的 cleanOnceBeforeBuildPatterns 这个配置参数我填进去了 把root 配置参数去掉 然后打包后 删除的文件路径总是
clean-webpack-plugin:
C:\Users\DELL\Desktop\MKlxWebpack4\build\diste has been removed.
应该是 C:\Users\DELL\Desktop\MKlxWebpack4\diste has been removed. 才对。

下面是我的目录和配置
图片描述

图片描述

请老师解答下,谢谢。

写回答

3回答

特别平凡的人

2020-03-04

const { CleanWebpackPlugin } = require('clean-webpack-plugin');

new CleanWebpackPlugin({

      verbose: true, // 在命令窗口中打印`clean-webpack-plugin`日志

      cleanOnceBeforeBuildPatterns: [path.resolve(__dirname, '../dist')] // 清除的文件/文件夹

    })

clean-webpack-plugin 现在的新版已经是 3.0.0 ,使用 `cleanOnceBeforeBuildPatterns`指定清楚目录了`root`参数以及取消了。


2
0

Mr苗先森丶

2019-09-25

引入:const { CleanWebpackPlugin } = require('clean-webpack-plugin'),配置:new CleanWebpackPlugin(),,看官网有说明的

0
0

慕沐7276372

提问者

2019-03-22

刚刚才发现不生效的原因 是插件还是1.0.0的版本更新后就可以了,尴尬了不过是个很好的提醒

0
0

从基础到实战 手把手带你掌握新版Webpack4.0

知识点+项目实例+原理讲解 全方位解析Webpack4新版本

3627 学习 · 1291 问题

查看课程