I'm using Angular 9 and I have some code like this:
(features.ts, autogenerated:)
// AUTO-GENERTATED FILE. DO NOT EDIT!
export const Features = {
// Whether to reveal our Secret New Feature to the world
ENABLE_SECRET_FEATURE: 1
};
(mycode.ts, app code)
import { Features } from 'generated/features.ts';
function doSomething() {
if (Features.ENABLE_SECRET_FEATURE) {
doAIBlockChainARThing();
} else {
doSomeBoringOldCRUDThing();
}
}
I'd like the code emitted to be EITHER
function doSomething() {
doAIBlockChainARThing();
}
OR
function doSomething() {
doSomeBoringOldCRUDThing();
}
but not both.
Is there an invocation of ng build
that would make this happen? I know uglify can sort of do this and Closure Compiler certainly can. My current invocation is: ng build --aot=true --stats-json --buildOptimizer=true --optimization=true --prod
After some experimentation I have see that the terser settings in Angular prod builds do what I want if I have code like:
const SECRET_FEATURE = true;
if (SECRET_FEATURE) {
// code here is emitted
} else {
// code here is NOT emitted
}
However if I try to do something like:
import {SECRET_FEATURE} from 'my-features';
if (SECRET_FEATURE) { // this conditional is emitted
// this code is emitted
} else {
// this code is also emitted
}
My thought is I'll have to use something like https://www.npmjs.com/package/tsickle for better dead-code elimination, along with a custom WebPack config to call it. I was hoping for a more Angular-centric path just so I don't have to create/document a lot of custom machinery for future engineers.