Skip to content

Commit

Permalink
feat(@angular-devkit/build-optimizer): scrub ɵsetClassMetadata and ɵɵ…
Browse files Browse the repository at this point in the history
…setNgModuleScope calls
  • Loading branch information
filipesilva authored and vikerman committed Sep 25, 2019
1 parent b1f7537 commit 5564ce6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export function testScrubFile(content: string) {
'__decorate',
'propDecorators',
'ctorParameters',
'ɵsetClassMetadata',
'ɵɵsetNgModuleScope',
];

return markers.some((marker) => content.indexOf(marker) !== -1);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
});
});

0 comments on commit 5564ce6

Please sign in to comment.