When using UglifyJS, function names are mangled, unless keep_fnames is set to true. For example, the following Typescript code:
class Test {}
console.log(Test.name);
compiled to JS as:
function Test() {}
console.log(Test.name);
will be uglified to:
function t() {}
console.log(t.name);
and output t instead of test to the console.
Is there a way (other than using keep_fnames option) to preserve the name property after uglification ? (I don't want to use keep_fnames:true because it increases the bundle size quite a lot.
Possible solutions I thought of:
- Writing a Webpack plugin that hard codes the function name
Test.name = 'Test', but this won't work asFunction.prototype.nameis a read only property; - Using Typescript decorators, metadata and the reflection API, but
design:typemetadata is not emitted for classes, it's only emitted for properties (which I believe is becauseFunction.prototype.nameexists, but I guess they missed this edge case ?).