Angular-cli: how to ignore class names from minified

For the application, we need the class name not to be minified because we are using

var className = myObject.constructor.name;
export class myObject{ ... }

      

when we run

ng build - pro

the class name gets the minimum value in the random name.

+3


source to share


3 answers


Angular cli uses webpack and cleans up internally. One solution would be to change the parameters in uglify by exporting the webpack config. You can see the webpack files by running ng eject and ng eject --prod

new UglifyJsPlugin({
      "mangle": false,
      "compress": {
        "screw_ie8": true,
        "warnings": false
      },
      "sourceMap": false
    }),

      

Mangle = false will keep the class names. The lack of options for webpack in angular cli is one big discussion of atm.

You can change all exceptions like this:



mangle: {
    except: ['foozah']
  }

      

Note: after fetching, you can remove the extracted true from angular-cli.json to do it again, or serve / build normally.

"project": {
    "name": "test",
    "ejected": true //remove
  },

      

+3


source


Parameter in mangle:

"mangle":{
   "keep_names" : true
}

      



Keeps class names intact.

0


source


With Andres M's answer, you can turn off mangling without ng eject

directly modifying the Angular file that manages the settings for Webpack:node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js

Please note that this will be overwritten whenever @angular-devkit/build-angular

updated and not supported by Angular or NPM (and could kill your cat / cause nuclear armageddon, don't say I didn't warn you!)

I completely disconnected by changing uglifyOptions

to uglifyOptions: Object.assign(uglifyOptions, {mangle: false})

.

For reference, here's the relevant part from my modified file common.js

...
new UglifyJSPlugin({
  sourceMap: buildOptions.sourceMap,
  parallel: true,
  cache: true,
  uglifyOptions: Object.assign(uglifyOptions, {"mangle": false})
}),
...

      

0


source







All Articles