Skip to content

Commit 010063a

Browse files
committed
fix: trigger watchChange hook for all environments for native resolver
1 parent 5ea606c commit 010063a

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

packages/vite/src/node/plugin.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ export interface Plugin<A = any> extends RolldownPlugin<A> {
186186
* @experimental
187187
*/
188188
perEnvironmentStartEndDuringDev?: boolean
189+
/**
190+
* Opt-in this plugin into per-environment watchChange during dev.
191+
* For backward-compatibility, the watchChange hook is called only once during
192+
* dev, for the client environment. Plugins can opt-in to be called
193+
* per-environment, aligning with the watchChange hook behavior.
194+
* @experimental
195+
*/
196+
perEnvironmentWatchChangeDuringDev?: boolean
189197
/**
190198
* Enforce plugin invocation tier similar to webpack loaders. Hooks ordering
191199
* is still subject to the `order` property in the hook object.

packages/vite/src/node/plugins/resolve.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export function oxcResolvePlugin(
256256
? options.noExternal
257257
: [options.noExternal]
258258

259-
return viteResolvePlugin({
259+
const plugin = viteResolvePlugin({
260260
resolveOptions: {
261261
isBuild: options.isBuild,
262262
isProduction: options.isProduction,
@@ -368,6 +368,8 @@ export function oxcResolvePlugin(
368368
}
369369
: {}),
370370
})
371+
;(plugin as Plugin).perEnvironmentWatchChangeDuringDev = true
372+
return plugin
371373
},
372374
),
373375
]

packages/vite/src/node/server/index.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ export interface ServerOptions extends CommonServerOptions {
180180
* @experimental
181181
*/
182182
perEnvironmentStartEndDuringDev?: boolean
183+
/**
184+
* Backward compatibility. The watchChange hook was called only once for all environments.
185+
* This option enables per-environment watchChange hooks.
186+
* @default false
187+
* @experimental
188+
*/
189+
perEnvironmentWatchChangeDuringDev?: boolean
183190
/**
184191
* Run HMR tasks, by default the HMR propagation is done in parallel for all environments
185192
* @experimental
@@ -800,9 +807,13 @@ export async function _createServer(
800807
file = normalizePath(file)
801808
reloadOnTsconfigChange(server, file)
802809

803-
await pluginContainer.watchChange(file, {
804-
event: isUnlink ? 'delete' : 'create',
805-
})
810+
await Promise.all(
811+
Object.values(server.environments).map((environment) =>
812+
environment.pluginContainer.watchChange(file, {
813+
event: isUnlink ? 'delete' : 'create',
814+
}),
815+
),
816+
)
806817

807818
if (publicDir && publicFiles) {
808819
if (file.startsWith(publicDir)) {
@@ -834,7 +845,11 @@ export async function _createServer(
834845
file = normalizePath(file)
835846
reloadOnTsconfigChange(server, file)
836847

837-
await pluginContainer.watchChange(file, { event: 'update' })
848+
await Promise.all(
849+
Object.values(server.environments).map((environment) =>
850+
environment.pluginContainer.watchChange(file, { event: 'update' }),
851+
),
852+
)
838853
// invalidate module graph cache on file change
839854
for (const environment of Object.values(server.environments)) {
840855
environment.moduleGraph.onFileChange(file)
@@ -1102,6 +1117,7 @@ const _serverConfigDefaults = Object.freeze({
11021117
preTransformRequests: true,
11031118
// sourcemapIgnoreList
11041119
perEnvironmentStartEndDuringDev: false,
1120+
perEnvironmentWatchChangeDuringDev: false,
11051121
// hotUpdateEnvironments
11061122
} satisfies ServerOptions)
11071123
export const serverConfigDefaults: Readonly<Partial<ServerOptions>> =

packages/vite/src/node/server/pluginContainer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,10 +623,15 @@ class EnvironmentPluginContainer<Env extends Environment = Environment> {
623623
id: string,
624624
change: { event: 'create' | 'update' | 'delete' },
625625
): Promise<void> {
626+
const config = this.environment.getTopLevelConfig()
626627
await this.hookParallel(
627628
'watchChange',
628629
(plugin) => this._getPluginContext(plugin),
629630
() => [id, change],
631+
(plugin) =>
632+
this.environment.name === 'client' ||
633+
config.server.perEnvironmentWatchChangeDuringDev ||
634+
plugin.perEnvironmentWatchChangeDuringDev,
630635
)
631636
}
632637

@@ -1239,7 +1244,8 @@ class PluginContainer {
12391244
}
12401245

12411246
// For backward compatibility, buildStart and watchChange are called only for the client environment
1242-
// buildStart is called per environment for a plugin with the perEnvironmentStartEndDuring dev flag
1247+
// buildStart is called per environment for a plugin with the perEnvironmentStartEndDuringDev flag
1248+
// watchChange is called per environment for a plugin with the perEnvironmentWatchChangeDuringDev flag
12431249

12441250
async buildStart(_options?: InputOptions): Promise<void> {
12451251
return (

0 commit comments

Comments
 (0)