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

[babel 8] Cleanup @babel/plugin-transform-runtime options #16310

Closed
nicolo-ribaudo opened this issue Feb 27, 2024 · 9 comments · Fixed by #16311
Closed

[babel 8] Cleanup @babel/plugin-transform-runtime options #16310

nicolo-ribaudo opened this issue Feb 27, 2024 · 9 comments · Fixed by #16311
Assignees
Labels
Milestone

Comments

@nicolo-ribaudo
Copy link
Member

Basically, make it only to inject imports to helpers

  • Remove the corejs option, and recommend babel-plugin-polyfill-corejs3 directly. [note: when the polyfill provider is enabled, we will still inject imports to @babel/runtime-corejs3 instead of @babel/runtime]
  • Remove the helpers option (and make it true)
  • Remove the runtime option (and make it false), since it's mostly not needed anymore now that we directly inject imports to @babel/runtime/helpers/regeneratorRuntime. For the edge case mentioned in [Bug]: Generator functions cannot be converted using only @babel/plugin-transform-runtime #16260 (comment), people can still use babel-plugin-polyfill-regenerator.

Then, clean up the docs at https://next.babeljs.io/docs/babel-plugin-transform-runtime/

@nyngwang
Copy link

nyngwang commented Mar 1, 2024

@nicolo-ribaudo

There is a problem regarding #16241, and I believe this is something that needs to be considered during your cleanup of @babel/plugin-transform-runtime. To disable the usage of any pure polyfill provided by @babel/plugin-transform-runtime, I do:

  1. I use @babel/plugin-transform-runtime with moduleName: '@babel/runtime'.
  2. I also use babel-plugin-polyfill-corejs3 with method: 'usage-global'.

And now I got the same error saying I'm using @babel/runtime-corejs3/regenerator (and I believe this is probably caused by the default regenerator: true option):

Error: Failed to resolve "@babel/runtime-corejs3/regenerator" relative to "/Users/ningwang/GitHub/Frontend"

So it seems that the option default regenerator option ignores moduleName. A naive way like below does NOT work:

'@babel/plugin-transform-runtime', { moduleName: '@babel/runtime', regenerator: false },
'babel-plugin-polyfill-corejs3', { method: 'usage-global' },
'babel-plugin-polyfill-regenerator', { method: 'usage-global' }

This doesn't work in that @babel/plugin-transform-runtime does not reference @babel/runtime/regenerator in the first place. So the setting of babel-plugin-polyfill-regenerator has no effect. To make it work, I still need this trick:

'@babel/plugin-transform-runtime', { moduleName: '@babel/runtime' },
'babel-plugin-polyfill-corejs3', {
  method: 'usage-global',
  ['#__secret_key__@babel/preset-env__compatibility']: {
    noRuntimeName: true,
  },
},

After all, I feel it is very counter-intuitive when I set moduleName: '@babel/runtime' and still see any usage of @babel/runtime-corejs3 in the error message.

@nicolo-ribaudo
Copy link
Member Author

Oh this is a bug in the new option. Feel free to open a separate PR, otherwise I'll take a look at it probably on Monday.

@nyngwang
Copy link

nyngwang commented Mar 1, 2024

I can wait since I haven't done it before and you probably know how to do it more concisely.

On the other hand, could you please make it show some warning when two different runtimes, e.g. in my case @babel/runtime, @babel/runtime-corejs3, are used in the code? It will be really helpful in the future debugging from the user's perspective. Thank you!

@nicolo-ribaudo
Copy link
Member Author

nicolo-ribaudo commented Mar 5, 2024

@nyngwang The import to @babel/helpers/regenerator is injected by babel-plugin-polyfill-regenerator -- do you think adding a moduleName option to it would be an acceptable solution?

Note that if you specify the version option of @babel/plugin-transform-runtime to a recent version of @babel/helpers (or if you use Babel 8 alpha) you don't need babel-plugin-polyfill-regenerator anymore to compile generators.

@nyngwang
Copy link

nyngwang commented Mar 5, 2024

To @nicolo-ribaudo:

The import to @babel/helpers/regenerator is injected by babel-plugin-polyfill-regenerator -- do you think adding a moduleName option to it would be an acceptable solution?

If the import to @babel/helpers/regenerator is injected by babel-plugin-polyfill-regenerator, then adding a moduleName option sounds like a good idea.

Note that if you specify the version option of @babel/plugin-transform-runtime to a recent version of @babel/helpers (or if you use Babel 8 alpha) you don't need babel-plugin-polyfill-regenerator anymore to compile generators.

I have a question: Am I correct that the injection of @babel/helpers/regenerator is still handled by @babel/plugin-transform-runtime but that the regenerator option will be removed? See the comment right below.

On the other hand, I wonder about the difference between injecting regeneratorRuntime by @babel/plugin-transform-runtime with the newer version and by babel-plugin-polyfill-regenerator.

@nicolo-ribaudo
Copy link
Member Author

nicolo-ribaudo commented Mar 5, 2024

@babel/runtime/regenerator itself will not exist anymore. It's now (when setting the version, or using Babel 8) a normal helper, like classCallCheck or asyncToGenerator. The imports are injected by @babel/plugin-transform-runtime like for any other helper (@babel/helpers/runtime/helpers/classCallCheck, @babel/runtime/helpers/asyncToGenerator, @babel/runtime/helpers/regeneratorRuntime), without any specific option.

@nyngwang
Copy link

nyngwang commented Mar 5, 2024

So the same thing is achieved, but it's just that nothing is a special case, right?

@nicolo-ribaudo
Copy link
Member Author

Yes :)

@nyngwang
Copy link

nyngwang commented Mar 5, 2024

I like this :) Sounds like a really good decision for me! Great that Babel 8 (or I should say now) has it!

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

Successfully merging a pull request may close this issue.

2 participants