From 8f76093dd0dc0922c0b4e0802ce2524af07ac7a8 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Fri, 7 Aug 2026 15:50:08 +0900 Subject: [PATCH] feat: add warning for unsupported hooks in plugin returned from applyToEnvironment hook --- docs/guide/api-environment-plugins.md | 2 + .../vite/src/node/__tests__/config.spec.ts | 37 +++++++++++++++++++ packages/vite/src/node/plugin.ts | 26 ++++++++++--- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/docs/guide/api-environment-plugins.md b/docs/guide/api-environment-plugins.md index 959cf5af6e1271..b85c73a891fb94 100644 --- a/docs/guide/api-environment-plugins.md +++ b/docs/guide/api-environment-plugins.md @@ -242,6 +242,8 @@ export default defineConfig({ }) ``` +Note that the plugin returned from `applyToEnvironment` or `perEnvironmentPlugin` should not use Vite-specific hooks. + 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. ## Application-Plugin Communication diff --git a/packages/vite/src/node/__tests__/config.spec.ts b/packages/vite/src/node/__tests__/config.spec.ts index 5cf98adaf2d835..7e1a5a61a4709e 100644 --- a/packages/vite/src/node/__tests__/config.spec.ts +++ b/packages/vite/src/node/__tests__/config.spec.ts @@ -1162,6 +1162,43 @@ describe('resolveConfig', () => { await resolveConfig({ root: './inc?ud#s*', customLogger: logger }, 'build') }) + test('warns about ignored hooks returned from applyToEnvironment', async () => { + const warn = vi.fn() + const logger = createLogger('info', { + console: { warn } as unknown as Console, + }) + + await resolveConfig( + { + configFile: false, + customLogger: logger, + plugins: [ + { + name: 'parent-plugin', + applyToEnvironment() { + return { + name: 'environment-plugin', + config: () => undefined, + configEnvironment: () => undefined, + configureServer: () => undefined, + configResolved: () => undefined, + resolveId: () => undefined, + } + }, + }, + ], + }, + 'serve', + ) + + expect(warn).toHaveBeenCalledOnce() + expect(warn).toHaveBeenCalledWith( + expect.stringContaining( + 'Plugin "environment-plugin" defines Vite-specific hooks (config, configEnvironment, configureServer, configResolved) in a plugin returned from applyToEnvironment. These hooks will be ignored.', + ), + ) + }) + test('syncs `build.rollupOptions` and `build.rolldownOptions`', async () => { const resolved = await resolveConfig({}, 'build') expect(resolved.build!.rollupOptions).toStrictEqual( diff --git a/packages/vite/src/node/plugin.ts b/packages/vite/src/node/plugin.ts index a0be15a581123e..380882ee6b31ee 100644 --- a/packages/vite/src/node/plugin.ts +++ b/packages/vite/src/node/plugin.ts @@ -387,6 +387,13 @@ export type PluginOption = Thenable< | PluginOption[] > +const ignoredEnvironmentPluginHooks = [ + 'config', + 'configEnvironment', + 'configureServer', + 'configResolved', +] as const + export async function resolveEnvironmentPlugins( environment: PartialEnvironment, ): Promise { @@ -398,11 +405,20 @@ export async function resolveEnvironmentPlugins( continue } if (applied !== true) { - environmentPlugins.push( - ...((await asyncFlatten(arraify(applied))).filter( - Boolean, - ) as Plugin[]), - ) + const appliedPlugins = (await asyncFlatten(arraify(applied))).filter( + Boolean, + ) as Plugin[] + for (const appliedPlugin of appliedPlugins) { + const ignoredHooks = ignoredEnvironmentPluginHooks.filter( + (hook) => appliedPlugin[hook], + ) + if (ignoredHooks.length > 0) { + environment.logger.warnOnce( + `Plugin "${appliedPlugin.name}" defines Vite-specific hooks (${ignoredHooks.join(', ')}) in a plugin returned from applyToEnvironment. These hooks will be ignored.`, + ) + } + } + environmentPlugins.push(...appliedPlugins) continue } }