如何使用角度4.0中的正则表达式设置UglifyJsPlugin来修改变量

时间:2017-11-10 06:06:17

标签: angular webpack uglifyjs

我想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

目前我使用eject命令从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弹出来捕获自动生成的weppack.config并修改它的博客文章。 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"   
 }

2 个答案:

答案 0 :(得分:2)

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”: {}
      }
    }),
...

这将进入write-name-cache-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;

答案 1 :(得分:0)

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
    }
  })