diff --git a/packages/vite/src/node/__tests__/environment.spec.ts b/packages/vite/src/node/__tests__/environment.spec.ts index 6cd24a2ece3c0b..96a2964ea9b8e4 100644 --- a/packages/vite/src/node/__tests__/environment.spec.ts +++ b/packages/vite/src/node/__tests__/environment.spec.ts @@ -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, @@ -19,7 +23,7 @@ describe('custom environment conditions', () => { // no web / default ssr: { resolve: { - noExternal: true, + noExternal, }, build: { outDir: path.join( @@ -35,8 +39,9 @@ describe('custom environment conditions', () => { worker: { webCompatible: true, resolve: { - noExternal: true, + noExternal, conditions: ['worker'], + externalConditions: ['worker'], }, build: { outDir: path.join( @@ -52,8 +57,9 @@ describe('custom environment conditions', () => { custom1: { webCompatible: true, resolve: { - noExternal: true, + noExternal, conditions: ['custom1'], + externalConditions: ['custom1'], }, build: { outDir: path.join( @@ -69,8 +75,9 @@ describe('custom environment conditions', () => { custom2: { webCompatible: false, resolve: { - noExternal: true, + noExternal, conditions: ['custom2'], + externalConditions: ['custom2'], }, build: { outDir: path.join( @@ -86,8 +93,9 @@ describe('custom environment conditions', () => { custom3: { webCompatible: false, resolve: { - noExternal: true, + noExternal, conditions: ['custom3'], + externalConditions: ['custom3'], }, build: { outDir: path.join( @@ -103,8 +111,9 @@ describe('custom environment conditions', () => { custom3_2: { webCompatible: false, resolve: { - noExternal: true, + noExternal, conditions: ['custom3'], + externalConditions: ['custom3'], }, build: { outDir: path.join( @@ -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 = {} @@ -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 = {} + 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( @@ -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 = {} for (const key of [ 'ssr', diff --git a/packages/vite/src/node/packages.ts b/packages/vite/src/node/packages.ts index 175505d49e5373..ffabe43f89595a 100644 --- a/packages/vite/src/node/packages.ts +++ b/packages/vite/src/node/packages.ts @@ -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) { @@ -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 @@ -223,7 +223,10 @@ 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 [ @@ -231,6 +234,7 @@ function getResolveCacheKey(key: string, options: InternalResolveOptions) { options.webCompatible ? '1' : '0', options.isRequire ? '1' : '0', options.conditions.join('_'), + options.overrideConditions?.join('_') || '', options.extensions.join('_'), options.mainFields.join('_'), ].join('|')