Skip to content

Commit fb35a78

Browse files
authoredOct 7, 2024··
fix: make it easier to configure environment runner (#18273)
1 parent 5cac054 commit fb35a78

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed
 

‎packages/vite/src/node/server/environments/runnableEnvironment.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ModuleRunner } from 'vite/module-runner'
22
import type { ResolvedConfig } from '../../config'
33
import type { DevEnvironmentContext } from '../environment'
44
import { DevEnvironment } from '../environment'
5+
import type { ServerModuleRunnerOptions } from '../../ssr/runtime/serverModuleRunner'
56
import { createServerModuleRunner } from '../../ssr/runtime/serverModuleRunner'
67
import type { HotChannel } from '../hmr'
78
import { createServerHotChannel } from '../hmr'
@@ -21,7 +22,11 @@ export function createRunnableDevEnvironment(
2122

2223
export interface RunnableDevEnvironmentContext
2324
extends Omit<DevEnvironmentContext, 'hot'> {
24-
runner?: (environment: RunnableDevEnvironment) => ModuleRunner
25+
runner?: (
26+
environment: RunnableDevEnvironment,
27+
options?: ServerModuleRunnerOptions,
28+
) => ModuleRunner
29+
runnerOptions?: ServerModuleRunnerOptions
2530
hot?: false | HotChannel
2631
}
2732

@@ -34,8 +39,12 @@ export function isRunnableDevEnvironment(
3439
class RunnableDevEnvironment extends DevEnvironment {
3540
private _runner: ModuleRunner | undefined
3641
private _runnerFactory:
37-
| ((environment: RunnableDevEnvironment) => ModuleRunner)
42+
| ((
43+
environment: RunnableDevEnvironment,
44+
options?: ServerModuleRunnerOptions,
45+
) => ModuleRunner)
3846
| undefined
47+
private _runnerOptions: ServerModuleRunnerOptions | undefined
3948

4049
constructor(
4150
name: string,
@@ -44,17 +53,15 @@ class RunnableDevEnvironment extends DevEnvironment {
4453
) {
4554
super(name, config, context as DevEnvironmentContext)
4655
this._runnerFactory = context.runner
56+
this._runnerOptions = context.runnerOptions
4757
}
4858

4959
get runner(): ModuleRunner {
5060
if (this._runner) {
5161
return this._runner
5262
}
53-
if (this._runnerFactory) {
54-
this._runner = this._runnerFactory(this)
55-
return this._runner
56-
}
57-
this._runner = createServerModuleRunner(this)
63+
const factory = this._runnerFactory || createServerModuleRunner
64+
this._runner = factory(this, this._runnerOptions)
5865
return this._runner
5966
}
6067
}

‎playground/hmr-ssr/__tests__/hmr-ssr.spec.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
createRunnableDevEnvironment,
1616
createServer,
1717
createServerHotChannel,
18-
createServerModuleRunner,
1918
} from 'vite'
2019
import type { ModuleRunner } from 'vite/module-runner'
2120
import {
@@ -1067,8 +1066,7 @@ async function setupModuleRunner(
10671066
dev: {
10681067
createEnvironment(name, config) {
10691068
return createRunnableDevEnvironment(name, config, {
1070-
runner: (env) =>
1071-
createServerModuleRunner(env, { hmr: { logger } }),
1069+
runnerOptions: { hmr: { logger } },
10721070
hot: createServerHotChannel(),
10731071
})
10741072
},

0 commit comments

Comments
 (0)
Please sign in to comment.