I want to uglify Angualr 4 output files file and mangle specific variables which start with my_ . The command line below does the exact thing I want. I just want to be able to tell uglifyJs plugin in angular-cli's webpack to do the same thing.
> 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
Currently I export the webpack.config.js from angular-cli with eject command. But I just can't find any documentation on how to tell uglifyJsplugin of the autogenerated file the regex and name-cache parameters. Both of those are critical for 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": {}
}
}),
Here is blog post about how to use angualr eject to capture the auto-generated weppack.config and modify it. Angular-cli : How to ignore class names from being minified But can't find anything on how to specify the regex for the ugllify plugin
Thanks in advance.
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"
}
There was a bug in webpack-uglifyjs plugin which did not transmit the nameCache value to the uglifyjs. This bug is fixed in ver 1.1.0.
The nameCache will have to be crated and then saved to a file with another plugin.
This goes into 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”: {}
}
}),
...
This goes into 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;