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

decorators not support in js for prebuild #2349

Closed
3 tasks done
soulcm opened this issue Mar 3, 2021 · 10 comments
Closed
3 tasks done

decorators not support in js for prebuild #2349

soulcm opened this issue Mar 3, 2021 · 10 comments

Comments

@soulcm
Copy link

soulcm commented Mar 3, 2021

⚠️ IMPORTANT ⚠️ Please do not ignore this template. If you do, your issue will be closed immediately.

Describe the bug

运行 dev时,因为有预购建,导致esbuild无法识别js中的decorators,报如下错误
image

Reproduction

code 如下

// test.js
function sealed() {
  console.log('sealed');
}

@sealed
class Greeter {
  greeting;
  constructor(message) {
    this.greeting = message;
  }
  greet() {
    return "Hello, " + this.greeting;
  }
}

export default Greeter;

// main.tsx
import App from './App'
import Gretter from './test';

console.log(new Gretter())
  1. yarn dev

image

PS. yarn build时,加入了babel插件且无预购建步骤,因此不会报错

System Info

  • vite version: 2.0.3
  • Operating System: mac os
  • Node version: v12.14.1
  • Package manager (npm/yarn/pnpm) and version: yarn

Logs (Optional if provided reproduction)

  1. yarn dev

image

@soulcm soulcm changed the title decorators not support in js for pre-build decorators not support in js for prebuild(vite dev) Mar 3, 2021
@soulcm soulcm changed the title decorators not support in js for prebuild(vite dev) decorators not support in js for prebuild Mar 3, 2021
@dlaub3
Copy link

dlaub3 commented Mar 16, 2021

I believe this is because of how esbuild is leveraged.

const result = await transform(code, resolvedOptions)
. There doesn't seem to be any way to make esbuild aware of the option to handle decorators. https://github.com/evanw/esbuild/blob/master/CHANGELOG.md#0410

I'm not sure if esbuild will compile decorators in a JS project since I discovered this in a TS project.

@soulcm
Copy link
Author

soulcm commented Mar 17, 2021

I believe this is because of how esbuild is leveraged.

const result = await transform(code, resolvedOptions)

. There doesn't seem to be any way to make esbuild aware of the option to handle decorators. https://github.com/evanw/esbuild/blob/master/CHANGELOG.md#0410
I'm not sure if esbuild will compile decorators in a JS project since I discovered this in a TS project.

Yes,esbuild dose not support decorator of javascript, it just support typescript. And vite when prebuild in scanImports function will use esbuild direct.
Now, I have rename my project files use ts instead of js and works fine.

But I hope vite can have a method to solve this issue, thanks

@dlaub3
Copy link

dlaub3 commented Mar 17, 2021

Now, I have rename my project files use ts instead of js and works fine.

Do you have Vite working with decorators or only esbuild?

@liho00
Copy link

liho00 commented Jun 22, 2021

having this issue as well. Anyone has the solution for it?

@haoqunjiang
Copy link
Member

Closing as it's an esbuild issue evanw/esbuild#1392

Though, in my opinion, such usage should be discouraged. Decorators in JS is still a stage-2 proposal, so it's still subject to change in the future. Its current specification is drastically different from the TypeScript implementation, too, which might surprise many users.

@PRossetti
Copy link

having this issue as well. Anyone has the solution for it?

Having the same issue here. Trying to use decorators with js (not typescript) because I'm working in a project that uses mobx to state management and mobx uses decorators.

@soulcm
Copy link
Author

soulcm commented Jul 8, 2021

Having the same issue here. Trying to use decorators with js (not typescript) because I'm working in a project that uses mobx to state management and mobx uses decorators.

yes, my project use mobx too. I have renamed the file from jsx to tsx and work fine.

@Shinigami92
Copy link
Member

yes, my project use mobx too. I have renamed the file from jsx to tsx and work fine.

I would count this as a very smart workaround 🙂

@PRossetti
Copy link

Having the same issue here. Trying to use decorators with js (not typescript) because I'm working in a project that uses mobx to state management and mobx uses decorators.

yes, my project use mobx too. I have renamed the file from jsx to tsx and work fine.

Didn't you need any other change in vite.config.js? did you had to add a tsconfig.json file?

@soulcm
Copy link
Author

soulcm commented Jul 12, 2021

Didn't you need any other change in vite.config.js? did you had to add a tsconfig.json file?

Yes. Just use the official doc for vite.config.js.
tsconfig.json also need to add for typescript work fine.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants