leonardfactory / babel-plugin-transform-typescript-metadata

Babel plugin to emit decorator metadata like typescript compiler

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add an option to disable the translation of parameter decorators

caghand opened this issue · comments

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!

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).

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? :)