如何在 angular 4.0 中设置 UglifyJsPlugin 以使用正则表达式处理变量

How to setup UglifyJsPlugin to mangle variables using regex in angular 4.0

我想 uglify Angualr 4 输出文件文件并破坏以 my_ 开头的特定变量。下面的命令行完全符合我的要求。我只想告诉 angular-cli 的 webpack 中的 uglifyJs 插件做同样的事情。

> uglifyjs script.js --source-map "filename='script.js.map',includeSources,content=inline" -o script.js -m
-c toplevel --mangle-props \"regex=/^my_[^_]{1}/\" --name-cache uglify-name-cache.json

目前我使用弹出命令从 angular-cli 导出 webpack.config.js。但是我找不到任何关于如何告诉自动生成文件的 uglifyJsplugin 正则表达式和名称缓存参数的文档。这两者对我们的应用程序都至关重要。

From webpack.config.js produced by eject command:

new UglifyJsPlugin({
  "test": /\.js$/i,
  "extractComments": false,
  "sourceMap": true,
  "cache": false,
  "parallel": false,
  "uglifyOptions": {
    "output": {
      "ascii_only": true,
      "comments": false
    },
    "ecma": 5,
    "warnings": false,
    "ie8": false,
    "mangle": true,
    "compress": {}
  }
}),

这里是关于如何使用 angualr eject 捕获自动生成的 weppack.config 并修改它的博客 post。 https://www.codesd.com/item/angular-cli-how-to-ignore-class-names-from-being-minified.html 但是找不到任何关于如何为 ugllify 插件指定正则表达式的信息

提前致谢。

Some other helpful info:

 "dependencies": {
    "@angular/common": "4.4.6",
    "@angular/compiler": "4.4.6",
    "@angular/core": "4.4.6",
    "@angular/http": "4.4.6",
    "@angular/platform-browser": "4.4.6",
    "@angular/platform-browser-dynamic": "4.4.6",
    "@angular/router": "4.4.6",   },   
 "devDependencies": {
    "@angular/cli": "1.5.0",
    "@angular/compiler-cli": "4.4.6",
    "@types/node": "7.0.43",
    "clean-webpack-plugin": "0.1.17",
    "codelyzer": "3.2.2",
    "copy-webpack-plugin": "4.2.0",
    "uglify-js": "3.1.8",
    "webpack": "3.8.1"   
 }

提供了 Webpack uglify og optimize,使用它,但我不保证它能像你一样工作。

const {optimize} = require("webpack")

new optimize.UglifyJsPlugin({
    beautify: false,
    output: {
      comments: false
    },
    mangle: {
      screw_ie8: true
    },
    compress: {
      screw_ie8: true,
      warnings: false,
      conditionals: true,
      unused: true,
      comparisons: true,
      sequences: true,
      dead_code: true,
      evaluate: true,
      if_return: true,
      join_vars: true,
      negate_iife: false
    }
  })

webpack-uglifyjs 插件中有一个错误,它没有将 nameCache 值传输到 uglifyjs。此错误已在版本 1.1.0 中修复。

必须创建 nameCache,然后使用另一个插件将其保存到文件中。

这进入 webpack.config.js:

   const WriteNameCachePlugin = require(‘./write-name-cache-plugin’);
    var nameCache = JSON.parse(fs.readFileSync(path.join(process.cwd(),“uglify-name-cache.json”), “utf8"));

...

    new UglifyJsPlugin({
      “test”: /\.js$/i,
      “extractComments”: false,
      “sourceMap”: true,
      “cache”: false,
      “parallel”: false,
      “uglifyOptions”: {
        “output”: {
          “ascii_only”: true,
          “comments”: false
        },
        “ecma”: 5,
        “warnings”: false,
        “ie8": false,
        “nameCache”: nameCache,
        “mangle”: {
          properties: {
              regex: /^my_[^_]{1}/,
              reserved: [“$”, “_”]
          }
        },
        “compress”: {}
      }
    }),
...

这进入写入名称缓存-plugin.js

   const fs = require(‘fs’);

   var opt;

   function WriteNameCachePlugin(options) {
        opt = options;
    }


   WriteNameCachePlugin.prototype.apply = function(compiler) {
        compiler.plugin(‘done’, function() {
            fs.writeFileSync(opt.fileName, JSON.stringify(opt.nameCache, null, 4), “utf8");
        });
    };

   module.exports = WriteNameCachePlugin;