Skip to content

Commit

Permalink
fix(ssr): ignore module exports condition (#11409)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Dec 20, 2022
1 parent 857d578 commit d3c9c0b
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 4 deletions.
7 changes: 4 additions & 3 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down
5 changes: 5 additions & 0 deletions playground/ssr-deps/__tests__/ssr-deps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]')
})
1 change: 1 addition & 0 deletions playground/ssr-deps/module-condition/import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '[success]'
4 changes: 4 additions & 0 deletions playground/ssr-deps/module-condition/module.js
Original file line number Diff line number Diff line change
@@ -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'
11 changes: 11 additions & 0 deletions playground/ssr-deps/module-condition/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@vitejs/test-module-condition",
"private": true,
"version": "0.0.0",
"exports": {
".": {
"module": "./module.js",
"import": "./import.mjs"
}
}
}
3 changes: 2 additions & 1 deletion playground/ssr-deps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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 @@ -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
Expand Down Expand Up @@ -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'
}
11 changes: 11 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 d3c9c0b

Please sign in to comment.