美文网首页
webpack编译报错:CleanWebpackPlugin i

webpack编译报错:CleanWebpackPlugin i

作者: 偶余杭 | 来源:发表于2020-01-04 14:55 被阅读0次

问题记录

使用webpack插件 cleanWebpackPlugin时,在编译时报错:

PS D:\project\ts_demo> npm run dll

> ts_demo@0.1.0 dll D:\project\ts_demo
> webpack -p --progress --config ./dll.config.js

D:\project\ts_demo\node_modules\webpack-cli\bin\cli.js:93
                                ^

TypeError: CleanWebpackPlugin is not a constructor
    at Object.<anonymous> (D:\project\ts_demo\dll.config.js:22:5)
    at Module._compile (D:\project\ts_demo\node_modules\v8-compile-cache\v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (D:\project\ts_demo\node_modules\v8-compile-cache\v8-compile-cache.js:161:20)
    at WEBPACK_OPTIONS (D:\project\ts_demo\node_modules\webpack-cli\bin\utils\convert-argv.js:114:13)
    at requireConfig (D:\project\ts_demo\node_modules\webpack-cli\bin\utils\convert-argv.js:116:6)
    at D:\project\ts_demo\node_modules\webpack-cli\bin\utils\convert-argv.js:123:17
    at Array.forEach (<anonymous>)
    at module.exports (D:\project\ts_demo\node_modules\webpack-cli\bin\utils\convert-argv.js:121:15)
    at yargs.parse (D:\project\ts_demo\node_modules\webpack-cli\bin\cli.js:71:45)
    at Object.parse (D:\project\ts_demo\node_modules\webpack-cli\node_modules\yargs\yargs.js:567:18)
    at D:\project\ts_demo\node_modules\webpack-cli\bin\cli.js:49:8
    at Object.<anonymous> (D:\project\ts_demo\node_modules\webpack-cli\bin\cli.js:366:3)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ts_demo@0.1.0 dll: `webpack -p --progress --config ./dll.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ts_demo@0.1.0 dll script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

查询使用的webpack版本和cleanWebpackPlugin版本:

    "webpack-cli": "^3.3.10"
    "clean-webpack-plugin": "^3.0.0",

当前使用插件的方式:

const CleanWebpackPlugin = require("clean-webpack-plugin");
module.exports = {
  plugins: [
    // 清除之前的dll文件
    new CleanWebpackPlugin(
      {
        cleanOnceBeforeBuildPatterns: ["*.*"]
      },
      {
        root: path.join(__dirname, dllPath)
      }
    ),
  ]
}

解决

查询 clean-webpack-plugin github ,得到其最新版本3.0.0导入方式修改了:

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

const webpackConfig = {
    plugins: [
        // 构造函数必须传入对象,不传也是可以,会取默认值
        new CleanWebpackPlugin(),
    ],
};

module.exports = webpackConfig;

修正如下:

const { CleanWebpackPlugin } = require("clean-webpack-plugin");
module.exports = {
  plugins: [
    // 清除之前的dll文件
    new CleanWebpackPlugin(),
  ]
}

正常运行

相关文章

网友评论

      本文标题:webpack编译报错:CleanWebpackPlugin i

      本文链接:https://www.haomeiwen.com/subject/xsnxactx.html