关于webpack打包插件uglifyjs-webpack-plugin报错

项目进行打包优化,配置了打包环境,代码如下:

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
configureWebpack: (config) => {
    if (isProd) {
      const plugins = []
      plugins.push(
        new UglifyJsPlugin ({
          sourceMap: false,
          // 多进程
          parallel: true,
          uglifyOptions: {
            warnings: false,
            compress: {
              drop_console: false,
              drop_debugger: false
            }
          },
        })
      );
      config.plugins = [...config.plugins, ...plugins]
    }
  }

但是打包发现报错

-  Building for production...

 ERROR  Failed to compile with 1 errors                                                                        9:36:08

 error

static/js/vendors~app.042c0081.js from UglifyJs
undefined

 ERROR  Build failed with errors.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! cli4@0.1.0 build:prod: `vue-cli-service build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the cli4@0.1.0 build:prod script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\14581\AppData\Roaming\npm-cache\_logs\2020-09-10T01_36_08_213Z-debug.log

这里的

 error

static/js/vendors~app.042c0081.js from UglifyJs
undefined

类似这种报错,是因为最新版的uglifyjs-webpack-plugin插件已经不支持es6语法(https://github.com/webpack-contrib/uglifyjs-webpack-plugin/blob/master/CHANGELOG.md#200-2018-09-14)

在这里插入图片描述

解决方法

用插件terser-webpack-plugin代替uglifyjs-webpack-plugin

配上terser-webpack-plugin:用法

const TerserPlugin = require('terser-webpack-plugin')
configureWebpack: (config) => {
    if (isProd) {
      const plugins = []
      plugins.push(
        new TerserPlugin({
          cache: true,
          sourceMap: false,
          // 多进程
          parallel: true,
          terserOptions: {
            warnings: false,
            compress: {
              drop_console: false,
              drop_debugger: false
            }
          },
        })
      );
      config.plugins = [...config.plugins, ...plugins]
    }
  }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值