diff --git a/packages/vite/src/node/index.ts b/packages/vite/src/node/index.ts index 88931d85800ef2..5aaff7552740ba 100644 --- a/packages/vite/src/node/index.ts +++ b/packages/vite/src/node/index.ts @@ -24,6 +24,7 @@ export { createRunnableDevEnvironment, isRunnableDevEnvironment, type RunnableDevEnvironment, + type RunnableDevEnvironmentContext, } from './server/environments/runnableEnvironment' export { DevEnvironment, diff --git a/packages/vite/src/node/server/environments/runnableEnvironment.ts b/packages/vite/src/node/server/environments/runnableEnvironment.ts index f0c90710900341..58eb0a52b8f2c3 100644 --- a/packages/vite/src/node/server/environments/runnableEnvironment.ts +++ b/packages/vite/src/node/server/environments/runnableEnvironment.ts @@ -8,7 +8,7 @@ import { createServerModuleRunner } from '../../ssr/runtime/serverModuleRunner' export function createRunnableDevEnvironment( name: string, config: ResolvedConfig, - context: DevEnvironmentContext, + context: RunnableDevEnvironmentContext, ): DevEnvironment { if (context.hot == null) { throw new Error( @@ -32,7 +32,7 @@ export function createRunnableDevEnvironment( } export interface RunnableDevEnvironmentContext extends DevEnvironmentContext { - runner?: ModuleRunner + runner?: (environment: RunnableDevEnvironment) => ModuleRunner } export function isRunnableDevEnvironment( @@ -50,7 +50,9 @@ class RunnableDevEnvironment extends DevEnvironment { context: RunnableDevEnvironmentContext, ) { super(name, config, context) - this.runner = context.runner || createServerModuleRunner(this) + this.runner = context.runner + ? context.runner(this) + : createServerModuleRunner(this) } import(url: string): Promise { diff --git a/packages/vite/src/node/ssr/runtime/__tests__/server-worker-runner.spec.ts b/packages/vite/src/node/ssr/runtime/__tests__/server-worker-runner.spec.ts index d756d57273bbf4..e58520a32ecba1 100644 --- a/packages/vite/src/node/ssr/runtime/__tests__/server-worker-runner.spec.ts +++ b/packages/vite/src/node/ssr/runtime/__tests__/server-worker-runner.spec.ts @@ -32,7 +32,7 @@ describe('running module runner inside a worker', () => { dev: { createEnvironment: (name, config) => { return new DevEnvironment(name, config, { - runner: { + runnerOptions: { transport: new RemoteEnvironmentTransport({ send: (data) => worker.postMessage(data), onMessage: (handler) => worker.on('message', handler), diff --git a/playground/hmr-ssr/__tests__/hmr-ssr.spec.ts b/playground/hmr-ssr/__tests__/hmr-ssr.spec.ts index b71d77289158ca..f6684242a2ea81 100644 --- a/playground/hmr-ssr/__tests__/hmr-ssr.spec.ts +++ b/playground/hmr-ssr/__tests__/hmr-ssr.spec.ts @@ -10,8 +10,13 @@ import { test, vi, } from 'vitest' -import type { InlineConfig, ViteDevServer } from 'vite' -import { createServer, createServerModuleRunner } from 'vite' +import type { InlineConfig, RunnableDevEnvironment, ViteDevServer } from 'vite' +import { + createRunnableDevEnvironment, + createServer, + createServerHotChannel, + createServerModuleRunner, +} from 'vite' import type { ModuleRunner } from 'vite/module-runner' import { addFile, @@ -1036,6 +1041,10 @@ async function setupModuleRunner( globalThis.__HMR__ = initHmrState as any + const logger = new HMRMockLogger() + // @ts-expect-error not typed for HMR + globalThis.log = (...msg) => logger.log(...msg) + server = await createServer({ configFile: resolve(testDir, 'vite.config.ts'), root: testDir, @@ -1053,6 +1062,19 @@ async function setupModuleRunner( }, preTransformRequests: false, }, + environments: { + ssr: { + dev: { + createEnvironment(name, config) { + return createRunnableDevEnvironment(name, config, { + runner: (env) => + createServerModuleRunner(env, { hmr: { logger } }), + hot: createServerHotChannel(), + }) + }, + }, + }, + }, optimizeDeps: { disabled: true, noDiscovery: true, @@ -1061,15 +1083,7 @@ async function setupModuleRunner( ...serverOptions, }) - const logger = new HMRMockLogger() - // @ts-expect-error not typed for HMR - globalThis.log = (...msg) => logger.log(...msg) - - runner = createServerModuleRunner(server.environments.ssr, { - hmr: { - logger, - }, - }) + runner = (server.environments.ssr as RunnableDevEnvironment).runner await waitForWatcher(server, waitForFile)