Skip to content

Commit db2ecc7

Browse files
authored
fix: rolldownOptions/rollupOptions merging at environment level (#21612)
1 parent e56103f commit db2ecc7

2 files changed

Lines changed: 140 additions & 1 deletion

File tree

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,117 @@ describe('mergeConfig', () => {
617617
testRolldownOptions.environments.client.build.rolldownOptions.platform,
618618
).toBe('browser')
619619
})
620+
621+
describe('later plugin can read `rollupOptions` set via `rolldownOptions` in earlier plugin', () => {
622+
test('top-level config', async () => {
623+
expect.assertions(2)
624+
await resolveConfig(
625+
{
626+
plugins: [
627+
{
628+
name: 'plugin-a',
629+
config() {
630+
return {
631+
build: {
632+
rolldownOptions: {
633+
platform: 'neutral',
634+
},
635+
},
636+
worker: {
637+
rolldownOptions: {
638+
platform: 'neutral',
639+
},
640+
},
641+
}
642+
},
643+
},
644+
{
645+
name: 'plugin-b',
646+
config(config) {
647+
expect(config.build?.rollupOptions?.platform).toBe('neutral')
648+
expect(config.worker?.rollupOptions?.platform).toBe('neutral')
649+
},
650+
},
651+
],
652+
},
653+
'build',
654+
)
655+
})
656+
657+
test('new `environments` object', async () => {
658+
expect.assertions(1)
659+
await resolveConfig(
660+
{
661+
plugins: [
662+
{
663+
name: 'plugin-a',
664+
config() {
665+
return {
666+
environments: {
667+
ssr: {
668+
build: {
669+
rolldownOptions: {
670+
platform: 'neutral',
671+
},
672+
},
673+
},
674+
},
675+
}
676+
},
677+
},
678+
{
679+
name: 'plugin-b',
680+
config(config) {
681+
expect(
682+
config.environments?.ssr?.build?.rollupOptions?.platform,
683+
).toBe('neutral')
684+
},
685+
},
686+
],
687+
},
688+
'build',
689+
)
690+
})
691+
692+
test('new environment on existing `environments` object', async () => {
693+
expect.assertions(1)
694+
await resolveConfig(
695+
{
696+
environments: {
697+
// environments exists, but no ssr
698+
client: {},
699+
},
700+
plugins: [
701+
{
702+
name: 'plugin-a',
703+
config() {
704+
return {
705+
environments: {
706+
ssr: {
707+
build: {
708+
rolldownOptions: {
709+
platform: 'neutral',
710+
},
711+
},
712+
},
713+
},
714+
}
715+
},
716+
},
717+
{
718+
name: 'plugin-b',
719+
config(config) {
720+
expect(
721+
config.environments?.ssr?.build?.rollupOptions?.platform,
722+
).toBe('neutral')
723+
},
724+
},
725+
],
726+
},
727+
'build',
728+
)
729+
})
730+
})
620731
})
621732

622733
describe('resolveEnvPrefix', () => {

packages/vite/src/node/utils.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,20 @@ const rollupOptionsRootPaths = new Set([
12891289
'ssr.optimizeDeps',
12901290
])
12911291

1292+
/**
1293+
* Sets up `rollupOptions` compat proxies for an environment.
1294+
*/
1295+
function setupRollupOptionCompatForEnvironment(environment: any): any {
1296+
if (!isObject(environment)) {
1297+
return environment
1298+
}
1299+
const merged: Record<string, any> = { ...environment }
1300+
if (isObject(merged.build)) {
1301+
setupRollupOptionCompat(merged.build, 'build')
1302+
}
1303+
return merged
1304+
}
1305+
12921306
export function hasBothRollupOptionsAndRolldownOptions(
12931307
options: Record<string, any>,
12941308
): boolean {
@@ -1334,7 +1348,21 @@ function mergeConfigRecursively(
13341348
}
13351349

13361350
if (existing == null) {
1337-
merged[key] = value
1351+
if (rootPath === '' && key === 'environments' && isObject(value)) {
1352+
// Clone to avoid mutating the original override object
1353+
const environments = { ...value }
1354+
for (const envName in environments) {
1355+
environments[envName] = setupRollupOptionCompatForEnvironment(
1356+
environments[envName],
1357+
)
1358+
}
1359+
merged[key] = environments
1360+
} else if (rootPath === 'environments') {
1361+
// `environments` exists, but a new environment is added
1362+
merged[key] = setupRollupOptionCompatForEnvironment(value)
1363+
} else {
1364+
merged[key] = value
1365+
}
13381366
continue
13391367
}
13401368

0 commit comments

Comments
 (0)