Babel Plugin Parameter Decorator
Function parameter decorator transform plugin for babel v7, just like typescript parameter decorator
{ const fn = descriptorvalue; descriptor { const metadata = `meta_`; targetmetadata; return fn; }; return descriptor;} { return { const metadata = `meta_`; targetmetadata = ...targetmetadata || index: parameterIndex key };} { thisgreeting = message; } @validate return "Hello " + name + ", " + thisgreeting;
NOTE:
This package depends on @babel/plugin-proposal-decorators
.
Installation & Usage
npm install @babel/plugin-proposal-decorators babel-plugin-parameter-decorator -D
And the .babelrc
looks like:
{
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
"babel-plugin-parameter-decorator"
]
}
By default, @babel/preset-typescript
will remove imports only referenced in Decorators.
Since this is prone to break Decorators, make sure disable it by setting onlyRemoveTypeImports
to true:
{
...
"presets": [
[
"@babel/preset-typescript",
{ "onlyRemoveTypeImports": true }
]
]
...
}
Additional
If you'd like to compile typescript files by babel, the file extensions .ts
or .tsx
expected, or we will get runtime error!
🎊 Hopefully this plugin would get along with typescript private/public
keywords in constructor
. For example,
And your .babelrc
looks like:
{
"presets": [
"@babel/preset-env",
"@babel/preset-typescript"
],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose" : true }],
"babel-plugin-parameter-decorator"
]
}