-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
feat(ssr): resolve externalized packages with resolve.externalConditions and add module-sync to default external condition
#20409
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
Changes from 3 commits
3706eee
87d34b9
1e2fc8f
26b1b82
6d38029
7ad1b12
e7c3d47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export default 'module-sync' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "name": "@vitejs/test-module-sync", | ||
| "type": "module", | ||
| "private": true, | ||
| "version": "0.0.0", | ||
| "exports": { | ||
| ".": { | ||
| "module-sync": "./index.js" | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -26,6 +26,12 @@ export default defineConfig({ | |||
| }, | ||||
| }, | ||||
| }, | ||||
| ssr: { | ||||
| resolve: { | ||||
| // FIXME: this is a bug in Vitest, resolving externalized modules should use externalConditions | ||||
| conditions: ['node', 'module-sync'], | ||||
| }, | ||||
| }, | ||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hi-ogawa It seems Vitest uses
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, currently Vitest (technically due to vite-node) cannot use
What's the desired goal for this diff specifically? Should tests pass without adding this but something failing?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was expecting the tests to pass without specifying
Since @vitejs/test-module-sync is not processed by Vite / Vitest, I expected the resolver to use module-sync condition (as it's included in ssr.resolve.externalConditions). But it used the ssr.resolve.conditions and that does not include module-sync condition. So the resolution fails (pkg-module-sync only has module-sync condition).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No ideal but this is how it works currently.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not entirely sure the purpose of this test. Is this to confirm Vitest's behavior (with current PR change) works same as Vite SSR? Or do you want to test importing
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vitest has its own externalization logic because Vite only supports externalizing in SSR, but we need the same behavior for JSDOM. We resolve the ID with We cannot use externalConditions because we don't know if it's external yet
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Thanks for the answers.
The main purpose of the test was to confirm the built file works in pure Node. But since the test was written in this way, I encountered this and wondered whether it's a way to configure Vitest to work more similar to Node on this aspect. I can run
I think Vite has the same problem. Vite uses |
||||
| esbuild: { | ||||
| target: 'node20', | ||||
| }, | ||||
|
|
||||
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.
Is this a bug fix?
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.
Yes.
If it is determined as external here,
vite/packages/vite/src/node/plugins/resolve.ts
Lines 348 to 352 in e7c3d47
the resolution here should use
externalConditions.vite/packages/vite/src/node/plugins/resolve.ts
Line 383 in e7c3d47
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.
Interesting, it's only for build? Might not be relevant to this PR specifically but curious what's the idea for
options.isBuildcheck? Is this because Vite crawl imports by ourselves by import analysis resolve (and not resolve for external) during dev, but this is specifically for rollup?If we have
externalizeflag inresolveExportsOrImports, then technically can we also pass this flag instead of swapping offoptions.conditionsforfetchModulenode resolve?vite/packages/vite/src/node/ssr/fetchModule.ts
Lines 51 to 54 in 8033e5b
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.
It is only for build. The purpose is to run this part of code:
vite/packages/vite/src/node/plugins/resolve.ts
Lines 771 to 787 in e7c3d47
In dev, this specifier rewrite is not needed because
fetchModulehandles that.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.
No, we cannot do that. Because
tryNodeResolvewill return a non-absolute id whenexternalizeistrue(e.g.react/jsx-runtime).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 reference 👍 As I understand, the latter part of
processResultwhere it checksresolved.idis fairly an edge case, so previously usingconditionsinstead ofexternalConditionswouldn't have made much difference.I'm still digesting the history of that logic, but will follow up on that on my own.