Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
30 changes: 29 additions & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> = { ...environment }
if (isObject(merged.build)) {
setupRollupOptionCompat(merged.build, 'build')
}
return merged
}

export function hasBothRollupOptionsAndRolldownOptions(
options: Record<string, any>,
): boolean {
Expand Down Expand Up @@ -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
}

Expand Down
Loading