Skip to content

Commit

Permalink
fix(@angular-devkit/build-optimizer): support jit mode guarded class …
Browse files Browse the repository at this point in the history
…metadata removal

This adds support for the alternate emit form of `ɵsetClassMetadata` that is guarded by `ngJitMode` instead of a pure annotation.
  • Loading branch information
clydin authored and alan-agius4 committed Oct 15, 2020
1 parent 31875e9 commit 2f45677
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
Expand Up @@ -49,13 +49,24 @@ function scrubFileTransformer(
}

const exprStmt = node as ts.ExpressionStatement;
const iife = getIifeStatement(exprStmt)?.expression;
// Do checks that don't need the typechecker first and bail early.
if (isIvyPrivateCallExpression(exprStmt) || isCtorParamsAssignmentExpression(exprStmt)) {
if (isCtorParamsAssignmentExpression(exprStmt)) {
nodes.push(node);
} else if (iife && isIvyPrivateCallExpression(iife, 'ɵsetClassMetadata')) {
nodes.push(node);
} else if (
iife &&
ts.isBinaryExpression(iife) &&
isIvyPrivateCallExpression(iife.right, 'ɵsetClassMetadata')
) {
nodes.push(node);
} else if (isDecoratorAssignmentExpression(exprStmt)) {
nodes.push(...pickDecorationNodesToRemove(exprStmt, ngMetadata, checker));
} else if (isDecorateAssignmentExpression(exprStmt, tslibImports, checker)
|| isAngularDecoratorExpression(exprStmt, ngMetadata, tslibImports, checker)) {
} else if (
isDecorateAssignmentExpression(exprStmt, tslibImports, checker) ||
isAngularDecoratorExpression(exprStmt, ngMetadata, tslibImports, checker)
) {
nodes.push(...pickDecorateNodesToRemove(exprStmt, tslibImports, ngMetadata, checker));
} else if (isPropDecoratorAssignmentExpression(exprStmt)) {
nodes.push(...pickPropDecorationNodesToRemove(exprStmt, ngMetadata, checker));
Expand Down Expand Up @@ -299,8 +310,8 @@ function isAssignmentExpressionTo(exprStmt: ts.ExpressionStatement, name: string
return true;
}

function isIvyPrivateCallExpression(exprStmt: ts.ExpressionStatement) {
// Each Ivy private call expression is inside an IIFE as single statements, so we must go down it.
// Each Ivy private call expression is inside an IIFE
function getIifeStatement(exprStmt: ts.ExpressionStatement): null | ts.ExpressionStatement {
const expression = exprStmt.expression;
if (!expression || !ts.isCallExpression(expression) || expression.arguments.length !== 0) {
return null;
Expand All @@ -326,18 +337,22 @@ function isIvyPrivateCallExpression(exprStmt: ts.ExpressionStatement) {
return null;
}

return innerExprStmt;
}

function isIvyPrivateCallExpression(expression: ts.Expression, name: string) {
// Now we're in the IIFE and have the inner expression statement. We can check if it matches
// a private Ivy call.
const callExpr = innerExprStmt.expression;
if (!ts.isCallExpression(callExpr)) {
if (!ts.isCallExpression(expression)) {
return false;
}
const propAccExpr = callExpr.expression;

const propAccExpr = expression.expression;
if (!ts.isPropertyAccessExpression(propAccExpr)) {
return false;
}

if (propAccExpr.name.text !== 'ɵsetClassMetadata') {
if (propAccExpr.name.text != name) {
return false;
}

Expand Down
Expand Up @@ -855,7 +855,7 @@ describe('scrub-file', () => {
});

describe('Ivy', () => {
it('removes ɵsetClassMetadata call', () => {
it('removes ɵsetClassMetadata call with pure annotation', () => {
const output = tags.stripIndent`
import { Component } from '@angular/core';
${clazz}
Expand All @@ -875,5 +875,26 @@ describe('scrub-file', () => {
expect(testScrubFile(input)).toBeTruthy();
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('removes ɵsetClassMetadata call', () => {
const output = tags.stripIndent`
import { Component } from '@angular/core';
${clazz}
`;
const input = tags.stripIndent`
${output}
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && 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}`);
});
});
});

0 comments on commit 2f45677

Please sign in to comment.