From d3c9c0b7c186c4415e8b347aebb5dd6942b0625a Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 20 Dec 2022 17:21:58 +0800 Subject: [PATCH] fix(ssr): ignore module exports condition (#11409) --- packages/vite/src/node/ssr/ssrModuleLoader.ts | 7 ++++--- playground/ssr-deps/__tests__/ssr-deps.spec.ts | 5 +++++ playground/ssr-deps/module-condition/import.mjs | 1 + playground/ssr-deps/module-condition/module.js | 4 ++++ playground/ssr-deps/module-condition/package.json | 11 +++++++++++ playground/ssr-deps/package.json | 3 ++- playground/ssr-deps/src/app.js | 3 +++ pnpm-lock.yaml | 11 +++++++++++ 8 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 playground/ssr-deps/module-condition/import.mjs create mode 100644 playground/ssr-deps/module-condition/module.js create mode 100644 playground/ssr-deps/module-condition/package.json diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 4952eed7645a79..4cb226de83944e 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -8,7 +8,7 @@ import { usingDynamicImport, } from '../utils' import { transformRequest } from '../server/transformRequest' -import type { InternalResolveOptions } from '../plugins/resolve' +import type { InternalResolveOptionsWithOverrideConditions } from '../plugins/resolve' import { tryNodeResolve } from '../plugins/resolve' import { ssrDynamicImportKey, @@ -112,10 +112,11 @@ async function instantiateModule( root, } = server.config - const resolveOptions: InternalResolveOptions = { + const resolveOptions: InternalResolveOptionsWithOverrideConditions = { mainFields: ['main'], browserField: true, conditions: [], + overrideConditions: ['production', 'development'], extensions: ['.js', '.cjs', '.json'], dedupe, preserveSymlinks, @@ -223,7 +224,7 @@ async function instantiateModule( async function nodeImport( id: string, importer: string, - resolveOptions: InternalResolveOptions, + resolveOptions: InternalResolveOptionsWithOverrideConditions, ) { let url: string if (id.startsWith('node:') || isBuiltin(id)) { diff --git a/playground/ssr-deps/__tests__/ssr-deps.spec.ts b/playground/ssr-deps/__tests__/ssr-deps.spec.ts index 5a45d047ee86a0..a6d8973977e7a3 100644 --- a/playground/ssr-deps/__tests__/ssr-deps.spec.ts +++ b/playground/ssr-deps/__tests__/ssr-deps.spec.ts @@ -116,3 +116,8 @@ test('import css library', async () => { await page.goto(url) expect(await getColor('.css-lib')).toBe('blue') }) + +test('import css library', async () => { + await page.goto(url) + expect(await page.textContent('.module-condition')).toMatch('[success]') +}) diff --git a/playground/ssr-deps/module-condition/import.mjs b/playground/ssr-deps/module-condition/import.mjs new file mode 100644 index 00000000000000..2ecbfe1a42cf13 --- /dev/null +++ b/playground/ssr-deps/module-condition/import.mjs @@ -0,0 +1 @@ +export default '[success]' diff --git a/playground/ssr-deps/module-condition/module.js b/playground/ssr-deps/module-condition/module.js new file mode 100644 index 00000000000000..2a5cbd94bcad9b --- /dev/null +++ b/playground/ssr-deps/module-condition/module.js @@ -0,0 +1,4 @@ +// this is written in ESM but the file extension implies this is evaluated as CJS. +// BUT this doesn't matter in practice as the `module` condition is not used in node. +// hence SSR should not load this file. +export default '[fail] should not load me' diff --git a/playground/ssr-deps/module-condition/package.json b/playground/ssr-deps/module-condition/package.json new file mode 100644 index 00000000000000..e7809eced127bb --- /dev/null +++ b/playground/ssr-deps/module-condition/package.json @@ -0,0 +1,11 @@ +{ + "name": "@vitejs/test-module-condition", + "private": true, + "version": "0.0.0", + "exports": { + ".": { + "module": "./module.js", + "import": "./import.mjs" + } + } +} diff --git a/playground/ssr-deps/package.json b/playground/ssr-deps/package.json index 13d641b4d6efaf..ce48077dc196d0 100644 --- a/playground/ssr-deps/package.json +++ b/playground/ssr-deps/package.json @@ -29,7 +29,8 @@ "@vitejs/test-external-using-external-entry": "file:./external-using-external-entry", "@vitejs/test-external-entry": "file:./external-entry", "@vitejs/test-linked-no-external": "link:./linked-no-external", - "@vitejs/test-pkg-exports": "file:./pkg-exports" + "@vitejs/test-pkg-exports": "file:./pkg-exports", + "@vitejs/test-module-condition": "file:./module-condition" }, "devDependencies": { "express": "^4.18.2" diff --git a/playground/ssr-deps/src/app.js b/playground/ssr-deps/src/app.js index 0f71a24d9d0bd9..0af57b094d76e0 100644 --- a/playground/ssr-deps/src/app.js +++ b/playground/ssr-deps/src/app.js @@ -15,6 +15,7 @@ import noExternalCjs from '@vitejs/test-no-external-cjs' 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 '@vitejs/test-css-lib' // 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) { html += `\n

I should be blue

` + html += `\n

${moduleConditionMessage}

` + return html + '\n' } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6fb5671e0e696a..35e61b439faa22 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -878,6 +878,7 @@ importers: '@vitejs/test-forwarded-export': file:./forwarded-export '@vitejs/test-import-builtin-cjs': file:./import-builtin-cjs '@vitejs/test-linked-no-external': link:./linked-no-external + '@vitejs/test-module-condition': file:./module-condition '@vitejs/test-no-external-cjs': file:./no-external-cjs '@vitejs/test-no-external-css': file:./no-external-css '@vitejs/test-non-optimized-with-nested-external': file:./non-optimized-with-nested-external @@ -901,6 +902,7 @@ importers: '@vitejs/test-forwarded-export': file:playground/ssr-deps/forwarded-export '@vitejs/test-import-builtin-cjs': file:playground/ssr-deps/import-builtin-cjs '@vitejs/test-linked-no-external': link:linked-no-external + '@vitejs/test-module-condition': file:playground/ssr-deps/module-condition '@vitejs/test-no-external-cjs': file:playground/ssr-deps/no-external-cjs '@vitejs/test-no-external-css': file:playground/ssr-deps/no-external-css '@vitejs/test-non-optimized-with-nested-external': file:playground/ssr-deps/non-optimized-with-nested-external @@ -947,6 +949,9 @@ importers: playground/ssr-deps/linked-no-external: specifiers: {} + playground/ssr-deps/module-condition: + specifiers: {} + playground/ssr-deps/nested-external: specifiers: {} @@ -8752,6 +8757,12 @@ packages: version: 0.0.0 dev: false + file:playground/ssr-deps/module-condition: + resolution: {directory: playground/ssr-deps/module-condition, type: directory} + name: '@vitejs/test-module-condition' + version: 0.0.0 + dev: false + file:playground/ssr-deps/nested-external: resolution: {directory: playground/ssr-deps/nested-external, type: directory} name: '@vitejs/test-nested-external'