Skip to content

Commit 9d6a152

Browse files
committed
refactor: remove server from createServerModuleRunner
1 parent bf65476 commit 9d6a152

File tree

8 files changed

+14
-29
lines changed

8 files changed

+14
-29
lines changed

packages/vite/src/node/ssr/runtime/__tests__/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export async function createModuleRunnerTester(
7676
...config,
7777
})
7878
t.environment = t.server.environments.ssr
79-
t.runner = await createServerModuleRunner(t.server, t.environment, {
79+
t.runner = await createServerModuleRunner(t.environment, {
8080
hmr: {
8181
logger: false,
8282
},

packages/vite/src/node/ssr/runtime/serverHmrConnector.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { CustomPayload, HMRPayload } from 'types/hmrPayload'
22
import type { ModuleRunnerHMRConnection } from 'vite/module-runner'
3-
import type { ViteDevServer } from '../../server'
43
import type { HMRBroadcasterClient, ServerHMRChannel } from '../../server/hmr'
54

65
class ServerHMRBroadcasterClient implements HMRBroadcasterClient {
@@ -32,20 +31,11 @@ class ServerHMRBroadcasterClient implements HMRBroadcasterClient {
3231
*/
3332
export class ServerHMRConnector implements ModuleRunnerHMRConnection {
3433
private handlers: ((payload: HMRPayload) => void)[] = []
35-
private hmrChannel: ServerHMRChannel
3634
private hmrClient: ServerHMRBroadcasterClient
3735

3836
private connected = false
3937

40-
constructor(server: ViteDevServer) {
41-
const hmrChannel = server.hot?.channels.find(
42-
(c) => c.name === 'ssr',
43-
) as ServerHMRChannel
44-
if (!hmrChannel) {
45-
throw new Error(
46-
"Your version of Vite doesn't support HMR during SSR. Please, use Vite 5.1 or higher.",
47-
)
48-
}
38+
constructor(private hmrChannel: ServerHMRChannel) {
4939
this.hmrClient = new ServerHMRBroadcasterClient(hmrChannel)
5040
hmrChannel.api.outsideEmitter.on('send', (payload: HMRPayload) => {
5141
this.handlers.forEach((listener) => listener(payload))

packages/vite/src/node/ssr/runtime/serverModuleRunner.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import type {
66
ModuleRunnerHmr,
77
ModuleRunnerOptions,
88
} from 'vite/module-runner'
9-
import type { ViteDevServer } from '../../server'
109
import type { DevEnvironment } from '../../server/environment'
10+
import type { ServerHMRChannel } from '../../server/hmr'
1111
import { ServerHMRConnector } from './serverHmrConnector'
1212

1313
/**
@@ -34,10 +34,10 @@ export interface ServerModuleRunnerOptions
3434
}
3535

3636
function createHMROptions(
37-
server: ViteDevServer,
37+
environment: DevEnvironment,
3838
options: ServerModuleRunnerOptions,
3939
) {
40-
if (server.config.server.hmr === false || options.hmr === false) {
40+
if (environment.config.server.hmr === false || options.hmr === false) {
4141
return false
4242
}
4343
if (options.hmr?.connection) {
@@ -46,7 +46,8 @@ function createHMROptions(
4646
logger: options.hmr.logger,
4747
}
4848
}
49-
const connection = new ServerHMRConnector(server)
49+
if (!('api' in environment.hot)) return false
50+
const connection = new ServerHMRConnector(environment.hot as ServerHMRChannel)
5051
return {
5152
connection,
5253
logger: options.hmr?.logger,
@@ -82,11 +83,10 @@ function resolveSourceMapOptions(options: ServerModuleRunnerOptions) {
8283
* @experimental
8384
*/
8485
export function createServerModuleRunner(
85-
server: ViteDevServer,
8686
environment: DevEnvironment,
8787
options: ServerModuleRunnerOptions = {},
8888
): ModuleRunner {
89-
const hmr = createHMROptions(server, options)
89+
const hmr = createHMROptions(environment, options)
9090
return new ModuleRunner(
9191
{
9292
...options,

packages/vite/src/node/ssr/ssrModuleLoader.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export async function ssrLoadModule(
2121
const runner =
2222
server._ssrCompatModuleRunner ||
2323
(server._ssrCompatModuleRunner = createServerModuleRunner(
24-
server,
2524
server.environments.ssr,
2625
{
2726
sourcemapInterceptor: false,

playground/environment-react-ssr/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function vitePluginSsrMiddleware({
6666
name: vitePluginSsrMiddleware.name,
6767

6868
configureServer(server) {
69-
const runner = createServerModuleRunner(server, server.environments.ssr)
69+
const runner = createServerModuleRunner(server.environments.ssr)
7070
const handler: Connect.NextHandleFunction = async (req, res, next) => {
7171
try {
7272
const mod = await runner.import(entry)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ async function setupModuleRunner(
11401140
// @ts-expect-error not typed for HMR
11411141
globalThis.log = (...msg) => logger.log(...msg)
11421142

1143-
runner = createServerModuleRunner(server, server.environments.ssr, {
1143+
runner = createServerModuleRunner(server.environments.ssr, {
11441144
hmr: {
11451145
logger,
11461146
},

playground/ssr-html/test-network-imports.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@ async function runTest(userRunner) {
1212
})
1313
let mod
1414
if (userRunner) {
15-
const runner = await createServerModuleRunner(
16-
server,
17-
server.environments.ssr,
18-
{
19-
hmr: false,
20-
},
21-
)
15+
const runner = await createServerModuleRunner(server.environments.ssr, {
16+
hmr: false,
17+
})
2218
mod = await runner.import('/src/network-imports.js')
2319
} else {
2420
mod = await server.ssrLoadModule('/src/network-imports.js')

playground/ssr-html/test-stacktrace-runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const server = await createServer({
1313
},
1414
})
1515

16-
const runner = await createServerModuleRunner(server, server.environments.ssr, {
16+
const runner = await createServerModuleRunner(server.environments.ssr, {
1717
sourcemapInterceptor: 'prepareStackTrace',
1818
})
1919

0 commit comments

Comments
 (0)