更广泛的处理 terser-webpack-plugin?

More extensive mangling terser-webpack-plugin?

有没有办法彻底破坏 vuewebpack 捆绑在一起的组件?

当通过 terser-webpack-plugin 应用重组且 mangle.properties 设置为 true 时,并非所有 属性 名称都会被重组,例如:

location: {
  lng: -.134281,
  lat:51.513508,
  zoom:13,
  pitch:1,
  bearing:60
}

变成

location:{
  k:-.134281,
  M:51.513508,
  zoom:13,
  pitch:1,
  V:60
}

编辑

根据要求:Webpack 配置文件的相关部分,在本例中为默认的 vie-cli 配置,手动添加了 mangle.properties 项:

minimizer: [
      {
        options: {
          test: /\.m?js(\?.*)?$/i,
          chunkFilter: () => true,
          warningsFilter: () => true,
          extractComments: false,
          sourceMap: false,
          cache: true,
          cacheKeys: defaultCacheKeys => defaultCacheKeys,
          parallel: true,
          include: undefined,
          exclude: undefined,
          minify: undefined,
          terserOptions: {
            output: {
              comments: /^\**!|@preserve|@license|@cc_on/i
            },
            compress: {
              arrows: false,
              collapse_vars: false,
              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,
              booleans: true,
              if_return: true,
              sequences: true,
              unused: true,
              conditionals: true,
              dead_code: true,
              evaluate: true
            },
            mangle: {
              safari10: true,
              properties: true
            }
          }
        }
      }
    ],

这两个属性(zoompitch)恰好包含在 UglifyJS 在 mangling 期间内部使用的 reserved name list, have a look at this default domprops.json 文件中。

A default exclusion file is provided in tools/domprops.json which should cover most standard JS and DOM properties defined in various browsers. Pass --mangle-props domprops to disable this feature

如果您想保留此默认列表,您可以在插件的 custom minify option 中执行以下任一操作:

  1. 创建您的自定义保留名称列表,
  2. 加载默认列表 (domprops.json) 并传入 function/filter 以删除不需要的名称,
  3. 如果您确定没有名称冲突,只需合并这两个文件即可。

webpack.config.js

{
  optimization: {
    minimizer: [
      new TerserPlugin({
        minify(file, sourceMap) {
          const uglifyJsOptions = {
            mangle: {
              properties: {
                reserved: require('your_custom_list')
              }

              // Or filter them

              properties: {
                reserved: require('uglify-js/tools/domprops.json')
                  .filter(name => ![
                    'zoom',
                    'pitch'
                  ]
                  .includes(name))
              }
            }
          };

          return require('uglify-js').minify(file, uglifyJsOptions);
        },
      }),
    ],
  },
}

此外,在执行此操作时请注意 mangle.reservedmangle.properties.reserved 之间的相似之处,因为后者可能是您在这里需要的。查看 minify option structure.