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
2 changes: 2 additions & 0 deletions docs/guide/api-environment-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
26 changes: 21 additions & 5 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,13 @@ export type PluginOption = Thenable<
| PluginOption[]
>

const ignoredEnvironmentPluginHooks = [
'config',
'configEnvironment',
'configureServer',
'configResolved',
] as const

export async function resolveEnvironmentPlugins(
environment: PartialEnvironment,
): Promise<Plugin[]> {
Expand All @@ -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
}
}
Expand Down
Loading