Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): ignore module exports condition #11409

Merged
merged 1 commit into from Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
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
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
@@ -0,0 +1 @@
export default '[success]'
4 changes: 4 additions & 0 deletions 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'
11 changes: 11 additions & 0 deletions 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"
}
}
}
3 changes: 2 additions & 1 deletion playground/ssr-deps/package.json
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
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.