Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES decorator generated by ESBuild cannot be consumed by Babel #3396

Closed
ocavue opened this issue Sep 19, 2023 · 2 comments
Closed

ES decorator generated by ESBuild cannot be consumed by Babel #3396

ocavue opened this issue Sep 19, 2023 · 2 comments

Comments

@ocavue
Copy link

ocavue commented Sep 19, 2023

Since currently ESBuild doesn't ES Stage-3 decorator yet, I'm trying to use @babel/plugin-proposal-decorators to process the decorator after ESBuild. However, I found a case that caused Babel to throw an error when processing the file from ESBuild. I'm not sure whether this is an ESBuild issue (i.e. ESBuild outputs invalid JavaScript file) or a Babel issue (i.e. Babel cannot handle some valid JavaScript syntax), but let me just post my case here so that we can discuss it.

I have created a minimal reproduction here: https://github.com/issueset/esbuild-19-3-decorator.

Step to reproduce:

git clone https://github.com/issueset/esbuild-19-3-decorator
cd esbuild-19-3-decorator
pnpm install 
cd packages/my-app
pnpm build

And you'll see the following error:

SyntaxError: /private/tmp/esbuild-19-3-decorator/packages/my-app/output-esbuild.cjs: Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'. (25:37)

  23 | module.exports = __toCommonJS(input_exports);
  24 | var import_my_lib = require("my-lib");
> 25 | @(0, import_my_lib.createMyDecorator)("PREFIX")

My source file (input.mjs) is shown below:

import { createMyDecorator, myDecorator } from "my-lib";

@createMyDecorator("PREFIX")
class MyClass1 {
  constructor() {}
}

@myDecorator
class MyClass2 {
  constructor() {}
}

ESBuild will transform it into:

var import_my_lib = require("my-lib");

@(0, import_my_lib.createMyDecorator)("PREFIX")
class MyClass1 {
  constructor() {
  }
}

@(import_my_lib.myDecorator)
class MyClass2 {
  constructor() {
  }
}

Babel doesn't like the @(0, import_my_lib.createMyDecorator) and throws the error. Also notice that Babel has no issue with another simpler line @(import_my_lib.myDecorator).

@ocavue
Copy link
Author

ocavue commented Sep 24, 2023

@evanw Thank you for fixing this!

@whawker
Copy link

whawker commented Sep 27, 2023

Thanks for looking into this! 😄 Just out of curiosity, do you know when the next esbuild release will be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants