Skip to content

Commit 93d286c

Browse files
authored
fix(resolve): fix resolve cache key for external conditions (#18332)
1 parent 896d6e3 commit 93d286c

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

packages/vite/src/node/__tests__/environment.spec.ts

+56-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { createBuilder } from '../build'
77
import { createServerModuleRunner } from '../ssr/runtime/serverModuleRunner'
88

99
describe('custom environment conditions', () => {
10-
function getConfig(): InlineConfig {
10+
function getConfig({
11+
noExternal,
12+
}: {
13+
noExternal: true | undefined
14+
}): InlineConfig {
1115
return {
1216
configFile: false,
1317
root: import.meta.dirname,
@@ -19,7 +23,7 @@ describe('custom environment conditions', () => {
1923
// no web / default
2024
ssr: {
2125
resolve: {
22-
noExternal: true,
26+
noExternal,
2327
},
2428
build: {
2529
outDir: path.join(
@@ -35,8 +39,9 @@ describe('custom environment conditions', () => {
3539
worker: {
3640
webCompatible: true,
3741
resolve: {
38-
noExternal: true,
42+
noExternal,
3943
conditions: ['worker'],
44+
externalConditions: ['worker'],
4045
},
4146
build: {
4247
outDir: path.join(
@@ -52,8 +57,9 @@ describe('custom environment conditions', () => {
5257
custom1: {
5358
webCompatible: true,
5459
resolve: {
55-
noExternal: true,
60+
noExternal,
5661
conditions: ['custom1'],
62+
externalConditions: ['custom1'],
5763
},
5864
build: {
5965
outDir: path.join(
@@ -69,8 +75,9 @@ describe('custom environment conditions', () => {
6975
custom2: {
7076
webCompatible: false,
7177
resolve: {
72-
noExternal: true,
78+
noExternal,
7379
conditions: ['custom2'],
80+
externalConditions: ['custom2'],
7481
},
7582
build: {
7683
outDir: path.join(
@@ -86,8 +93,9 @@ describe('custom environment conditions', () => {
8693
custom3: {
8794
webCompatible: false,
8895
resolve: {
89-
noExternal: true,
96+
noExternal,
9097
conditions: ['custom3'],
98+
externalConditions: ['custom3'],
9199
},
92100
build: {
93101
outDir: path.join(
@@ -103,8 +111,9 @@ describe('custom environment conditions', () => {
103111
custom3_2: {
104112
webCompatible: false,
105113
resolve: {
106-
noExternal: true,
114+
noExternal,
107115
conditions: ['custom3'],
116+
externalConditions: ['custom3'],
108117
},
109118
build: {
110119
outDir: path.join(
@@ -120,8 +129,8 @@ describe('custom environment conditions', () => {
120129
}
121130
}
122131

123-
test('dev', async () => {
124-
const server = await createServer(getConfig())
132+
test('dev noExternal', async () => {
133+
const server = await createServer(getConfig({ noExternal: true }))
125134
onTestFinished(() => server.close())
126135

127136
const results: Record<string, unknown> = {}
@@ -154,8 +163,44 @@ describe('custom environment conditions', () => {
154163
`)
155164
})
156165

166+
test('dev external', async () => {
167+
const server = await createServer(getConfig({ noExternal: undefined }))
168+
onTestFinished(() => server.close())
169+
170+
const results: Record<string, unknown> = {}
171+
for (const key of [
172+
'ssr',
173+
'worker',
174+
'custom1',
175+
'custom2',
176+
'custom3',
177+
'custom3_2',
178+
]) {
179+
const runner = createServerModuleRunner(server.environments[key], {
180+
hmr: {
181+
logger: false,
182+
},
183+
sourcemapInterceptor: false,
184+
})
185+
const mod = await runner.import(
186+
'/fixtures/test-dep-conditions-app/entry.js',
187+
)
188+
results[key] = mod.default
189+
}
190+
expect(results).toMatchInlineSnapshot(`
191+
{
192+
"custom1": "index.custom1.js",
193+
"custom2": "index.custom2.js",
194+
"custom3": "index.custom3.js",
195+
"custom3_2": "index.custom3.js",
196+
"ssr": "index.default.js",
197+
"worker": "index.worker.js",
198+
}
199+
`)
200+
})
201+
157202
test('css', async () => {
158-
const server = await createServer(getConfig())
203+
const server = await createServer(getConfig({ noExternal: true }))
159204
onTestFinished(() => server.close())
160205

161206
const modJs = await server.ssrLoadModule(
@@ -174,7 +219,7 @@ describe('custom environment conditions', () => {
174219
})
175220

176221
test('build', async () => {
177-
const builder = await createBuilder(getConfig())
222+
const builder = await createBuilder(getConfig({ noExternal: true }))
178223
const results: Record<string, unknown> = {}
179224
for (const key of [
180225
'ssr',

packages/vite/src/node/packages.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
tryStatSync,
1010
} from './utils'
1111
import type { Plugin } from './plugin'
12-
import type { InternalResolveOptions } from './plugins/resolve'
12+
import type { InternalResolveOptionsWithOverrideConditions } from './plugins/resolve'
1313

1414
let pnp: typeof import('pnpapi') | undefined
1515
if (process.versions.pnp) {
@@ -27,11 +27,11 @@ export interface PackageData {
2727
setResolvedCache: (
2828
key: string,
2929
entry: string,
30-
options: InternalResolveOptions,
30+
options: InternalResolveOptionsWithOverrideConditions,
3131
) => void
3232
getResolvedCache: (
3333
key: string,
34-
options: InternalResolveOptions,
34+
options: InternalResolveOptionsWithOverrideConditions,
3535
) => string | undefined
3636
data: {
3737
[field: string]: any
@@ -223,14 +223,18 @@ export function loadPackageData(pkgPath: string): PackageData {
223223
return pkg
224224
}
225225

226-
function getResolveCacheKey(key: string, options: InternalResolveOptions) {
226+
function getResolveCacheKey(
227+
key: string,
228+
options: InternalResolveOptionsWithOverrideConditions,
229+
) {
227230
// cache key needs to include options which affect
228231
// `resolvePackageEntry` or `resolveDeepImport`
229232
return [
230233
key,
231234
options.webCompatible ? '1' : '0',
232235
options.isRequire ? '1' : '0',
233236
options.conditions.join('_'),
237+
options.overrideConditions?.join('_') || '',
234238
options.extensions.join('_'),
235239
options.mainFields.join('_'),
236240
].join('|')

0 commit comments

Comments
 (0)