Skip to content

Commit fdef04f

Browse files
authored
feat: add warning for unsupported hooks in plugin returned from applyToEnvironment hook (#23191)
1 parent 472385e commit fdef04f

3 files changed

Lines changed: 60 additions & 5 deletions

File tree

‎docs/guide/api-environment-plugins.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ export default defineConfig({
242242
})
243243
```
244244
245+
Note that the plugin returned from `applyToEnvironment` or `perEnvironmentPlugin` should not use Vite-specific hooks.
246+
245247
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.
246248
247249
## Application-Plugin Communication

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,43 @@ describe('resolveConfig', () => {
11671167
await resolveConfig({ root: './inc?ud#s*', customLogger: logger }, 'build')
11681168
})
11691169

1170+
test('warns about ignored hooks returned from applyToEnvironment', async () => {
1171+
const warn = vi.fn()
1172+
const logger = createLogger('info', {
1173+
console: { warn } as unknown as Console,
1174+
})
1175+
1176+
await resolveConfig(
1177+
{
1178+
configFile: false,
1179+
customLogger: logger,
1180+
plugins: [
1181+
{
1182+
name: 'parent-plugin',
1183+
applyToEnvironment() {
1184+
return {
1185+
name: 'environment-plugin',
1186+
config: () => undefined,
1187+
configEnvironment: () => undefined,
1188+
configureServer: () => undefined,
1189+
configResolved: () => undefined,
1190+
resolveId: () => undefined,
1191+
}
1192+
},
1193+
},
1194+
],
1195+
},
1196+
'serve',
1197+
)
1198+
1199+
expect(warn).toHaveBeenCalledOnce()
1200+
expect(warn).toHaveBeenCalledWith(
1201+
expect.stringContaining(
1202+
'Plugin "environment-plugin" defines Vite-specific hooks (config, configEnvironment, configureServer, configResolved) in a plugin returned from applyToEnvironment. These hooks will be ignored.',
1203+
),
1204+
)
1205+
})
1206+
11701207
test('syncs `build.rollupOptions` and `build.rolldownOptions`', async () => {
11711208
const resolved = await resolveConfig({}, 'build')
11721209
expect(resolved.build!.rollupOptions).toStrictEqual(

‎packages/vite/src/node/plugin.ts‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,13 @@ export type PluginOption = Thenable<
401401
| PluginOption[]
402402
>
403403

404+
const ignoredEnvironmentPluginHooks = [
405+
'config',
406+
'configEnvironment',
407+
'configureServer',
408+
'configResolved',
409+
] as const
410+
404411
export async function resolveEnvironmentPlugins(
405412
environment: PartialEnvironment,
406413
): Promise<Plugin[]> {
@@ -412,11 +419,20 @@ export async function resolveEnvironmentPlugins(
412419
continue
413420
}
414421
if (applied !== true) {
415-
environmentPlugins.push(
416-
...((await asyncFlatten(arraify(applied))).filter(
417-
Boolean,
418-
) as Plugin[]),
419-
)
422+
const appliedPlugins = (await asyncFlatten(arraify(applied))).filter(
423+
Boolean,
424+
) as Plugin[]
425+
for (const appliedPlugin of appliedPlugins) {
426+
const ignoredHooks = ignoredEnvironmentPluginHooks.filter(
427+
(hook) => appliedPlugin[hook],
428+
)
429+
if (ignoredHooks.length > 0) {
430+
environment.logger.warnOnce(
431+
`Plugin "${appliedPlugin.name}" defines Vite-specific hooks (${ignoredHooks.join(', ')}) in a plugin returned from applyToEnvironment. These hooks will be ignored.`,
432+
)
433+
}
434+
}
435+
environmentPlugins.push(...appliedPlugins)
420436
continue
421437
}
422438
}

0 commit comments

Comments
 (0)