fix(ssr): ignore module exports condition (#11409)

This commit is contained in:
Bjorn Lu 2022-12-20 17:21:58 +08:00 committed by GitHub
parent 857d578191
commit d3c9c0b7c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 4 deletions

View File

@ -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)) {

View File

@ -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]')
})

View File

@ -0,0 +1 @@
export default '[success]'

View File

@ -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'

View File

@ -0,0 +1,11 @@
{
"name": "@vitejs/test-module-condition",
"private": true,
"version": "0.0.0",
"exports": {
".": {
"module": "./module.js",
"import": "./import.mjs"
}
}
}

View File

@ -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"

View File

@ -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<p class="css-lib">I should be blue</p>`
html += `\n<p class="module-condition">${moduleConditionMessage}</p>`
return html + '\n'
}

View File

@ -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'