-
Notifications
You must be signed in to change notification settings - Fork 508
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
Use regenerator instead of async-to-promises to transpile async functions #795
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/formium/tsdx/mza5yccgm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @hb-seb ! Very much appreciate the level of detail you've given in your description and commit messages as well 😄
Per #509 (comment) this is quite wanted given that async-to-promises
has been unmaintained for some time now and has correctness issues.
There are some issues with the code you've added though; specifically, libraries use TSDX as a dev dependency, so we can't ensure they have polyfills (like core-js
) added as dependencies. This also changes the current behavior of bundling as well. Whether or not libraries should bundle their polyfills is a bit of a debate in the community (personally I think no, that means less code re-use), but as TSDX isn't a dep we can't control that anyway.
Similarly, given the scale of this change and potential for issues like those, we should definitely add tests to this change prior to merging.
So I did some research in Babel to see if there had been any progress on the issue of library polyfills in recent time and there actually has been! It doesn't totally resolve all the issues with library polyfills but should help this move forward babel/babel#6629 was recently closed and plugins: [
// ...
['polyfill-regenerator', {
method: 'usage-pure',
targets: customOptions.targets
}]
] That should be functionally equivalent to |
This comment has been minimized.
This comment has been minimized.
Since this has been stale for several weeks now and is a high priority item (and the main item left for v0.14.0), I'll be taking over this PR now to get it over the finish line |
12696aa
to
2ae875c
Compare
Was going to add a test, but I see a test is failing here because I forgot I added an integration test in #627 for But this error is a bit cryptic 😕 ... and no files are output for me to inspect 😕 : Error when using sourcemap for reporting an error: Can't resolve original location of error.
Error when using sourcemap for reporting an error: Can't resolve original location of error.
Error: 'default' is not exported by ../node_modules/regenerator-runtime/runtime.js, imported by src/generator.ts
at /Users/antongilgur/Desktop/GitHub/oss/tsdx/stage-integration-build-withBabel/src/generator.ts:1:7
1: export function* testGenerator() {
^
2: return yield 'blah';
FAIL test/integration/tsdx-build-withBabel.test.ts (8.222s)
integration :: tsdx build :: .babelrc.js
✕ should add an import of regeneratorRuntime (5718ms) Default import is the issue? Can't tell if this is because of the external/bundling piece, a bug in EDIT: seems to error out even if I remove |
EDIT: or not... the error seems common: rollup/rollup-plugin-commonjs#361 and explained well here: wessberg/rollup-plugin-ts#78 (comment). So I guess the |
Ah, Got tests to work by using the same config from OP for |
2ae875c
to
41cf86e
Compare
Ah... well This shouldn't be too hard to add though fortunately. Will file an issue now and maybe PR tomorrow. EDIT: see babel/babel-polyfills#36 |
- now that generators are supported out-of-the-box via polyfill-regenerator, this is no longer an integration test - also remove @babel/plugin-transform-runtime because it's no longer used in tests and likely not needed much either, especially if babel-polyfills is recommended for other uses like core-js - it no longer ran either since the code was already transformed by polyfill-regenerator first
- cover the bases and ensure async is supported too, not just generators - use some syntax from a bug report that would cause async-to-promises to produce invalid JS and Rollup to subsequently error out
- specifically, this checks that `--target node`, which targets Node 10 right now, does not output regeneratorRuntime as generators have native support there (as does async/await) - remove `targets` from `polyfill-regenerator` config because it's actually not necessary for this specific polyfill - it only inserts regenerator if a `regeneratorRuntime` global is detected, which is only inserted by `@babel/plugin-transform-generator` if the `targets` require it - meaning that for this particular polyfill, `preset-env`'s `targets` are sufficient
af948c8
to
65ef336
Compare
Added a regression test for #869 Also babel/babel-polyfills#36 was responded to, so I've added a test against All tests pass. This is ready for review now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, fixing a whole swath of issues with this 🚀
@allcontributors please add @hb-seb for code |
I've put up a pull request to add @hb-seb! 🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found some test issues after merging, will have to refactor these in a follow-up PR
Reason for this change:
Ran into a variety of transpilation issues that surfaced after migrating existing code to tsdx, ranging from functions resolving with wrong values, loop iterations not being executed, and code after return statements being run.
Seeing that there are already other known issues with the (unmaintained looking) library, and the bugs are hard to isolate and behave differently in automated test environments, only replacing the transpilation library for a more robust solution would give me peace of mind.
Unfortunately doing so from the outside currently seems impossible, without replacing rollup plugins as a whole, so a fork seemed more reasonable. Maybe this PR could help to fix this issue at its core.
Changes:
babel-plugin-transform-async-to-promises
package and logic associated with filtering it out ofpreset-env
andexternals
async: false
option of the@babel/plugin-transform-regenerator
to transpile async functions to regeneratorsuseBuiltIns: 'usage'
flag to automatically require theregeneratorRuntime
corejs: 2
flag and the already transitively installedcore-js@2
dependency to satisfy a warning that would otherwise appear on every build, stating that core-js should be explicitly installed and the version added to the options when usinguseBuiltIns
to avoid problems when the babel default changes tocore-js@3
.