Skip to content

Commit 87d34b9

Browse files
committed
feat(ssr): add module-sync to default externalCondition
1 parent 3706eee commit 87d34b9

File tree

8 files changed

+55
-7
lines changed

8 files changed

+55
-7
lines changed

packages/vite/src/node/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ export const DEFAULT_SERVER_CONDITIONS = Object.freeze(
6464
DEFAULT_CONDITIONS.filter((c) => c !== 'browser'),
6565
)
6666

67-
export const DEFAULT_EXTERNAL_CONDITIONS = Object.freeze(['node'])
67+
export const DEFAULT_EXTERNAL_CONDITIONS = Object.freeze([
68+
'node',
69+
'module-sync',
70+
])
6871

6972
/**
7073
* The browser versions that are included in the Baseline Widely Available on 2025-05-01.

packages/vite/src/node/plugins/resolve.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ export function tryNodeResolve(
753753
const resolveId = deepMatch ? resolveDeepImport : resolvePackageEntry
754754
const unresolvedId = deepMatch ? '.' + id.slice(pkgId.length) : id
755755

756-
let resolved = resolveId(unresolvedId, pkg, options)
756+
let resolved = resolveId(unresolvedId, pkg, options, externalize)
757757
if (!resolved) {
758758
return
759759
}
@@ -893,6 +893,7 @@ export function resolvePackageEntry(
893893
id: string,
894894
{ dir, data, setResolvedCache, getResolvedCache }: PackageData,
895895
options: InternalResolveOptions,
896+
externalize?: boolean,
896897
): string | undefined {
897898
const { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id)
898899

@@ -907,7 +908,13 @@ export function resolvePackageEntry(
907908
// resolve exports field with highest priority
908909
// using https://github.com/lukeed/resolve.exports
909910
if (data.exports) {
910-
entryPoint = resolveExportsOrImports(data, '.', options, 'exports')
911+
entryPoint = resolveExportsOrImports(
912+
data,
913+
'.',
914+
options,
915+
'exports',
916+
externalize,
917+
)
911918
}
912919

913920
// fallback to mainFields if still not resolved
@@ -987,8 +994,12 @@ function resolveExportsOrImports(
987994
key: string,
988995
options: InternalResolveOptions,
989996
type: 'imports' | 'exports',
997+
externalize?: boolean,
990998
) {
991-
const conditions = options.conditions.map((condition) => {
999+
const rawConditions = externalize
1000+
? options.externalConditions
1001+
: options.conditions
1002+
const conditions = rawConditions.map((condition) => {
9921003
if (condition === DEV_PROD_CONDITION) {
9931004
return options.isProduction ? 'production' : 'development'
9941005
}
@@ -1010,6 +1021,7 @@ function resolveDeepImport(
10101021
id: string,
10111022
{ setResolvedCache, getResolvedCache, dir, data }: PackageData,
10121023
options: InternalResolveOptions,
1024+
externalize?: boolean,
10131025
): string | undefined {
10141026
const cache = getResolvedCache(id, options)
10151027
if (cache) {
@@ -1024,7 +1036,13 @@ function resolveDeepImport(
10241036
if (isObject(exportsField) && !Array.isArray(exportsField)) {
10251037
// resolve without postfix (see #7098)
10261038
const { file, postfix } = splitFileAndPostfix(relativeId)
1027-
const exportsId = resolveExportsOrImports(data, file, options, 'exports')
1039+
const exportsId = resolveExportsOrImports(
1040+
data,
1041+
file,
1042+
options,
1043+
'exports',
1044+
externalize,
1045+
)
10281046
if (exportsId !== undefined) {
10291047
relativeId = exportsId + postfix
10301048
} else {

playground/ssr-resolve/__tests__/ssr-resolve.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ test.runIf(isBuild)('correctly resolve entrypoints', async () => {
2323
new RegExp(`from ${_}@vitejs/test-deep-import/bar${_}`),
2424
)
2525

26+
expect(contents).toMatch(new RegExp(`from ${_}@vitejs/test-module-sync${_}`))
27+
2628
await expect(import(`${testDir}/dist/main.mjs`)).resolves.toBeTruthy()
2729
})
2830

playground/ssr-resolve/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import fileEntry from '@vitejs/test-entries/file'
66
import pkgExportsEntry from '@vitejs/test-resolve-pkg-exports/entry'
77
import deepFoo from '@vitejs/test-deep-import/foo'
88
import deepBar from '@vitejs/test-deep-import/bar'
9+
import moduleSync from '@vitejs/test-module-sync'
910
import { used } from './util'
1011

1112
export default `
@@ -14,5 +15,6 @@ export default `
1415
pkg-exports/entry: ${pkgExportsEntry}
1516
deep-import/foo: ${deepFoo}
1617
deep-import/bar: ${deepBar}
18+
module-sync: ${moduleSync}
1719
util: ${used(['[success]'])}
1820
`

playground/ssr-resolve/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"debug": "node --inspect-brk ../../packages/vite/bin/vite build"
99
},
1010
"dependencies": {
11+
"@vitejs/test-deep-import": "file:./deep-import",
1112
"@vitejs/test-entries": "file:./entries",
12-
"@vitejs/test-resolve-pkg-exports": "file:./pkg-exports",
13-
"@vitejs/test-deep-import": "file:./deep-import"
13+
"@vitejs/test-module-sync": "file:./pkg-module-sync",
14+
"@vitejs/test-resolve-pkg-exports": "file:./pkg-exports"
1415
}
1516
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'module-sync'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@vitejs/test-module-sync",
3+
"type": "module",
4+
"private": true,
5+
"version": "0.0.0",
6+
"exports": {
7+
".": {
8+
"module-sync": "./index.js"
9+
}
10+
}
11+
}

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)