feat(@angular-devkit/build-optimizer): scrub ɵsetClassMetadata and ɵɵsetNgModuleScope calls

This commit is contained in:
Filipe Silva 2019-09-24 15:50:18 +01:00 committed by vikerman
parent b1f7537342
commit 5564ce6ea1
2 changed files with 60 additions and 1 deletions

View File

@ -17,6 +17,8 @@ export function testScrubFile(content: string) {
'__decorate',
'propDecorators',
'ctorParameters',
'ɵsetClassMetadata',
'ɵɵsetNgModuleScope',
];
return markers.some((marker) => content.indexOf(marker) !== -1);
@ -51,7 +53,8 @@ function scrubFileTransformer(checker: ts.TypeChecker, isAngularCoreFile: boolea
}
const exprStmt = node as ts.ExpressionStatement;
// Do checks that don't need the typechecker first and bail early.
if (isCtorParamsAssignmentExpression(exprStmt)) {
if (isIvyPrivateCallExpression(exprStmt)
|| isCtorParamsAssignmentExpression(exprStmt)) {
nodes.push(node);
} else if (isDecoratorAssignmentExpression(exprStmt)) {
nodes.push(...pickDecorationNodesToRemove(exprStmt, ngMetadata, checker));
@ -310,6 +313,24 @@ function isAssignmentExpressionTo(exprStmt: ts.ExpressionStatement, name: string
return true;
}
function isIvyPrivateCallExpression(exprStmt: ts.ExpressionStatement) {
const callExpr = exprStmt.expression;
if (!ts.isCallExpression(callExpr)) {
return false;
}
const propAccExpr = callExpr.expression;
if (!ts.isPropertyAccessExpression(propAccExpr)) {
return false;
}
if (propAccExpr.name.text != 'ɵsetClassMetadata'
&& propAccExpr.name.text != 'ɵɵsetNgModuleScope') {
return false;
}
return true;
}
// Remove Angular decorators from`Clazz.decorators = [...];`, or expression itself if all are
// removed.
function pickDecorationNodesToRemove(

View File

@ -612,4 +612,42 @@ describe('scrub-file', () => {
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
});
describe('Ivy', () => {
it('removes ɵsetClassMetadata call', () => {
const output = tags.stripIndent`
import { Component } from '@angular/core';
${clazz}
`;
const input = tags.stripIndent`
${output}
/*@__PURE__*/ i0.ɵsetClassMetadata(Clazz, [{
type: Component,
args: [{
selector: 'app-lazy',
template: 'very lazy',
styles: []
}]
}], null, null);
`;
expect(testScrubFile(input)).toBeTruthy();
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it('removes ɵɵsetNgModuleScope call', () => {
const output = tags.stripIndent`
import { CommonModule } from '@angular/common';
import * as i0 from "@angular/core";
${clazz}
`;
const input = tags.stripIndent`
${output}
/*@__PURE__*/ i0.ɵɵsetNgModuleScope(Clazz, { declarations: [], imports: [CommonModule] });
`;
expect(testScrubFile(input)).toBeTruthy();
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
});
});