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

Customize @babel/plugin-transform-typescript options #33

Closed
IlyaSemenov opened this issue May 28, 2021 · 3 comments
Closed

Customize @babel/plugin-transform-typescript options #33

IlyaSemenov opened this issue May 28, 2021 · 3 comments

Comments

@IlyaSemenov
Copy link

I would like to be able to pass custom options to @babel/plugin-transform-typescript

In particular, for interoperability reasons, I need to support declare x: string class members, and for that I need to use this:

['@babel/transform-typescript', { allowDeclareFields: true }]

However, it doesn't seem to be possible to achieve with opts.babel. It's not even possible to disable opts.ts and re-apply the same plugins manually because you only push custom plugins and one of them must be unshifted:

jiti/src/babel.ts

Lines 21 to 26 in f25960a

if (opts.ts) {
_opts.plugins!.push(require('@babel/plugin-transform-typescript'))
// `unshift` because this plugin must come before `@babel/plugin-syntax-class-properties`
_opts.plugins!.unshift([require('@babel/plugin-proposal-decorators'), { legacy: true }])
_opts.plugins!.push(require('babel-plugin-parameter-decorator'))
}

jiti/src/babel.ts

Lines 33 to 35 in f25960a

if (opts.babel && Array.isArray(opts.babel.plugins)) {
_opts.plugins?.push(...opts.babel.plugins)
}

Basically I have to copy/paste the entire babel.ts in order to setup typescript options.

Suggestions

  1. opts.ts to support not only boolean but also object with @babel/transform-typescript options
  2. opts.babel to support not only subset of options but also a config customization callback which will be called directly before transformSync
@pi0
Copy link
Member

pi0 commented May 28, 2021

Thanks for describing issue. allowDeclareFields is enabled in the 1.10.0 since babel 8 will enable it by default too (for nuxt you can recreate lockfile or use yarn upgrade nuxt).

  • Supporting plugin options can be a good idea but I need more specific reasons to support them.
  • Supporting babelrc and callback function per transform is dangerous since we cannot predict cache key anymore

Feel free opening more issues if anything else was missing.

@IlyaSemenov
Copy link
Author

@pi0 It now crashes with:

   ╭────────────────────────────────────────────────────────────────────────────────────────────────╮
   │                                                                                                │
   │   ✖ Nuxt Fatal Error                                                                           │
   │                                                                                                │
   │   Error: UNKNOWN_PLUGIN_PROPERTY: [BABEL] .allowDeclareFields is not a valid Plugin property   │
   │   /Users/semenov/work/eg11/eg11-web/src/frontend/nuxt.config.ts:0:0                            │
   │                                                                                                │
   ╰────────────────────────────────────────────────────────────────────────────────────────────────╯

See my comment in 914499c

@pi0
Copy link
Member

pi0 commented May 28, 2021

Fixed in 0.10.1

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

No branches or pull requests

2 participants