From f92ed86f53a2cbcc4e7dde96041e7e8f41f15803 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Tue, 19 Sep 2023 19:09:24 -0500 Subject: [PATCH] fix: respect `resolve.conditions` during `nodeResolve` Upstream: #13487 --- packages/vite/src/node/ssr/ssrModuleLoader.ts | 4 ++-- .../ssr-deps/__tests__/ssr-deps.spec.ts | 7 +++++++ playground/ssr-deps/package.json | 1 + playground/ssr-deps/react-server/package.json | 21 +++++++++++++++++++ .../ssr-deps/react-server/server.browser.js | 1 + .../ssr-deps/react-server/server.edge.js | 1 + playground/ssr-deps/react-server/server.js | 1 + .../ssr-deps/react-server/server.node.js | 1 + .../react-server/server.node.unbundled.js | 1 + playground/ssr-deps/server.js | 3 +++ playground/ssr-deps/src/app.js | 3 +++ pnpm-lock.yaml | 10 +++++++++ 12 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 playground/ssr-deps/react-server/package.json create mode 100644 playground/ssr-deps/react-server/server.browser.js create mode 100644 playground/ssr-deps/react-server/server.edge.js create mode 100644 playground/ssr-deps/react-server/server.js create mode 100644 playground/ssr-deps/react-server/server.node.js create mode 100644 playground/ssr-deps/react-server/server.node.unbundled.js diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 2181b8b6ab9127..daeaf74d30882d 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -121,7 +121,7 @@ async function instantiateModule( const { isProduction, - resolve: { dedupe, preserveSymlinks }, + resolve: { dedupe, preserveSymlinks, conditions }, root, } = server.config @@ -129,7 +129,7 @@ async function instantiateModule( mainFields: ['main'], browserField: true, conditions: [], - overrideConditions: ['production', 'development'], + overrideConditions: [...conditions, 'production', 'development'], extensions: ['.js', '.cjs', '.json'], dedupe, preserveSymlinks, diff --git a/playground/ssr-deps/__tests__/ssr-deps.spec.ts b/playground/ssr-deps/__tests__/ssr-deps.spec.ts index 2af338507c5a0d..e2a6cffb1d17df 100644 --- a/playground/ssr-deps/__tests__/ssr-deps.spec.ts +++ b/playground/ssr-deps/__tests__/ssr-deps.spec.ts @@ -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) diff --git a/playground/ssr-deps/package.json b/playground/ssr-deps/package.json index e861b9c51815e1..be67567c30bdb5 100644 --- a/playground/ssr-deps/package.json +++ b/playground/ssr-deps/package.json @@ -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", diff --git a/playground/ssr-deps/react-server/package.json b/playground/ssr-deps/react-server/package.json new file mode 100644 index 00000000000000..8557764083ba41 --- /dev/null +++ b/playground/ssr-deps/react-server/package.json @@ -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" + } + } +} diff --git a/playground/ssr-deps/react-server/server.browser.js b/playground/ssr-deps/react-server/server.browser.js new file mode 100644 index 00000000000000..d90ab1cf809559 --- /dev/null +++ b/playground/ssr-deps/react-server/server.browser.js @@ -0,0 +1 @@ +export default 'server.browser.js' diff --git a/playground/ssr-deps/react-server/server.edge.js b/playground/ssr-deps/react-server/server.edge.js new file mode 100644 index 00000000000000..df3a0e4e03c879 --- /dev/null +++ b/playground/ssr-deps/react-server/server.edge.js @@ -0,0 +1 @@ +export default 'server.edge.js' diff --git a/playground/ssr-deps/react-server/server.js b/playground/ssr-deps/react-server/server.js new file mode 100644 index 00000000000000..2bb4704e980e3e --- /dev/null +++ b/playground/ssr-deps/react-server/server.js @@ -0,0 +1 @@ +export default 'server.js' diff --git a/playground/ssr-deps/react-server/server.node.js b/playground/ssr-deps/react-server/server.node.js new file mode 100644 index 00000000000000..c0ace533475086 --- /dev/null +++ b/playground/ssr-deps/react-server/server.node.js @@ -0,0 +1 @@ +export default 'server.node.js' diff --git a/playground/ssr-deps/react-server/server.node.unbundled.js b/playground/ssr-deps/react-server/server.node.unbundled.js new file mode 100644 index 00000000000000..942e6303cd3aaf --- /dev/null +++ b/playground/ssr-deps/react-server/server.node.unbundled.js @@ -0,0 +1 @@ +export default 'server.node.unbundled.js' diff --git a/playground/ssr-deps/server.js b/playground/ssr-deps/server.js index 05e86a863ea744..e3c7f2b32d742c 100644 --- a/playground/ssr-deps/server.js +++ b/playground/ssr-deps/server.js @@ -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: { diff --git a/playground/ssr-deps/src/app.js b/playground/ssr-deps/src/app.js index 9942dec47d7401..accf592cf5c510 100644 --- a/playground/ssr-deps/src/app.js +++ b/playground/ssr-deps/src/app.js @@ -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 @@ -91,6 +92,8 @@ export async function render(url, rootDir) { html += `\n

${moduleConditionMessage}

` + html += `\n

${reactServerMessage}

` + html += `\n

${isomorphicModuleMessage}

` html += `\n

` diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 53f46924c2a124..7199e93a718018 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1222,6 +1222,9 @@ importers: '@vitejs/test-primitive-export': specifier: file:./primitive-export version: file:playground/ssr-deps/primitive-export + '@vitejs/test-react-server': + specifier: file:./react-server + version: file:playground/ssr-deps/react-server '@vitejs/test-read-file-content': specifier: file:./read-file-content version: file:playground/ssr-deps/read-file-content @@ -1302,6 +1305,8 @@ importers: playground/ssr-deps/primitive-export: {} + playground/ssr-deps/react-server: {} + playground/ssr-deps/read-file-content: {} playground/ssr-deps/require-absolute: {} @@ -10917,6 +10922,11 @@ packages: name: '@vitejs/test-primitive-export' dev: false + file:playground/ssr-deps/react-server: + resolution: {directory: playground/ssr-deps/react-server, type: directory} + name: '@vitejs/test-react-server' + dev: false + file:playground/ssr-deps/read-file-content: resolution: {directory: playground/ssr-deps/read-file-content, type: directory} name: '@vitejs/test-read-file-content'