Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): handle wrapping of class expressi…
Browse files Browse the repository at this point in the history
…ons 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.
  • Loading branch information
alan-agius4 committed Mar 20, 2024
1 parent c5cac49 commit 8a54875
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,7 @@ export default function (): PluginObj {
return;
}

if (
!classNode.id ||
!parentPath.isVariableDeclarator() ||
!types.isIdentifier(parentPath.node.id) ||
parentPath.node.id.name !== classNode.id.name
) {
if (!parentPath.isVariableDeclarator() || !types.isIdentifier(parentPath.node.id)) {
return;
}

Expand Down Expand Up @@ -402,16 +397,17 @@ export default function (): PluginObj {
[],
types.blockStatement([
types.variableDeclaration('let', [
types.variableDeclarator(types.cloneNode(classNode.id), classNode),
types.variableDeclarator(types.cloneNode(parentPath.node.id), classNode),
]),
...wrapStatementNodes,
types.returnStatement(types.cloneNode(classNode.id)),
types.returnStatement(types.cloneNode(parentPath.node.id)),
]),
);
const replacementInitializer = types.callExpression(
types.parenthesizedExpression(container),
[],
);

annotateAsPure(replacementInitializer);

// Add the wrapped class directly to the variable declaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,31 @@ describe('adjust-static-class-members Babel plugin', () => {
}),
);

it(
'wraps class with class decorators when wrapDecorators is true (esbuild output)',
testCase({
input: `
var ExampleClass = class {
method() {
}
};
__decorate([
SomeDecorator()
], ExampleClass.prototype, "method", null);
`,
expected: `
var ExampleClass = /*#__PURE__*/ (() => {
let ExampleClass = class {
method() {}
};
__decorate([SomeDecorator()], ExampleClass.prototype, "method", null);
return ExampleClass;
})();
`,
options: { wrapDecorators: true },
}),
);

it(
'wraps class with class decorators when wrapDecorators is true',
testCase({
Expand Down

0 comments on commit 8a54875

Please sign in to comment.