Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make it easier to configure environment runner #18273

Merged
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
21 changes: 14 additions & 7 deletions packages/vite/src/node/server/environments/runnableEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ModuleRunner } from 'vite/module-runner'
import type { ResolvedConfig } from '../../config'
import type { DevEnvironmentContext } from '../environment'
import { DevEnvironment } from '../environment'
import type { ServerModuleRunnerOptions } from '../../ssr/runtime/serverModuleRunner'
import { createServerModuleRunner } from '../../ssr/runtime/serverModuleRunner'
import type { HotChannel } from '../hmr'
import { createServerHotChannel } from '../hmr'
Expand All @@ -21,7 +22,11 @@ export function createRunnableDevEnvironment(

export interface RunnableDevEnvironmentContext
extends Omit<DevEnvironmentContext, 'hot'> {
runner?: (environment: RunnableDevEnvironment) => ModuleRunner
runner?: (
environment: RunnableDevEnvironment,
options?: ServerModuleRunnerOptions,
) => ModuleRunner
runnerOptions?: ServerModuleRunnerOptions
hot?: false | HotChannel
}

Expand All @@ -34,8 +39,12 @@ export function isRunnableDevEnvironment(
class RunnableDevEnvironment extends DevEnvironment {
private _runner: ModuleRunner | undefined
private _runnerFactory:
| ((environment: RunnableDevEnvironment) => ModuleRunner)
| ((
environment: RunnableDevEnvironment,
options?: ServerModuleRunnerOptions,
) => ModuleRunner)
| undefined
private _runnerOptions: ServerModuleRunnerOptions | undefined

constructor(
name: string,
Expand All @@ -44,17 +53,15 @@ class RunnableDevEnvironment extends DevEnvironment {
) {
super(name, config, context as DevEnvironmentContext)
this._runnerFactory = context.runner
this._runnerOptions = context.runnerOptions
}

get runner(): ModuleRunner {
if (this._runner) {
return this._runner
}
if (this._runnerFactory) {
this._runner = this._runnerFactory(this)
return this._runner
}
this._runner = createServerModuleRunner(this)
const factory = this._runnerFactory || createServerModuleRunner
this._runner = factory(this, this._runnerOptions)
return this._runner
}
}
Expand Down
4 changes: 1 addition & 3 deletions playground/hmr-ssr/__tests__/hmr-ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
createRunnableDevEnvironment,
createServer,
createServerHotChannel,
createServerModuleRunner,
} from 'vite'
import type { ModuleRunner } from 'vite/module-runner'
import {
Expand Down Expand Up @@ -1067,8 +1066,7 @@ async function setupModuleRunner(
dev: {
createEnvironment(name, config) {
return createRunnableDevEnvironment(name, config, {
runner: (env) =>
createServerModuleRunner(env, { hmr: { logger } }),
runnerOptions: { hmr: { logger } },
hot: createServerHotChannel(),
})
},
Expand Down
Loading