Skip to content

Commit

Permalink
fix: respect resolve.conditions during nodeResolve
Browse files Browse the repository at this point in the history
Upstream: vitejs#13487
  • Loading branch information
himself65 committed Sep 20, 2023
1 parent 21a62da commit f92ed86
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ async function instantiateModule(

const {
isProduction,
resolve: { dedupe, preserveSymlinks },
resolve: { dedupe, preserveSymlinks, conditions },
root,
} = server.config

const resolveOptions: InternalResolveOptionsWithOverrideConditions = {
mainFields: ['main'],
browserField: true,
conditions: [],
overrideConditions: ['production', 'development'],
overrideConditions: [...conditions, 'production', 'development'],
extensions: ['.js', '.cjs', '.json'],
dedupe,
preserveSymlinks,
Expand Down
7 changes: 7 additions & 0 deletions playground/ssr-deps/__tests__/ssr-deps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ test('import css library', async () => {
expect(await page.textContent('.module-condition')).toMatch('[success]')
})

test('import react/server/server.node.unbundled.js', async () => {
await page.goto(url)
expect(await page.textContent('.react-server')).toMatch(
'server.node.unbundled.js',
)
})

describe.runIf(isServe)('hmr', () => {
test('handle isomorphic module updates', async () => {
await page.goto(url)
Expand Down
1 change: 1 addition & 0 deletions playground/ssr-deps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@vitejs/test-object-assigned-exports": "file:./object-assigned-exports",
"@vitejs/test-only-object-assigned-exports": "file:./only-object-assigned-exports",
"@vitejs/test-primitive-export": "file:./primitive-export",
"@vitejs/test-react-server": "file:./react-server",
"@vitejs/test-read-file-content": "file:./read-file-content",
"@vitejs/test-require-absolute": "file:./require-absolute",
"@vitejs/test-ts-transpiled-exports": "file:./ts-transpiled-exports",
Expand Down
21 changes: 21 additions & 0 deletions playground/ssr-deps/react-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@vitejs/test-react-server",
"private": true,
"type": "module",
"version": "0.0.0",
"exports": {
"./server": {
"react-server": {
"workerd": "./server.edge.js",
"deno": "./server.browser.js",
"node": {
"webpack": "./server.node.js",
"default": "./server.node.unbundled.js"
},
"edge-light": "./server.edge.js",
"browser": "./server.browser.js"
},
"default": "./server.js"
}
}
}
1 change: 1 addition & 0 deletions playground/ssr-deps/react-server/server.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'server.browser.js'
1 change: 1 addition & 0 deletions playground/ssr-deps/react-server/server.edge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'server.edge.js'
1 change: 1 addition & 0 deletions playground/ssr-deps/react-server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'server.js'
1 change: 1 addition & 0 deletions playground/ssr-deps/react-server/server.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'server.node.js'
1 change: 1 addition & 0 deletions playground/ssr-deps/react-server/server.node.unbundled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'server.node.unbundled.js'
3 changes: 3 additions & 0 deletions playground/ssr-deps/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export async function createServer(root = process.cwd(), hmrPort) {
).createServer({
root,
logLevel: isTest ? 'error' : 'info',
resolve: {
conditions: ['react-server'],
},
server: {
middlewareMode: true,
watch: {
Expand Down
3 changes: 3 additions & 0 deletions playground/ssr-deps/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import importBuiltinCjs from '@vitejs/test-import-builtin-cjs'
import { hello as linkedNoExternal } from '@vitejs/test-linked-no-external'
import virtualMessage from '@vitejs/test-pkg-exports/virtual'
import moduleConditionMessage from '@vitejs/test-module-condition'
import reactServerMessage from '@vitejs/test-react-server/server'
import '@vitejs/test-css-lib'

// This import will set a 'Hello World!" message in the nested-external non-entry dependency
Expand Down Expand Up @@ -91,6 +92,8 @@ export async function render(url, rootDir) {

html += `\n<p class="module-condition">${moduleConditionMessage}</p>`

html += `\n<p class="react-server">${reactServerMessage}</p>`

html += `\n<p class="isomorphic-module-server">${isomorphicModuleMessage}</p>`

html += `\n<p class="isomorphic-module-browser"></p>`
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f92ed86

Please sign in to comment.