Skip to content

Commit d3c9c0b

Browse files
authored
fix(ssr): ignore module exports condition (#11409)
1 parent 857d578 commit d3c9c0b

File tree

8 files changed

+41
-4
lines changed

8 files changed

+41
-4
lines changed

packages/vite/src/node/ssr/ssrModuleLoader.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
usingDynamicImport,
99
} from '../utils'
1010
import { transformRequest } from '../server/transformRequest'
11-
import type { InternalResolveOptions } from '../plugins/resolve'
11+
import type { InternalResolveOptionsWithOverrideConditions } from '../plugins/resolve'
1212
import { tryNodeResolve } from '../plugins/resolve'
1313
import {
1414
ssrDynamicImportKey,
@@ -112,10 +112,11 @@ async function instantiateModule(
112112
root,
113113
} = server.config
114114

115-
const resolveOptions: InternalResolveOptions = {
115+
const resolveOptions: InternalResolveOptionsWithOverrideConditions = {
116116
mainFields: ['main'],
117117
browserField: true,
118118
conditions: [],
119+
overrideConditions: ['production', 'development'],
119120
extensions: ['.js', '.cjs', '.json'],
120121
dedupe,
121122
preserveSymlinks,
@@ -223,7 +224,7 @@ async function instantiateModule(
223224
async function nodeImport(
224225
id: string,
225226
importer: string,
226-
resolveOptions: InternalResolveOptions,
227+
resolveOptions: InternalResolveOptionsWithOverrideConditions,
227228
) {
228229
let url: string
229230
if (id.startsWith('node:') || isBuiltin(id)) {

playground/ssr-deps/__tests__/ssr-deps.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,8 @@ test('import css library', async () => {
116116
await page.goto(url)
117117
expect(await getColor('.css-lib')).toBe('blue')
118118
})
119+
120+
test('import css library', async () => {
121+
await page.goto(url)
122+
expect(await page.textContent('.module-condition')).toMatch('[success]')
123+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default '[success]'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// this is written in ESM but the file extension implies this is evaluated as CJS.
2+
// BUT this doesn't matter in practice as the `module` condition is not used in node.
3+
// hence SSR should not load this file.
4+
export default '[fail] should not load me'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@vitejs/test-module-condition",
3+
"private": true,
4+
"version": "0.0.0",
5+
"exports": {
6+
".": {
7+
"module": "./module.js",
8+
"import": "./import.mjs"
9+
}
10+
}
11+
}

playground/ssr-deps/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"@vitejs/test-external-using-external-entry": "file:./external-using-external-entry",
3030
"@vitejs/test-external-entry": "file:./external-entry",
3131
"@vitejs/test-linked-no-external": "link:./linked-no-external",
32-
"@vitejs/test-pkg-exports": "file:./pkg-exports"
32+
"@vitejs/test-pkg-exports": "file:./pkg-exports",
33+
"@vitejs/test-module-condition": "file:./module-condition"
3334
},
3435
"devDependencies": {
3536
"express": "^4.18.2"

playground/ssr-deps/src/app.js

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import noExternalCjs from '@vitejs/test-no-external-cjs'
1515
import importBuiltinCjs from '@vitejs/test-import-builtin-cjs'
1616
import { hello as linkedNoExternal } from '@vitejs/test-linked-no-external'
1717
import virtualMessage from '@vitejs/test-pkg-exports/virtual'
18+
import moduleConditionMessage from '@vitejs/test-module-condition'
1819
import '@vitejs/test-css-lib'
1920

2021
// This import will set a 'Hello World!" message in the nested-external non-entry dependency
@@ -87,5 +88,7 @@ export async function render(url, rootDir) {
8788

8889
html += `\n<p class="css-lib">I should be blue</p>`
8990

91+
html += `\n<p class="module-condition">${moduleConditionMessage}</p>`
92+
9093
return html + '\n'
9194
}

pnpm-lock.yaml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)