Skip to content

Commit

Permalink
fix(resolve): fix resolve cache key for external conditions (#18332)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Oct 15, 2024
1 parent 896d6e3 commit 93d286c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
67 changes: 56 additions & 11 deletions packages/vite/src/node/__tests__/environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { createBuilder } from '../build'
import { createServerModuleRunner } from '../ssr/runtime/serverModuleRunner'

describe('custom environment conditions', () => {
function getConfig(): InlineConfig {
function getConfig({
noExternal,
}: {
noExternal: true | undefined
}): InlineConfig {
return {
configFile: false,
root: import.meta.dirname,
Expand All @@ -19,7 +23,7 @@ describe('custom environment conditions', () => {
// no web / default
ssr: {
resolve: {
noExternal: true,
noExternal,
},
build: {
outDir: path.join(
Expand All @@ -35,8 +39,9 @@ describe('custom environment conditions', () => {
worker: {
webCompatible: true,
resolve: {
noExternal: true,
noExternal,
conditions: ['worker'],
externalConditions: ['worker'],
},
build: {
outDir: path.join(
Expand All @@ -52,8 +57,9 @@ describe('custom environment conditions', () => {
custom1: {
webCompatible: true,
resolve: {
noExternal: true,
noExternal,
conditions: ['custom1'],
externalConditions: ['custom1'],
},
build: {
outDir: path.join(
Expand All @@ -69,8 +75,9 @@ describe('custom environment conditions', () => {
custom2: {
webCompatible: false,
resolve: {
noExternal: true,
noExternal,
conditions: ['custom2'],
externalConditions: ['custom2'],
},
build: {
outDir: path.join(
Expand All @@ -86,8 +93,9 @@ describe('custom environment conditions', () => {
custom3: {
webCompatible: false,
resolve: {
noExternal: true,
noExternal,
conditions: ['custom3'],
externalConditions: ['custom3'],
},
build: {
outDir: path.join(
Expand All @@ -103,8 +111,9 @@ describe('custom environment conditions', () => {
custom3_2: {
webCompatible: false,
resolve: {
noExternal: true,
noExternal,
conditions: ['custom3'],
externalConditions: ['custom3'],
},
build: {
outDir: path.join(
Expand All @@ -120,8 +129,8 @@ describe('custom environment conditions', () => {
}
}

test('dev', async () => {
const server = await createServer(getConfig())
test('dev noExternal', async () => {
const server = await createServer(getConfig({ noExternal: true }))
onTestFinished(() => server.close())

const results: Record<string, unknown> = {}
Expand Down Expand Up @@ -154,8 +163,44 @@ describe('custom environment conditions', () => {
`)
})

test('dev external', async () => {
const server = await createServer(getConfig({ noExternal: undefined }))
onTestFinished(() => server.close())

const results: Record<string, unknown> = {}
for (const key of [
'ssr',
'worker',
'custom1',
'custom2',
'custom3',
'custom3_2',
]) {
const runner = createServerModuleRunner(server.environments[key], {
hmr: {
logger: false,
},
sourcemapInterceptor: false,
})
const mod = await runner.import(
'/fixtures/test-dep-conditions-app/entry.js',
)
results[key] = mod.default
}
expect(results).toMatchInlineSnapshot(`
{
"custom1": "index.custom1.js",
"custom2": "index.custom2.js",
"custom3": "index.custom3.js",
"custom3_2": "index.custom3.js",
"ssr": "index.default.js",
"worker": "index.worker.js",
}
`)
})

test('css', async () => {
const server = await createServer(getConfig())
const server = await createServer(getConfig({ noExternal: true }))
onTestFinished(() => server.close())

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

test('build', async () => {
const builder = await createBuilder(getConfig())
const builder = await createBuilder(getConfig({ noExternal: true }))
const results: Record<string, unknown> = {}
for (const key of [
'ssr',
Expand Down
12 changes: 8 additions & 4 deletions packages/vite/src/node/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
tryStatSync,
} from './utils'
import type { Plugin } from './plugin'
import type { InternalResolveOptions } from './plugins/resolve'
import type { InternalResolveOptionsWithOverrideConditions } from './plugins/resolve'

let pnp: typeof import('pnpapi') | undefined
if (process.versions.pnp) {
Expand All @@ -27,11 +27,11 @@ export interface PackageData {
setResolvedCache: (
key: string,
entry: string,
options: InternalResolveOptions,
options: InternalResolveOptionsWithOverrideConditions,
) => void
getResolvedCache: (
key: string,
options: InternalResolveOptions,
options: InternalResolveOptionsWithOverrideConditions,
) => string | undefined
data: {
[field: string]: any
Expand Down Expand Up @@ -223,14 +223,18 @@ export function loadPackageData(pkgPath: string): PackageData {
return pkg
}

function getResolveCacheKey(key: string, options: InternalResolveOptions) {
function getResolveCacheKey(
key: string,
options: InternalResolveOptionsWithOverrideConditions,
) {
// cache key needs to include options which affect
// `resolvePackageEntry` or `resolveDeepImport`
return [
key,
options.webCompatible ? '1' : '0',
options.isRequire ? '1' : '0',
options.conditions.join('_'),
options.overrideConditions?.join('_') || '',
options.extensions.join('_'),
options.mainFields.join('_'),
].join('|')
Expand Down

0 comments on commit 93d286c

Please sign in to comment.