Antoine Drian
Antoine Drian

Reputation: 41

angular ng build --prod does not keep the name of the class (MyClass.constructor.name)

When compiling in angular 7 production mode I can't keep the class names. I tried the different parameters of the Terser plugin but none of them solve this problem. Is there a solution for this problem? Or is there another alternative to MyClass.constructor.name.

Thank you in advance

Below the custom Webpack configuration

const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
    optimization: {
        minimizer: [
            new TerserPlugin({
                parallel: true,
                terserOptions: {
                    keep_classnames: true,
                    mangle: false
                }
            })
        ]
    }
};

Upvotes: 2

Views: 2023

Answers (1)

Guerric P
Guerric P

Reputation: 31815

As any other optimized Javascript code, an Angular app in production mode goes through a minification process that changes all classes, functions and variables names to minimal ones (usually one letter) in order to reduce the loaded bundle size. So you can't rely on the obj.constructor.name and compare it to a static value.

The clean, and "minification safe" alternative to obj.constructor.name === 'MyClass' is obj instanceof Myclass

Upvotes: 2

Related Questions