How to configure UglifyJsPlugin to manage variables with regex in angular 4.0

I want Uuglify Angualr 4 output file files and mangle specific variables that start with my_. The command line below does what I want. I just want to say that the uglifyJs plugin in the angular-cli web package does the same.

> 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

I am currently exporting a webpack.config.js file from angular-cli with an extract command. But I just can't find the documentation on how to tell uglifyJsplugin from the auto-generated file the regular expression and cache-name parameters. Both of these factors are critical to our application.

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 . 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"   
 }
+4
2

webpack-uglifyjs , nameCache uglifyjs. ​​ 1.1.0.

Cache , .

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

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

Source: https://habr.com/ru/post/1689037/


All Articles