1.0.3 • Published 3 years ago

ngx-uglifier v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

ngx-uglifier uglifies your angular library, meaning it shortens class member names to be of a single character. It utilizes terser to do the uglification, terser is used by the angular compiler to minify the code in build time, so it is already installed by angular.

The uglification also reduces your library bundle size since it shortens class member names.

Uglifying is not the same as obfuscating which mangles the code. This package only shortens the names of classes/methods/variables.

There are 2 stages for uglifying the code: 1. building your library as usual, this will produce a transpiled .js files. 2. uglifying the transpiled files.

When building an angular library, the following names need to remain unchanged, all others can be shortened:

  • public exports, those are usually class names
  • public class methods/variables which should be accessed from outside the class
  • public interfaces AND their properties

All class members declared as protected/private can be uglified. However, since the uglification works on .js file there can be no distinction between public and protected/private members. In order to provide this distinction we need to plan our code for it. My suggestion is to prefix the protected/private members with an underscore or something alike, you can then pass an option to terser telling it to uglify only members which start with underscore.

Here is a sample code for a directive having private variables/methods prefixed with underscore. In this sample the only names that will be preserved after uglification will be the class name MyLibDirective and the public method isEmbed.

@Directive({ selector: '[myLib]' })
export class MyLibDirective {
  private _options;

  constructor(private viewContainer: ViewContainerRef) {
	  this._initOptions();
  }

  private _initOptions() {
	return this._options = { isEmbed: true };
  }

  public isEmbed() {
	return this._options.isEmbed;
  }
}

Installation

npm install -D ngx-uglifier

Uglification

Uglification is made by running a script which processes the built code. The script will contain a config object with the input/output folder names.

const NgxUglifier = require('ngx-uglifier');

const config = { projectName: 'my-lib' }
const ngxUglifier = new NgxUglifier(config);
ngxUglifier.init().then();

config object

The config object mainly contains folder names and optionally some uglification options. The interface name is NgxUglifierConfig.

name
projectNamethe library name
srcParentFolderthis will usually be 'dist', if you omit this property it will default to 'dist'
destParentFolderan optional property which specifies the parent folder where the uglified code will be placed.if you want it to be placed at 'uglified/projectName' then pass 'uglified' here.if you omit this property or have it the same as srcParentFolder then the uglified code will be placed at srcParentFolder/projectName.
uglifyOptionsan optional object of interface NgxUglifierOptions containing options you want to pass to terser.The options and their default values are described below.

uglifyOptions object

The uglifyOptions object contain properties which will be used by terser to uglify the code. This object is optional, the default values are optimized for es6 apps. The interface name is NgxUglifierOptions.

Option
ecmapass 5, 2015, 2016, etc.default: 2015
isModuleUse when minifying an ES6 module.default: true
sourceMapgenerate source maps or not. default: false
classesspecify class names to preserve, pass a regular expression or true to preserve all.default: /^_/     (preserve those which start with underscore).
functionsspecify function names to preserve, pass a regular expression or true to preserve all.default: /^_/     (preserve those which start with underscore).
propertiesspecify variable names to preserve, pass a regular expression or true to preserve all.default: /^_/     (preserve those which start with underscore).
topLevelset to true if you wish to enable top level variable and function name mangling and to drop unused variables and functions.default: false
isLegacyAccessorsDefinitiontypescript 3.7 (used by angular >=9) has breaking changes from earlier versions in regards to setters/getters in the produced definition files (the d.ts files).so if you build your lib with angular 9 and consume it by an angular 8 app, then the app's typescript compiler (version <= 3.5) will not recognize the getters/setters syntax produced by typescript 3.7.passing true will make the setters/getters definition to be produced as the old syntax which is compatible to BOTH the old typescript and and the new one.default: true
1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago