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

Add an option to disable the translation of parameter decorators #69

Open
caghand opened this issue Oct 18, 2022 · 2 comments
Open

Add an option to disable the translation of parameter decorators #69

caghand opened this issue Oct 18, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@caghand
Copy link

caghand commented Oct 18, 2022

This particular translation causes problems with esbuild and esbuild-based frameworks like vite.

esbuild does not support TypeScript's emitDecoratorMetadata functionality, so we must use this brilliant Babel plugin to inject the missing metadata before running esbuild.
On the other hand, esbuild can handle TypeScript's parameter decorators perfectly well. Internally, esbuild does the same kind of translation as this Babel plugin + @babel/plugin-proposal-decorators (in legacy mode), but implemented differently.
But if this Babel plugin translates parameter decorators to function expressions, and the result is fed to esbuild, then esbuild errors out.
Therefore, could you please add an option to disable the translation of parameter decorators?

Thanks!

@leonardfactory
Copy link
Owner

leonardfactory commented Oct 24, 2022

Seems an important thing to have, I'll look into this asap (this period is extremely busy but I'll try to find a moment in the weekends).

@leonardfactory leonardfactory added the enhancement New feature or request label Oct 24, 2022
@caghand
Copy link
Author

caghand commented Nov 12, 2022

Thanks @leonardfactory! But, now that I think about it, maybe a better solution is to translate parameter decorators in a way that does not confuse esbuild, by avoiding function expressions.
For example, let's say I start with this:

class Some {
  constructor(@Inject() private: SomeService);
}

At the moment, you are translating it to this:

@(function (target, key) {
  return Inject()(target, undefined, 0);
})
class Some {
  constructor(private: SomeService);
}

But instead, you could translate it to this:

function _generateParameterDecorator(index) {
  return function (target, key) {
    return Inject()(target, undefined, index);
  }
}

@_generateParameterDecorator(0)
class Some {
  constructor(private: SomeService);
}

Then, you won't need to offer any options for configuring this plugin.
And also, derivative projects like https://github.com/intellild/babel-plugin-transform-typescript-metadata won't need to implement their own configuration mechanism for turning off this specific translation.
What do you think? :)

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

No branches or pull requests

2 participants