乐趣区

关于webpack:webpack的cleanwebpackplugin插件报错

1、出错代码

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

module.exports = {
  entry: './input.js',
  output: {path: path.resolve(__dirname, 'dist'),
    filename: 'output.bundle.js'
  },
  mode: 'development',
  plugins: [new CleanWebpackPlugin()
  ],
  module: {
    rules: [
      {test: /\.(png|jpg|gif)$/i,
        use: [
          {
            loader: 'url-loader',
            options: {
              // limit: 8192 // 阐明小于 8192 字节也就是 8k 才会执行
              limit: 919200
            }
          }
        ]
      }
    ]
  }
}

2、谬误形容

 [webpack-cli] TypeError: CleanWebpackPlugin is not a constructor
 

3、出错起因

导入插件语句有误,以及应用有误

4、解决

应改为如下导入语句:

    const {CleanWebpackPlugin} = require('clean-webpack-plugin')
    
    // es modules
    import {CleanWebpackPlugin} from 'clean-webpack-plugin';

而在应用时也是,如果都是默认清空 dist 文件下下的内容的话,默认不须要写参数。

 和默认删除目录不同时才须要传入门路,且须要通过选项 `cleanOnceBeforeBuildPatterns` 来传入。

参考【clean-webpack-plugin】

退出移动版