Skip to content

Commit a330b80

Browse files
authored
fix: applyToEnvironment after configResolved (vitejs#20170)
1 parent 3a30c0a commit a330b80

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

docs/guide/api-environment-plugins.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ export default defineConfig({
184184
})
185185
```
186186
187+
The `applyToEnvironment` hook is called at config time, currently after `configResolved` due to projects in the ecosystem modifying the plugins in it. Environment plugins resolution may be moved before `configResolved` in the future.
188+
187189
## Environment in Build Hooks
188190
189191
In the same way as during dev, plugin hooks also receive the environment instance during build, replacing the `ssr` boolean.

packages/vite/src/node/config.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,16 @@ export async function resolveConfig(
14331433
workerPostPlugins,
14341434
)
14351435

1436+
// run configResolved hooks
1437+
await Promise.all(
1438+
createPluginHookUtils(workerResolved.plugins)
1439+
.getSortedPluginHooks('configResolved')
1440+
.map((hook) => hook.call(resolvedConfigContext, workerResolved)),
1441+
)
1442+
1443+
// Resolve environment plugins after configResolved because there are
1444+
// downstream projects modifying the plugins in it. This may change
1445+
// once the ecosystem is ready.
14361446
// During Build the client environment is used to bundle the worker
14371447
// Avoid overriding the mainConfig (resolved.environments.client)
14381448
;(workerResolved.environments as Record<
@@ -1448,13 +1458,6 @@ export async function resolveConfig(
14481458
},
14491459
}
14501460

1451-
// run configResolved hooks
1452-
await Promise.all(
1453-
createPluginHookUtils(workerResolved.plugins)
1454-
.getSortedPluginHooks('configResolved')
1455-
.map((hook) => hook.call(resolvedConfigContext, workerResolved)),
1456-
)
1457-
14581461
return workerResolved
14591462
}
14601463

@@ -1605,19 +1608,22 @@ export async function resolveConfig(
16051608
// TODO: Deprecate config.getSortedPlugins and config.getSortedPluginHooks
16061609
Object.assign(resolved, createPluginHookUtils(resolved.plugins))
16071610

1608-
for (const name of Object.keys(resolved.environments)) {
1609-
resolved.environments[name].plugins = await resolveEnvironmentPlugins(
1610-
new PartialEnvironment(name, resolved),
1611-
)
1612-
}
1613-
16141611
// call configResolved hooks
16151612
await Promise.all(
16161613
resolved
16171614
.getSortedPluginHooks('configResolved')
16181615
.map((hook) => hook.call(resolvedConfigContext, resolved)),
16191616
)
16201617

1618+
// Resolve environment plugins after configResolved because there are
1619+
// downstream projects modifying the plugins in it. This may change
1620+
// once the ecosystem is ready.
1621+
for (const name of Object.keys(resolved.environments)) {
1622+
resolved.environments[name].plugins = await resolveEnvironmentPlugins(
1623+
new PartialEnvironment(name, resolved),
1624+
)
1625+
}
1626+
16211627
optimizeDepsDisabledBackwardCompatibility(resolved, resolved.optimizeDeps)
16221628
optimizeDepsDisabledBackwardCompatibility(
16231629
resolved,

0 commit comments

Comments
 (0)