Skip to content

Commit

Permalink
fix(@ngtools/webpack): remove setClassDebugInfo calls
Browse files Browse the repository at this point in the history
Angular v17 adds another dev-mode-only function that needs to be removed called `ɵsetClassDebugInfo`. These changes update the Webpack plugin to account for it.
  • Loading branch information
crisbeto authored and alan-agius4 committed Oct 31, 2023
1 parent 2602679 commit 7a41e8f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
2 changes: 2 additions & 0 deletions goldens/public-api/ngtools/webpack/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface AngularWebpackPluginOptions {
// (undocumented)
emitNgModuleScope: boolean;
// (undocumented)
emitSetClassDebugInfo?: boolean;
// (undocumented)
fileReplacements: Record<string, string>;
// (undocumented)
inlineStyleFileExtension?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/ngtools/webpack/src/ivy/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface AngularWebpackPluginOptions {
directTemplateLoading: boolean;
emitClassMetadata: boolean;
emitNgModuleScope: boolean;
emitSetClassDebugInfo?: boolean;
jitMode: boolean;
inlineStyleFileExtension?: string;
}
Expand Down
16 changes: 13 additions & 3 deletions packages/ngtools/webpack/src/ivy/transformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import { replaceResources } from '../transformers/replace_resources';

export function createAotTransformers(
builder: ts.BuilderProgram,
options: { emitClassMetadata?: boolean; emitNgModuleScope?: boolean },
options: {
emitClassMetadata?: boolean;
emitNgModuleScope?: boolean;
emitSetClassDebugInfo?: boolean;
},
imageDomains: Set<string>,
): ts.CustomTransformers {
const getTypeChecker = () => builder.getProgram().getTypeChecker();
Expand All @@ -25,10 +29,16 @@ export function createAotTransformers(

const removeClassMetadata = !options.emitClassMetadata;
const removeNgModuleScope = !options.emitNgModuleScope;
if (removeClassMetadata || removeNgModuleScope) {
const removeSetClassDebugInfo = !options.emitSetClassDebugInfo;
if (removeClassMetadata || removeNgModuleScope || removeSetClassDebugInfo) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
transformers.before!.push(
removeIvyJitSupportCalls(removeClassMetadata, removeNgModuleScope, getTypeChecker),
removeIvyJitSupportCalls(
removeClassMetadata,
removeNgModuleScope,
removeSetClassDebugInfo,
getTypeChecker,
),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { elideImports } from './elide_imports';
export function removeIvyJitSupportCalls(
classMetadata: boolean,
ngModuleScope: boolean,
debugInfo: boolean,
getTypeChecker: () => ts.TypeChecker,
): ts.TransformerFactory<ts.SourceFile> {
return (context: ts.TransformationContext) => {
Expand Down Expand Up @@ -43,6 +44,16 @@ export function removeIvyJitSupportCalls(
return undefined;
}
}

if (
debugInfo &&
ts.isBinaryExpression(innerExpression) &&
isIvyPrivateCallExpression(innerExpression.right, 'ɵsetClassDebugInfo')
) {
removedNodes.push(innerExpression);

return undefined;
}
}

return ts.visitEachChild(node, visitNode, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ const inputAsyncArrowFn = tags.stripIndent`
}); })();
`;

const inputDebugInfo = tags.stripIndent`
import { Component } from '@angular/core';
import * as i0 from "@angular/core";
export class TestCmp {
}
TestCmp.ɵfac = function TestCmp_Factory(t) { return new (t || TestCmp)(); };
TestCmp.ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: TestCmp, selectors: [["test-cmp"]], decls: 0, vars: 0, template: function TestCmp_Template(rf, ctx) { }, encapsulation: 2 });
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(TestCmp, { className: "TestCmp" }); })();
`;

describe('@ngtools/webpack transformers', () => {
describe('remove-ivy-dev-calls', () => {
it('should allow removing only set class metadata with pure annotation', () => {
Expand All @@ -194,7 +204,7 @@ describe('@ngtools/webpack transformers', () => {
`;

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

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
Expand All @@ -215,7 +225,7 @@ describe('@ngtools/webpack transformers', () => {
`;

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

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
Expand Down Expand Up @@ -248,7 +258,7 @@ describe('@ngtools/webpack transformers', () => {
`;

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

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
Expand Down Expand Up @@ -281,7 +291,7 @@ describe('@ngtools/webpack transformers', () => {
`;

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

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
Expand All @@ -299,7 +309,7 @@ describe('@ngtools/webpack transformers', () => {
`;

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

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
Expand All @@ -317,23 +327,23 @@ describe('@ngtools/webpack transformers', () => {
`;

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

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

it('should allow removing neither set class metadata nor ng module scope with pure annotation', () => {
const result = transform(input, (getTypeChecker) =>
removeIvyJitSupportCalls(false, false, getTypeChecker),
removeIvyJitSupportCalls(false, false, false, getTypeChecker),
);

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

it('should allow removing neither set class metadata nor ng module scope', () => {
const result = transform(inputNoPure, (getTypeChecker) =>
removeIvyJitSupportCalls(false, false, getTypeChecker),
removeIvyJitSupportCalls(false, false, false, getTypeChecker),
);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${inputNoPure}`);
Expand Down Expand Up @@ -364,7 +374,7 @@ describe('@ngtools/webpack transformers', () => {
`;

const result = transform(imports + input, (getTypeChecker) =>
removeIvyJitSupportCalls(true, true, getTypeChecker),
removeIvyJitSupportCalls(true, true, false, getTypeChecker),
);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
Expand Down Expand Up @@ -395,7 +405,7 @@ describe('@ngtools/webpack transformers', () => {
`;

const result = transform(imports + inputNoPure, (getTypeChecker) =>
removeIvyJitSupportCalls(true, true, getTypeChecker),
removeIvyJitSupportCalls(true, true, false, getTypeChecker),
);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
Expand All @@ -413,7 +423,7 @@ describe('@ngtools/webpack transformers', () => {
`;

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

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
Expand All @@ -431,7 +441,7 @@ describe('@ngtools/webpack transformers', () => {
`;

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

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
Expand All @@ -446,7 +456,7 @@ describe('@ngtools/webpack transformers', () => {
`;

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

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
Expand All @@ -461,7 +471,22 @@ describe('@ngtools/webpack transformers', () => {
`;

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

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

it('should remove setClassDebugInfo calls', () => {
const output = tags.stripIndent`
import * as i0 from "@angular/core";
export class TestCmp { }
TestCmp.ɵfac = function TestCmp_Factory(t) { return new (t || TestCmp)(); };
TestCmp.ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: TestCmp, selectors: [["test-cmp"]], decls: 0, vars: 0, template: function TestCmp_Template(rf, ctx) { }, encapsulation: 2 });
`;

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

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

0 comments on commit 7a41e8f

Please sign in to comment.