Angular/Typescript להסיר קוד שלא בשימוש בזמן הקומפילציה
אני משתמש ב-Angular 9 ויש לי קוד כזה:
(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();
}
}
אני רוצה שהקוד שיופק יהיה אֶתֶר
function doSomething() {
doAIBlockChainARThing();
}
או
function doSomething() {
doSomeBoringOldCRUDThing();
}
אבל לא שניהם.
האם יש קריאה ל-ng build שתגרום לכך לקרות? אני יודע ש-uglify יכול במידה מסוימת לעשות זאת ו-Closure Compiler בהחלט יכול. הקריאה הנוכחית שלי היא: ng build --aot=true --stats-json --buildOptimizer=true --optimization=true --prod
עדכון :
לאחר כמה ניסויים גיליתי שהגדרות ה-terser בבניות פרוד של Angular עושות את מה שאני רוצה אם יש לי קוד כמו:
const SECRET_FEATURE = true;
if (SECRET_FEATURE) {
// code here is emitted
} else {
// code here is NOT emitted
}
עם זאת, אם אני מנסה לעשות משהו כמו:
import {SECRET_FEATURE} from 'my-features';
if (SECRET_FEATURE) { // this conditional is emitted
// this code is emitted
} else {
// this code is also emitted
}