-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
docs: document ssr.resolve.conditions for custom package conditions
#9392
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
a28b986
707ba3b
1bd2250
b21497b
5081bd1
a7a170f
1674975
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 |
|---|---|---|
|
|
@@ -64,6 +64,63 @@ vitest --pool=forks | |
| ``` | ||
| ::: | ||
|
|
||
| ## Subpath imports with custom conditions are not resolved | ||
|
|
||
| If you are using [Node.js subpath imports](https://nodejs.org/api/packages.html#subpath-imports) with custom conditions in your `package.json`, you may find that Vitest does not respect these conditions by default. | ||
|
|
||
| For example, if you have the following in your `package.json`: | ||
|
|
||
| ```json | ||
| { | ||
| "imports": { | ||
| "#my-lib": { | ||
| "custom": "./lib/custom_lib.js", | ||
| "import": "./lib/node_lib.js" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| By default, Vitest will only use the `import` and `default` conditions. To make Vitest respect custom conditions, you need to configure [`ssr.resolve.conditions`](https://vite.dev/config/ssr-options#ssr-resolve-conditions) in your Vitest config: | ||
|
|
||
| ```ts [vitest.config.js] | ||
| import { defineConfig } from 'vitest/config' | ||
|
|
||
| export default defineConfig({ | ||
| ssr: { | ||
| resolve: { | ||
| conditions: ['custom', 'import', 'default'], | ||
| }, | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| ::: tip Why `ssr.resolve.conditions` and not `resolve.conditions`? | ||
| Vitest inherits Vite's configuration convention where: | ||
| - `resolve.conditions` is used for client-side application code (browser environments) | ||
| - `ssr.resolve.conditions` is used for server-side application code (Node.js environments) | ||
|
||
|
|
||
| Since Vitest runs tests in a Node.js environment (unless using browser mode), it uses `ssr.resolve.conditions` for module resolution. This applies to both package exports and subpath imports. | ||
|
|
||
| For more information, see [Vite's SSR documentation](https://vite.dev/guide/ssr#ssr-externals). | ||
| ::: | ||
|
|
||
| If you need to conditionally set conditions based on the runtime environment: | ||
|
|
||
| ```ts [vitest.config.js] | ||
| import { defineConfig } from 'vitest/config' | ||
|
|
||
| const isRunningOnBun = 'bun' in process.versions | ||
|
|
||
| export default defineConfig({ | ||
| ssr: { | ||
| resolve: { | ||
| conditions: isRunningOnBun ? ['bun', 'import', 'default'] : ['import', 'default'], | ||
| }, | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| ## Segfaults and native code errors | ||
|
|
||
| Running [native NodeJS modules](https://nodejs.org/api/addons.html) in `pool: 'threads'` can run into cryptic errors coming from the native code. | ||
|
|
||
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.
@copilot This is not only for sub path imports but any package condition in general. See #8764. Rewrite section with the framing.
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.
Rewritten in b21497b - changed section title to "Custom package conditions are not resolved" and expanded examples to include both package exports and subpath imports, addressing issue #8764.