Skip to content

Commit

Permalink
refactor: allow custom connections in node module runner
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 27, 2024
1 parent 52edfc9 commit 9005841
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/vite/src/module-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type {
ModuleRunnerHMRConnection,
ModuleRunnerImportMeta,
ModuleRunnerOptions,
ModuleRunnerHmr,
} from './types'
export {
ssrDynamicImportKey,
Expand Down
24 changes: 12 additions & 12 deletions packages/vite/src/module-runner/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ export type FetchFunction = (
importer?: string,
) => Promise<FetchResult>

export interface ModuleRunnerHmr {
/**
* Configure how HMR communicates between the client and the server.
*/
connection: ModuleRunnerHMRConnection
/**
* Configure HMR logger.
*/
logger?: false | HMRLogger
}

export interface ModuleRunnerOptions {
/**
* Root of the project
Expand All @@ -140,18 +151,7 @@ export interface ModuleRunnerOptions {
/**
* Disable HMR or configure HMR options.
*/
hmr?:
| false
| {
/**
* Configure how HMR communicates between the client and the server.
*/
connection: ModuleRunnerHMRConnection
/**
* Configure HMR logger.
*/
logger?: false | HMRLogger
}
hmr?: false | ModuleRunnerHmr
/**
* Custom module cache. If not provided, creates a separate module cache for each ModuleRunner instance.
*/
Expand Down
24 changes: 18 additions & 6 deletions packages/vite/src/node/ssr/runtime/serverModuleRunner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { existsSync, readFileSync } from 'node:fs'
import { ESModulesEvaluator, ModuleRunner } from 'vite/module-runner'
import type { ModuleEvaluator, ModuleRunnerOptions } from 'vite/module-runner'
import {
ESModulesEvaluator,
ModuleRunner
} from 'vite/module-runner'
import type {
ModuleEvaluator,
ModuleRunnerHMRConnection,
ModuleRunnerHmr,

ModuleRunnerOptions} from 'vite/module-runner'
import type { ViteDevServer } from '../../server'
import type { DevEnvironment } from '../../server/environment'
import { ServerHMRConnector } from './serverHmrConnector'
Expand All @@ -19,10 +27,8 @@ export interface ServerModuleRunnerOptions
hmr?:
| false
| {
logger?: Exclude<
ModuleRunnerOptions['hmr'],
false | undefined
>['logger']
connection?: ModuleRunnerHMRConnection
logger?: ModuleRunnerHmr['logger']
}
/**
* Provide a custom module runner. This controls how the code is executed.
Expand All @@ -37,6 +43,12 @@ function createHMROptions(
if (server.config.server.hmr === false || options.hmr === false) {
return false
}
if (options.hmr?.connection) {
return {
connection: options.hmr.connection,
logger: options.hmr.logger,
}
}
const connection = new ServerHMRConnector(server)
return {
connection,
Expand Down

0 comments on commit 9005841

Please sign in to comment.