代码之家  ›  专栏  ›  技术社区  ›  David Dehghan

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

  •  4
  • David Dehghan  · 技术社区  · 8 年前

    我想把Angualr 4输出文件弄脏,并把以my_u2;开始的特定变量弄乱。下面的命令行正是我想要的。我只是想告诉angular cli的网页中的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
    

    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的博客文章。配置并修改它。 https://www.codesd.com/item/angular-cli-how-to-ignore-class-names-from-being-minified.html 但是找不到任何关于如何为Uglify插件指定正则表达式的信息

    提前谢谢。

    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 回复  |  直到 8 年前
        1
  •  2
  •   David Dehghan    8 年前

    nameCache必须装箱,然后用另一个插件保存到文件中。

    这会进入网页包。配置。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”: {}
          }
        }),
    ...
    

    这将进入写名称缓存插件。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;
    
        2
  •  0
  •   Rach Chen    8 年前

    提供网页包 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
        }
      })