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
Feature request: Decorators support #104
Comments
https://github.com/vuejs/vue-cli/tree/dev/packages/@vue/babel-preset-app#readme Hope can support Decorators, then we can try it in vue project. |
The decorator spec unfortunately still seems like it's a long way off, both based on low activity and based on how many things it looks like they still have to figure out. The proposal has drifted pretty far from what the entire rest of the JavaScript ecosystem has been doing for many years. It also appears to be incompatible with a lot of the JavaScript ecosystem (e.g. no CommonJS support). So I'm not planning on supporting decorators based on the spec, at least not any time soon. Separate from that, decorators in TypeScript have been around for a while behind the |
nest.js is a great web framework but it's extremely slow during compilation. There are over 20 modules in my project. :( |
An initial implementation of TypeScript decorators has been released in version 0.4.10. You can read more about this in the release notes. Please let me know if you encounter any issues. |
I have a JS project, not TS. When compiling decorators, it throws errors:
|
Decorators are not a JS feature, so esbuild doesn't support them in JS. Nothing in the JS specification mentions decorators: https://262.ecma-international.org/12.0/. But they are a TS feature, so esbuild supports them in TS. You can tell esbuild to compile your JS as TS if you like: |
Thanks. I am now moving my project to TS and it works very well. |
Looks like decorators but without the metadata has moved to stage 3 and i am sure typescript will implement this in a while, what are your plans for javascript decorators support, @evanw |
My plans are to implement it after it has shipped in a browser, in node, or in TypeScript. This is the same thing that I do for all other syntax features. |
TypeScript implemented this on 5.0 beta https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#decorators |
The issues linked by @evanw seem to be fixed in the meantime. Is there anything else holding back an implementation? |
@evanw Any updates on this? TS has been moving heavily away from legacy decorators in favor of the standard. I've been using esbuild via |
Our saviour has been working hard in the past 2 weeks. Let's give him some space. |
@cayter Amazing, thanks for the update! It's a fine line between highlighting the relative importance of an issue (helpful) and adding undue stress on an issue the maintainer has already prioritized or is working on (unhelpful). I'm very sorry if my comment came across as the latter. @evanw I deeply appreciate your work and just seeing this is actively being worked on alleviates my primary concern. |
@ssalbdivad While waiting on this to be finished I'm successfully using this vite plugin as workaround to precompile typescript: https://github.com/herberttn/vite-plugin-typescript-transform |
I'm still making progress on this. However, progress is slow because decorators are very complicated. Some things I did recently:
|
Ok version 0.21.0 is out and now supports the JavaScript decorators proposal. Please try it out and let me know what you think. @justinfagnani Code size definitely isn't optimal but it shouldn't be that bad I think. I made a trade-off that generates accessor methods at run-time instead of compile-time. It would be interesting to see where esbuild's implementation ends up in your size comparison (is it public somewhere perhaps?). |
This is crazy. I thought this would take few more months to ship. Thanks a lot! Note that I couldn't get the example in the release working, have to do it like this: function log(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log(`before ${propertyKey}`);
const result = originalMethod.apply(this, args);
console.log(`after ${propertyKey}`);
return result;
};
}
class Foo {
@log
static foo() {
console.log("in foo");
}
}
Foo.foo(); |
Here's an example of it working: link. That logs this when run:
|
Ah I didn't specify target. Thanks man! |
The grind was real. Thank you so much @evanw! I will test this thoroughly with our internal ORM. |
What do people think? Does it work with existing code that uses the JavaScript decorators proposal? Any feedback? |
EDIT: Just found out you released a new version and this seems to have fixed the issue I had. So from me absolute positiv feedback, since it's also really fast! Previous comment: I tried it in our product but got an error with MobX and thus had to return to the transformer workaround. I then tried to make a small reproduction by using your online esbuild tranformer and putting it in a jsfiddle, but there it runs without problems. I'll just post the error and what I found out so far where it goes wrong, but I guess it's not easy to debug. MobX just throws this: This was my reproduction attempt:
Just to clarify: This may sound like a rant, but is not meant as such. I think you are doing a great job and I assume the cause of the problem could be in our code, but it's hard to find for me and maybe solving this could provide valuable feedback. |
Wow! I was literally trying to write a decorator yesterday to add logging to datadog to our common logger utility. Can't wait to try this out 👍 thank you so much! |
Works great! I was able to migrate my decorators from the legacy Typescript implementation to the TC39 proposal without problems. |
@evanw I tried the Lit decorators, but it looks like metadata isn't implemented yet, which is a blocker for us. I'll be able to test again when metadata support is implemented. |
Initial support for decorator metadata has been released in version 0.21.3. |
Evan you rock! |
Awesome @evanw! I ran all of Lit's decorator tests after an esbuild compile and they all pass! I'll check those tests in so if you ever want to check changes against them you can do: git clone https://github.com/lit/lit.git
cd lit
npm ci
npm link esbuild
npm run test:dev -w @lit/reactive-element |
Any plan to support decorators?
The text was updated successfully, but these errors were encountered: