-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
feat(ssr): tolerate circular imports #3950
Conversation
@aleclarson i've brought my improvement to your branch: aleclarson#2 in can still be improved but it works on the project i'm working at the moment |
@raythurnevoid Thanks for the test case. I've solved it in a better way, see here: 0333312 |
I can confirm it is working on the project i'm working too |
22b88e7
to
840d020
Compare
@aleclarson would you resolve the conflicts? Is this one ready to be discussed with the team? |
840d020
to
317bbcf
Compare
Should @brillout have a look? |
@Shinigami92 LGTM (Although my review is superficial and I didn't dig that much) @aleclarson Neat neat - one thing though: how about we create a new playground instead of populating @LydiaF Hope this makes you re-consider Vite hehe. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I agree. The tests were contributed by others. If someone can create the new playground, it would be appreciated. 👍 |
If dependencies are not loaded one at a time, the load order depends on the depth of the dependency tree plus the transformation time per module. When forced to load serially, the load orders depends on the order of import declarations, which is how NodeJS works too.
SSR modules are now wrapped in an async function, so they can await their imports. Also, exports are now always defined in place, instead of being appended to the bottom of the module. This allows better parity with how ESM handles circular imports.
c0f4983
to
49aee2c
Compare
Co-authored-by: Greg Fairbanks <[email protected]> Co-authored-by: Ray Thurne Void <[email protected]>
It seems like there is still possible issues with circular imports and SSR that can lead to intermittent hanging: #11468 |
Description
This PR makes SSR imports work like Node.js in the following way:
Modules that depend on each other will never receive an undefined exports. Instead, the module that is first to import the other module will wait for the other module to be instantiated. When the other module imports the first module, it receives the incomplete exports object, which will be completed when the first module is done being instantiated.
To accomplish this behavior, we need to load SSR dependencies one at a time. This ensures a predictable import order. Before this PR, the import order is non-deterministic, as it depends on the depth of the dependency tree plus the transformation time per module.
This PR also avoids infinite hangs caused by a dependency having its
ssrLoadModule
promise awaited without first checking if one of its dependencies is within theurlStack
.As of 0333312, this PR also allows the "other module" to access the partial exports of the "first module". For example, if
export { foo } from "./foo"
comes beforeexport { bar } from "./bar"
in the./index.js
module, then./bar.js
will still be able toimport { foo } from "./index"
and use it before./index.js
has finished being instantiated.Todo
playground/ssr-react
changes into a new playgroundssr-circular-import
Additional context
Fixes #2258
Fixes #2491
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).