Skip to content

Commit

Permalink
fix(@ngtools/webpack): remove setClassMetadataAsync calls
Browse files Browse the repository at this point in the history
Updates the logic that remove `setClassMetadata` calls to also elide `setClassMetadataAsync`. The latter will be emitted when the component uses the new `defer` block syntax.
  • Loading branch information
crisbeto committed Sep 4, 2023
1 parent d321714 commit 0139e82
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Expand Up @@ -34,7 +34,10 @@ export function removeIvyJitSupportCalls(
const expression = ts.isBinaryExpression(innerExpression)
? innerExpression.right
: innerExpression;
if (isIvyPrivateCallExpression(expression, 'ɵsetClassMetadata')) {
if (
isIvyPrivateCallExpression(expression, 'ɵsetClassMetadata') ||
isIvyPrivateCallExpression(expression, 'ɵsetClassMetadataAsync')
) {
removedNodes.push(innerExpression);

return undefined;
Expand Down Expand Up @@ -120,7 +123,7 @@ function isIvyPrivateCallExpression(expression: ts.Expression, name: string) {
return false;
}

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

Expand Down
Expand Up @@ -137,6 +137,46 @@ const inputArrowFnWithImplicitReturn = tags.stripIndent`
}], null, null))();
`;

const inputAsync = tags.stripIndent`
export class TestCmp {
}
TestCmp.ɵfac = function TestCmp_Factory(t) { return new (t || TestCmp)(); };
TestCmp.ɵcmp = i0.ɵɵdefineComponent({ type: TestCmp, selectors: [["test-cmp"]], standalone: true, features: [i0.ɵɵStandaloneFeature], decls: 3, vars: 0, template: function TestCmp_Template(rf, ctx) { }, encapsulation: 2 });
(function () {
(typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵsetClassMetadataAsync(TestCmp,
function () { return [import("./cmp-a").then(function (m) { return m.CmpA; })]; },
function (CmpA) { i0.ɵsetClassMetadata(TestCmp, [{
type: Component,
args: [{
selector: 'test-cmp',
standalone: true,
imports: [CmpA],
template: '{#defer}<cmp-a />{/defer}',
}]
}], null, null); }); })();
`;

const inputAsyncArrowFn = tags.stripIndent`
export class TestCmp {
}
TestCmp.ɵfac = function TestCmp_Factory(t) { return new (t || TestCmp)(); };
TestCmp.ɵcmp = i0.ɵɵdefineComponent({ type: TestCmp, selectors: [["test-cmp"]], standalone: true, features: [i0.ɵɵStandaloneFeature], decls: 3, vars: 0, template: function TestCmp_Template(rf, ctx) { }, encapsulation: 2 });
(() => {
(typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵsetClassMetadataAsync(TestCmp,
() => [import("./cmp-a").then((m) => m.CmpA)],
(CmpA) => {
i0.ɵsetClassMetadata(TestCmp, [{
type: Component,
args: [{
selector: 'test-cmp',
standalone: true,
imports: [CmpA],
template: '{#defer}<cmp-a />{/defer}',
}]
}], null, null);
}); })();
`;

describe('@ngtools/webpack transformers', () => {
describe('remove-ivy-dev-calls', () => {
it('should allow removing only set class metadata with pure annotation', () => {
Expand Down Expand Up @@ -396,5 +436,35 @@ describe('@ngtools/webpack transformers', () => {

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});

it('should remove setClassMetadataAsync calls', () => {
const output = tags.stripIndent`
export class TestCmp {
}
TestCmp.ɵfac = function TestCmp_Factory(t) { return new (t || TestCmp)(); };
TestCmp.ɵcmp = i0.ɵɵdefineComponent({ type: TestCmp, selectors: [["test-cmp"]], standalone: true, features: [i0.ɵɵStandaloneFeature], decls: 3, vars: 0, template: function TestCmp_Template(rf, ctx) { }, encapsulation: 2 });
`;

const result = transform(inputAsync, (getTypeChecker) =>
removeIvyJitSupportCalls(true, false, getTypeChecker),
);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});

it('should remove arrow-function-based setClassMetadataAsync calls', () => {
const output = tags.stripIndent`
export class TestCmp {
}
TestCmp.ɵfac = function TestCmp_Factory(t) { return new (t || TestCmp)(); };
TestCmp.ɵcmp = i0.ɵɵdefineComponent({ type: TestCmp, selectors: [["test-cmp"]], standalone: true, features: [i0.ɵɵStandaloneFeature], decls: 3, vars: 0, template: function TestCmp_Template(rf, ctx) { }, encapsulation: 2 });
`;

const result = transform(inputAsyncArrowFn, (getTypeChecker) =>
removeIvyJitSupportCalls(true, false, getTypeChecker),
);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});
});
});

0 comments on commit 0139e82

Please sign in to comment.