Skip to content

Commit

Permalink
fix: make it easier to configure environment runner
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 3, 2024
1 parent 0356e10 commit c7bf649
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
15 changes: 5 additions & 10 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,7 @@ export function createRunnableDevEnvironment(

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

Expand All @@ -33,28 +34,22 @@ export function isRunnableDevEnvironment(

class RunnableDevEnvironment extends DevEnvironment {
private _runner: ModuleRunner | undefined
private _runnerFactory:
| ((environment: RunnableDevEnvironment) => ModuleRunner)
| undefined
private _runnerOptions: ServerModuleRunnerOptions | undefined

constructor(
name: string,
config: ResolvedConfig,
context: RunnableDevEnvironmentContext,
) {
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)
this._runner = createServerModuleRunner(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

0 comments on commit c7bf649

Please sign in to comment.