Skip to content

Commit 5564ce6

Browse files
filipesilvavikerman
authored andcommitted
feat(@angular-devkit/build-optimizer): scrub ɵsetClassMetadata and ɵɵsetNgModuleScope calls
1 parent b1f7537 commit 5564ce6

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

packages/angular_devkit/build_optimizer/src/transforms/scrub-file.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export function testScrubFile(content: string) {
1717
'__decorate',
1818
'propDecorators',
1919
'ctorParameters',
20+
'ɵsetClassMetadata',
21+
'ɵɵsetNgModuleScope',
2022
];
2123

2224
return markers.some((marker) => content.indexOf(marker) !== -1);
@@ -51,7 +53,8 @@ function scrubFileTransformer(checker: ts.TypeChecker, isAngularCoreFile: boolea
5153
}
5254
const exprStmt = node as ts.ExpressionStatement;
5355
// Do checks that don't need the typechecker first and bail early.
54-
if (isCtorParamsAssignmentExpression(exprStmt)) {
56+
if (isIvyPrivateCallExpression(exprStmt)
57+
|| isCtorParamsAssignmentExpression(exprStmt)) {
5558
nodes.push(node);
5659
} else if (isDecoratorAssignmentExpression(exprStmt)) {
5760
nodes.push(...pickDecorationNodesToRemove(exprStmt, ngMetadata, checker));
@@ -310,6 +313,24 @@ function isAssignmentExpressionTo(exprStmt: ts.ExpressionStatement, name: string
310313
return true;
311314
}
312315

316+
function isIvyPrivateCallExpression(exprStmt: ts.ExpressionStatement) {
317+
const callExpr = exprStmt.expression;
318+
if (!ts.isCallExpression(callExpr)) {
319+
return false;
320+
}
321+
const propAccExpr = callExpr.expression;
322+
if (!ts.isPropertyAccessExpression(propAccExpr)) {
323+
return false;
324+
}
325+
326+
if (propAccExpr.name.text != 'ɵsetClassMetadata'
327+
&& propAccExpr.name.text != 'ɵɵsetNgModuleScope') {
328+
return false;
329+
}
330+
331+
return true;
332+
}
333+
313334
// Remove Angular decorators from`Clazz.decorators = [...];`, or expression itself if all are
314335
// removed.
315336
function pickDecorationNodesToRemove(

packages/angular_devkit/build_optimizer/src/transforms/scrub-file_spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,4 +612,42 @@ describe('scrub-file', () => {
612612
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
613613
});
614614
});
615+
616+
describe('Ivy', () => {
617+
it('removes ɵsetClassMetadata call', () => {
618+
const output = tags.stripIndent`
619+
import { Component } from '@angular/core';
620+
${clazz}
621+
`;
622+
const input = tags.stripIndent`
623+
${output}
624+
/*@__PURE__*/ i0.ɵsetClassMetadata(Clazz, [{
625+
type: Component,
626+
args: [{
627+
selector: 'app-lazy',
628+
template: 'very lazy',
629+
styles: []
630+
}]
631+
}], null, null);
632+
`;
633+
634+
expect(testScrubFile(input)).toBeTruthy();
635+
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
636+
});
637+
638+
it('removes ɵɵsetNgModuleScope call', () => {
639+
const output = tags.stripIndent`
640+
import { CommonModule } from '@angular/common';
641+
import * as i0 from "@angular/core";
642+
${clazz}
643+
`;
644+
const input = tags.stripIndent`
645+
${output}
646+
/*@__PURE__*/ i0.ɵɵsetNgModuleScope(Clazz, { declarations: [], imports: [CommonModule] });
647+
`;
648+
649+
expect(testScrubFile(input)).toBeTruthy();
650+
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
651+
});
652+
});
615653
});

0 commit comments

Comments
 (0)