diff --git a/packages/vite/src/node/__tests__/build.spec.ts b/packages/vite/src/node/__tests__/build.spec.ts index f87a27b37e9c45..8000a212f95b58 100644 --- a/packages/vite/src/node/__tests__/build.spec.ts +++ b/packages/vite/src/node/__tests__/build.spec.ts @@ -18,6 +18,7 @@ import { ChunkMetadataMap, build, createBuilder, + injectEnvironmentToHooks, onRollupLog, resolveBuildOutputs, resolveLibFilename, @@ -25,6 +26,8 @@ import { } from '../build' import type { Logger } from '../logger' import { createLogger } from '../logger' +import type { Plugin } from '../plugin' +import { getHookHandler } from '../plugins' const dirname = import.meta.dirname @@ -1578,6 +1581,34 @@ async function buildProjectWithRenderBuiltUrl( })) as RolldownOutput } +describe('injectEnvironmentToHooks', () => { + test('binds environment to hotUpdate hook', async () => { + const config = await resolveConfig( + { configFile: false, logLevel: 'error' }, + 'serve', + ) + const environment = new BuildEnvironment('client', config) + const chunkMetadataMap = new ChunkMetadataMap() + let name: string | undefined + const plugin: Plugin = { + name: 'test-hot-update-env', + hotUpdate() { + name = this.environment.name + }, + } + + const wrapped = injectEnvironmentToHooks( + environment, + chunkMetadataMap, + plugin, + ) + const context = { meta: {} } as import('rolldown').PluginContext + + await getHookHandler(wrapped.hotUpdate!).call(context, {} as any) + expect(name).toBe('client') + }) +}) + /** * for each chunks in output1, if there's a chunk in output2 with the same fileName, * ensure that the chunk code is the same. if not, the chunk hash should have changed. diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 38ba6ba27f5492..b9d3f44f232ba2 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -1339,6 +1339,15 @@ export function injectEnvironmentToHooks( } } + if (plugin.hotUpdate) { + clone.hotUpdate = wrapEnvironmentHook( + environment, + chunkMetadataMap, + plugin, + 'hotUpdate', + ) + } + return clone }