-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Support an async "preload" phase for source transformers #11081
Comments
Immediate idea is just to add optional However, @ahnpnl said in #9504 (comment)
I don't really like that. Maybe we should go with a class instead of a plain object? Then that class can stick whatever it wants as a property on its instances. Thoughts?
I think it makes sense regardless, if nothing else for the flexibility for people to download some remote stuff or something. |
This looks OK to me. My request is only that transformer should be able to access the data of "preload" phase. Currently I'm using this experimental workaround
|
Assuming that the class is only instantiated once (before actually running/compiling the tests), wouldn't it be the same as a normal variable outside the object? let state;
export const transformer = {
async initialize() {
state = { foo: 2 }
},
process(sourceText, sourcePath, options) {
return transform(sourceText, state);
}
} |
I actually don't like this usage because it might end up that all workers do the same thing. My idea is something which loads stuffs once before initializing transformers. That will benefit more for performance. |
That would only be possible if the result of |
How is this better than enabling async transformation? (#9889 was probably the latest on that topic?) The async initialization could be done first thing in |
Async transformation doesn't work with |
CJS is synchronous, so any async fork of the logic is a non-starter. Async transformation is orthogonal to allowing sync transformation to do some async preparation work. However, ESM is async. So the question is if any use case for async code stransforms is covered by ESM - in that case this issue is not worth addressing. That said, I think "async setup before sync transformation" is a valid use case.
I'd jump through 100 hoops to avoid hacking sync vs async. I get why Babel has gone that way, but I don't think it's necessarily worth it for others |
I like the transformer state idea. Transformer state will also help custom transformers to remove the caching logic. Currently |
Oh, yeah I didn't realize it was about transforming in a sync context where the code that does the import. I thought it was a long term thing for when we want to change to the Babel ESM version, but for CJS we would keep using the sync one |
@nicolo-ribaudo is your use case being able to do |
I think |
That diff also makes it trivial to call some async |
Released in next.4 |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🚀 Feature Proposal
Source transformers (such as
babel-jest
) should be allowed to have a one-time asynchronous initialization step, to load external resources or dependencies.This step must be executed before compiling, so that
require()
calls can still be intercepted and handled synchronously.Motivation
We are exploring publishing Babel as ESM starting from Babel 8. This means that would not be possible to synchronously
require("@babel/core")
anymore, butbabel-jest
would need to load it withawait import("@babel/core")
.Note that if Babel will be the only reason to implement this feature, we should wait until we are 100% sure that Babel will be released as ESM.
Example
I don't know how the source transformers API works 😅 If anyone can suggest an example, I can edit this description
Pitch
Why does this feature belong in the Jest core platform?
Transformers are already part of the core platform, and it's impossible to implement it externally.
Ref #9504 (comment), cc @ahnpnl
The text was updated successfully, but these errors were encountered: