优化Webpack打包时产生的错误uglifyjs / terser

在学习webpack优化打包时,发生的打包错误。使用uglifyjs-webpack-plugin或terser-webpack-plugin插件优化打包,都会报这样的错误。

vue.config.js配置

const { defineConfig } = require('@vue/cli-service')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin');

module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    proxy: {
      '/api': {
        target: 'https://127.0.0.1:3000/', 
        ws: true,
        changeOrigin: true, 
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  },
  // TerserPlugin 
  // configureWebpack: {
  //   optimization: {
  //     minimizer: [
  //       new TerserPlugin({
  //         terserOptions: {
  //           compress: {
  //             warnings: false,
  //             drop_console: true,
  //             drop_debugger: true,
  //             pure_funcs: ["console.log"]
  //           }
  //         }
  //       })
  //     ],
  //   },
  // }
 // UglifyJsPlugin
  configureWebpack: config => {
    // 生产环境
    if (process.env.NODE_ENV == 'production') {
      // 压缩代码
      config.plugins.push(
        new UglifyJsPlugin({
          uglifyOptions: {
            warnings: false, // 不显示警告信息
            compress: {
              // warnings: false, // 打包错误,则注释这一行
              drop_debugger: true, // 删除debug
              drop_console: true, // 移除console.* 、log=console.log
              pure_funcs: ['console.log']
            }
          },
          sourceMap: false, // 缩小打包体积
          parallel: true, // 使用多进程压缩
          cache: true, // 启用缓存
          extractComments: true, // 抽离注释,单独抽出成一个文件 [name].[hash].[ext].LICENSE
        })
      )
    }
  }

})

直接执行打包Webpack命令,是没有问题,就是配置vue.config.js之后,不行了。

# webpack打包
npm run build

当然,也找到一些方案,说是webpack版本与插件版本不对应产生的。

// ...
webpack 5.x 匹配terser-webpack-plugin 5.x
webpack 4.x 匹配terser-webpack-plugin 4.x

找了半天暂时还没有解决,大家有什么好的解决方案吗?欢迎留言!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Webpack 打包优化可以从以下方面入手: 1. 减少打包体积:Webpack 打包的体积越小,页面加载的速度就越快,用户体验就越好。可以通过以下方式减少打包体积: - 压缩代码:使用 UglifyJSTerser 等工具压缩代码; - 按需加载:使用 SplitChunksPlugin 将公共代码抽离成单独的文件; - Tree Shaking:通过配置 module.rules 中的 sideEffects 属性,让 Webpack打包去掉没有使用的代码; - 使用静态分析工具:使用 BundleAnalyzerPlugin 等工具分析打包后的文件结构,找出冗余代码并删除。 2. 加快打包速度:Webpack 打包速度越快,开发效率就越高。可以通过以下方式加快打包速度: - 多进程打包:使用 HappyPack、thread-loader 等工具开启多个进程进行打包; - 使用缓存:使用 cache-loader、hard-source-webpack-plugin 等工具将中间结果缓存起来,下次打包直接使用缓存结果,减少耗; - 减少文件搜索范围:使用 resolve.alias、resolve.extensions 等配置项减少 Webpack 在文件系统中搜索文件的范围。 3. 提高代码质量:提高代码质量可以让代码更易于维护和扩展。可以通过以下方式提高代码质量: - 使用 ESLint、Prettier 等工具对代码进行规范化和格式化; - 使用 TypeScript、Flow 等类型检查工具提高代码的可靠性; - 使用代码分割、懒加载等技术降低代码的复杂度和耦合度。 综上所述,Webpack 打包优化需要综合考虑打包体积、打包速度和代码质量等方面,通过合理的配置和工具选择提高打包效率和开发体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值