Skip to content

Commit 89f21ac

Browse files
crisbetodgp1130
authored andcommitted
fix(@ngtools/webpack): remove setClassMetadataAsync calls
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.
1 parent 188a00f commit 89f21ac

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

packages/ngtools/webpack/src/transformers/remove-ivy-jit-support-calls.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ export function removeIvyJitSupportCalls(
3434
const expression = ts.isBinaryExpression(innerExpression)
3535
? innerExpression.right
3636
: innerExpression;
37-
if (isIvyPrivateCallExpression(expression, 'ɵsetClassMetadata')) {
37+
if (
38+
isIvyPrivateCallExpression(expression, 'ɵsetClassMetadata') ||
39+
isIvyPrivateCallExpression(expression, 'ɵsetClassMetadataAsync')
40+
) {
3841
removedNodes.push(innerExpression);
3942

4043
return undefined;
@@ -120,7 +123,7 @@ function isIvyPrivateCallExpression(expression: ts.Expression, name: string) {
120123
return false;
121124
}
122125

123-
if (propAccExpr.name.text != name) {
126+
if (propAccExpr.name.text !== name) {
124127
return false;
125128
}
126129

packages/ngtools/webpack/src/transformers/remove-ivy-jit-support-calls_spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,46 @@ const inputArrowFnWithImplicitReturn = tags.stripIndent`
137137
}], null, null))();
138138
`;
139139

140+
const inputAsync = tags.stripIndent`
141+
export class TestCmp {
142+
}
143+
TestCmp.ɵfac = function TestCmp_Factory(t) { return new (t || TestCmp)(); };
144+
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 });
145+
(function () {
146+
(typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵsetClassMetadataAsync(TestCmp,
147+
function () { return [import("./cmp-a").then(function (m) { return m.CmpA; })]; },
148+
function (CmpA) { i0.ɵsetClassMetadata(TestCmp, [{
149+
type: Component,
150+
args: [{
151+
selector: 'test-cmp',
152+
standalone: true,
153+
imports: [CmpA],
154+
template: '{#defer}<cmp-a />{/defer}',
155+
}]
156+
}], null, null); }); })();
157+
`;
158+
159+
const inputAsyncArrowFn = tags.stripIndent`
160+
export class TestCmp {
161+
}
162+
TestCmp.ɵfac = function TestCmp_Factory(t) { return new (t || TestCmp)(); };
163+
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 });
164+
(() => {
165+
(typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵsetClassMetadataAsync(TestCmp,
166+
() => [import("./cmp-a").then((m) => m.CmpA)],
167+
(CmpA) => {
168+
i0.ɵsetClassMetadata(TestCmp, [{
169+
type: Component,
170+
args: [{
171+
selector: 'test-cmp',
172+
standalone: true,
173+
imports: [CmpA],
174+
template: '{#defer}<cmp-a />{/defer}',
175+
}]
176+
}], null, null);
177+
}); })();
178+
`;
179+
140180
describe('@ngtools/webpack transformers', () => {
141181
describe('remove-ivy-dev-calls', () => {
142182
it('should allow removing only set class metadata with pure annotation', () => {
@@ -396,5 +436,35 @@ describe('@ngtools/webpack transformers', () => {
396436

397437
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
398438
});
439+
440+
it('should remove setClassMetadataAsync calls', () => {
441+
const output = tags.stripIndent`
442+
export class TestCmp {
443+
}
444+
TestCmp.ɵfac = function TestCmp_Factory(t) { return new (t || TestCmp)(); };
445+
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 });
446+
`;
447+
448+
const result = transform(inputAsync, (getTypeChecker) =>
449+
removeIvyJitSupportCalls(true, false, getTypeChecker),
450+
);
451+
452+
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
453+
});
454+
455+
it('should remove arrow-function-based setClassMetadataAsync calls', () => {
456+
const output = tags.stripIndent`
457+
export class TestCmp {
458+
}
459+
TestCmp.ɵfac = function TestCmp_Factory(t) { return new (t || TestCmp)(); };
460+
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 });
461+
`;
462+
463+
const result = transform(inputAsyncArrowFn, (getTypeChecker) =>
464+
removeIvyJitSupportCalls(true, false, getTypeChecker),
465+
);
466+
467+
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
468+
});
399469
});
400470
});

0 commit comments

Comments
 (0)