[Vue CLI 3] Uglify 相干的运用和设想

在本文最先之前,先留一个题目?

假如在新版本我想加一个
drop_console 的设置呢?

老版本的脚手架天生的设置中,关于线上环境的文件:webpack.prod.conf.js

使用了插件:uglifyjs-webpack-plugin

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

插件设置以下:

plugins: [
  new UglifyJsPlugin({
    uglifyOptions: {
      compress: {
        warnings: false
      }
    },
    sourceMap: config.build.productionSourceMap,
    parallel: true
  })
]

我们看一下新版本的是怎样处置惩罚的呢?

在文件:@vue/cli-service/lib/config/prod.js 中:

先有一个推断,测试环境不紧缩:经由过程 optimization.minimize

if (process.env.VUE_CLI_TEST) {
  webpackConfig.optimization.minimize(false)
}

然后假如不是测试环境:

第一步也是加载插件 uglifyjs-webpack-plugin

const UglifyPlugin = require('uglifyjs-webpack-plugin')

第二步举行插件设置:经由过程 optimization.minimizer

webpackConfig.optimization.minimizer([
  new UglifyPlugin(uglifyOptions(options))
])

这里的内置设置有哪些呢?我们看看 uglifyOptions.js 文件:

比拟之前的 uglifyOptions 要多许多:

module.exports = options => ({
  uglifyOptions: {
    compress: {
      // turn off flags with small gains to speed up minification
      arrows: false,
      collapse_vars: false, // 0.3kb
      comparisons: false,
      computed_props: false,
      hoist_funs: false,
      hoist_props: false,
      hoist_vars: false,
      inline: false,
      loops: false,
      negate_iife: false,
      properties: false,
      reduce_funcs: false,
      reduce_vars: false,
      switches: false,
      toplevel: false,
      typeofs: false,

      // a few flags with noticable gains/speed ratio
      // numbers based on out of the box vendor bundle
      booleans: true, // 0.7kb
      if_return: true, // 0.4kb
      sequences: true, // 0.7kb
      unused: true, // 2.3kb

      // required features to drop conditional branches
      conditionals: true,
      dead_code: true,
      evaluate: true
    },
    mangle: {
      safari10: true
    }
  },
  sourceMap: options.productionSourceMap,
  cache: true,
  parallel: true
})
    原文作者:dailyvuejs
    原文地址: https://segmentfault.com/a/1190000016254326
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞