Skip to content

Commit 8a54875

Browse files
committed
fix(@angular-devkit/build-angular): handle wrapping of class expressions emitted by esbuild
This update modifies the 'adjust-static-class-members' Babel plugin to accommodate the wrapping of class expressions produced by esbuild. This adjustment becomes necessary as ng-packagr currently utilizes esbuild for bundling FESM.
1 parent c5cac49 commit 8a54875

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

packages/angular_devkit/build_angular/src/tools/babel/plugins/adjust-static-class-members.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,7 @@ export default function (): PluginObj {
365365
return;
366366
}
367367

368-
if (
369-
!classNode.id ||
370-
!parentPath.isVariableDeclarator() ||
371-
!types.isIdentifier(parentPath.node.id) ||
372-
parentPath.node.id.name !== classNode.id.name
373-
) {
368+
if (!parentPath.isVariableDeclarator() || !types.isIdentifier(parentPath.node.id)) {
374369
return;
375370
}
376371

@@ -402,16 +397,17 @@ export default function (): PluginObj {
402397
[],
403398
types.blockStatement([
404399
types.variableDeclaration('let', [
405-
types.variableDeclarator(types.cloneNode(classNode.id), classNode),
400+
types.variableDeclarator(types.cloneNode(parentPath.node.id), classNode),
406401
]),
407402
...wrapStatementNodes,
408-
types.returnStatement(types.cloneNode(classNode.id)),
403+
types.returnStatement(types.cloneNode(parentPath.node.id)),
409404
]),
410405
);
411406
const replacementInitializer = types.callExpression(
412407
types.parenthesizedExpression(container),
413408
[],
414409
);
410+
415411
annotateAsPure(replacementInitializer);
416412

417413
// Add the wrapped class directly to the variable declaration

packages/angular_devkit/build_angular/src/tools/babel/plugins/adjust-static-class-members_spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,31 @@ describe('adjust-static-class-members Babel plugin', () => {
595595
}),
596596
);
597597

598+
it(
599+
'wraps class with class decorators when wrapDecorators is true (esbuild output)',
600+
testCase({
601+
input: `
602+
var ExampleClass = class {
603+
method() {
604+
}
605+
};
606+
__decorate([
607+
SomeDecorator()
608+
], ExampleClass.prototype, "method", null);
609+
`,
610+
expected: `
611+
var ExampleClass = /*#__PURE__*/ (() => {
612+
let ExampleClass = class {
613+
method() {}
614+
};
615+
__decorate([SomeDecorator()], ExampleClass.prototype, "method", null);
616+
return ExampleClass;
617+
})();
618+
`,
619+
options: { wrapDecorators: true },
620+
}),
621+
);
622+
598623
it(
599624
'wraps class with class decorators when wrapDecorators is true',
600625
testCase({

0 commit comments

Comments
 (0)