diff --git a/packages/vite/src/node/__tests__/config.spec.ts b/packages/vite/src/node/__tests__/config.spec.ts index e3488cc253288e..6ccbe75f421c27 100644 --- a/packages/vite/src/node/__tests__/config.spec.ts +++ b/packages/vite/src/node/__tests__/config.spec.ts @@ -617,6 +617,117 @@ describe('mergeConfig', () => { testRolldownOptions.environments.client.build.rolldownOptions.platform, ).toBe('browser') }) + + describe('later plugin can read `rollupOptions` set via `rolldownOptions` in earlier plugin', () => { + test('top-level config', async () => { + expect.assertions(2) + await resolveConfig( + { + plugins: [ + { + name: 'plugin-a', + config() { + return { + build: { + rolldownOptions: { + platform: 'neutral', + }, + }, + worker: { + rolldownOptions: { + platform: 'neutral', + }, + }, + } + }, + }, + { + name: 'plugin-b', + config(config) { + expect(config.build?.rollupOptions?.platform).toBe('neutral') + expect(config.worker?.rollupOptions?.platform).toBe('neutral') + }, + }, + ], + }, + 'build', + ) + }) + + test('new `environments` object', async () => { + expect.assertions(1) + await resolveConfig( + { + plugins: [ + { + name: 'plugin-a', + config() { + return { + environments: { + ssr: { + build: { + rolldownOptions: { + platform: 'neutral', + }, + }, + }, + }, + } + }, + }, + { + name: 'plugin-b', + config(config) { + expect( + config.environments?.ssr?.build?.rollupOptions?.platform, + ).toBe('neutral') + }, + }, + ], + }, + 'build', + ) + }) + + test('new environment on existing `environments` object', async () => { + expect.assertions(1) + await resolveConfig( + { + environments: { + // environments exists, but no ssr + client: {}, + }, + plugins: [ + { + name: 'plugin-a', + config() { + return { + environments: { + ssr: { + build: { + rolldownOptions: { + platform: 'neutral', + }, + }, + }, + }, + } + }, + }, + { + name: 'plugin-b', + config(config) { + expect( + config.environments?.ssr?.build?.rollupOptions?.platform, + ).toBe('neutral') + }, + }, + ], + }, + 'build', + ) + }) + }) }) describe('resolveEnvPrefix', () => { diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 7aac91f0f69e5d..e88e373b87c3bc 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1289,6 +1289,20 @@ const rollupOptionsRootPaths = new Set([ 'ssr.optimizeDeps', ]) +/** + * Sets up `rollupOptions` compat proxies for an environment. + */ +function setupRollupOptionCompatForEnvironment(environment: any): any { + if (!isObject(environment)) { + return environment + } + const merged: Record = { ...environment } + if (isObject(merged.build)) { + setupRollupOptionCompat(merged.build, 'build') + } + return merged +} + export function hasBothRollupOptionsAndRolldownOptions( options: Record, ): boolean { @@ -1334,7 +1348,21 @@ function mergeConfigRecursively( } if (existing == null) { - merged[key] = value + if (rootPath === '' && key === 'environments' && isObject(value)) { + // Clone to avoid mutating the original override object + const environments = { ...value } + for (const envName in environments) { + environments[envName] = setupRollupOptionCompatForEnvironment( + environments[envName], + ) + } + merged[key] = environments + } else if (rootPath === 'environments') { + // `environments` exists, but a new environment is added + merged[key] = setupRollupOptionCompatForEnvironment(value) + } else { + merged[key] = value + } continue }