Skip to content

Commit

Permalink
fix: merge environments.ssr.resolve with root ssr config (#18857)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Dec 2, 2024
1 parent a75fc31 commit 3104331
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 7 deletions.
122 changes: 122 additions & 0 deletions packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,125 @@ describe('resolveConfig', () => {
await resolveConfig({ root: './inc?ud#s', customLogger: logger }, 'build')
})
})

test('config compat 1', async () => {
const config = await resolveConfig(
{
resolve: {
conditions: ['client1'],
},
ssr: {
resolve: {
conditions: ['ssr1'],
},
},
plugins: [
{
name: 'test',
config() {
return {
environments: {
client: {
resolve: {
conditions: ['client2'],
},
},
ssr: {
resolve: {
conditions: ['ssr2'],
},
},
},
}
},
},
],
},
'serve',
)
expect(config.resolve.conditions).toMatchInlineSnapshot(`
[
"client1",
"client2",
]
`)
expect(config.environments.client.resolve.conditions).toMatchInlineSnapshot(`
[
"client1",
"client2",
]
`)
expect(config.ssr.resolve?.conditions).toMatchInlineSnapshot(`
[
"ssr1",
"ssr2",
]
`)
expect(config.environments.ssr.resolve?.conditions).toMatchInlineSnapshot(`
[
"ssr1",
"ssr2",
]
`)
})

test('config compat 2', async () => {
const config = await resolveConfig(
{
environments: {
client: {
resolve: {
conditions: ['client1'],
},
},
ssr: {
resolve: {
conditions: ['ssr1'],
},
},
},
plugins: [
{
name: 'test',
config() {
return {
resolve: {
conditions: ['client2'],
},
ssr: {
resolve: {
conditions: ['ssr2'],
},
},
}
},
},
],
},
'serve',
)
expect(config.resolve.conditions).toMatchInlineSnapshot(`
[
"client2",
"client1",
]
`)
expect(config.environments.client.resolve.conditions).toMatchInlineSnapshot(`
[
"client2",
"client1",
]
`)
expect(config.ssr.resolve?.conditions).toMatchInlineSnapshot(`
[
"ssr2",
"ssr1",
]
`)
expect(config.environments.ssr.resolve?.conditions).toMatchInlineSnapshot(`
[
"ssr2",
"ssr1",
]
`)
})
22 changes: 15 additions & 7 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1083,13 +1083,16 @@ export async function resolveConfig(
configEnvironmentsSsr.optimizeDeps ?? {},
)

configEnvironmentsSsr.resolve ??= {}
configEnvironmentsSsr.resolve.conditions ??= config.ssr?.resolve?.conditions
configEnvironmentsSsr.resolve.externalConditions ??=
config.ssr?.resolve?.externalConditions
configEnvironmentsSsr.resolve.mainFields ??= config.ssr?.resolve?.mainFields
configEnvironmentsSsr.resolve.external ??= config.ssr?.external
configEnvironmentsSsr.resolve.noExternal ??= config.ssr?.noExternal
configEnvironmentsSsr.resolve = mergeConfig(
{
conditions: config.ssr?.resolve?.conditions,
externalConditions: config.ssr?.resolve?.externalConditions,
mainFields: config.ssr?.resolve?.mainFields,
external: config.ssr?.external,
noExternal: config.ssr?.noExternal,
} satisfies EnvironmentResolveOptions,
configEnvironmentsSsr.resolve ?? {},
)
}

if (config.build?.ssrEmitAssets !== undefined) {
Expand Down Expand Up @@ -1146,6 +1149,11 @@ export async function resolveConfig(
config.ssr?.target === 'webworker',
)

// Backward compatibility: merge config.environments.client.resolve back into config.resolve
config.resolve ??= {}
config.resolve.conditions = config.environments.client.resolve?.conditions
config.resolve.mainFields = config.environments.client.resolve?.mainFields

const resolvedDefaultResolve = resolveResolveOptions(config.resolve, logger)

const resolvedEnvironments: Record<string, ResolvedEnvironmentOptions> = {}
Expand Down

0 comments on commit 3104331

Please sign in to comment.