Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Sep 24, 2024
1 parent a42744f commit ab90883
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export {
createRunnableDevEnvironment,
isRunnableDevEnvironment,
type RunnableDevEnvironment,
type RunnableDevEnvironmentContext,
} from './server/environments/runnableEnvironment'
export {
DevEnvironment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createServerModuleRunner } from '../../ssr/runtime/serverModuleRunner'
export function createRunnableDevEnvironment(
name: string,
config: ResolvedConfig,
context: DevEnvironmentContext,
context: RunnableDevEnvironmentContext,
): DevEnvironment {
if (context.hot == null) {
throw new Error(
Expand All @@ -32,7 +32,7 @@ export function createRunnableDevEnvironment(
}

export interface RunnableDevEnvironmentContext extends DevEnvironmentContext {
runner?: ModuleRunner
runner?: (environment: RunnableDevEnvironment) => ModuleRunner
}

export function isRunnableDevEnvironment(
Expand All @@ -50,7 +50,9 @@ class RunnableDevEnvironment extends DevEnvironment {
context: RunnableDevEnvironmentContext,
) {
super(name, config, context)
this.runner = context.runner || createServerModuleRunner(this)
this.runner = context.runner
? context.runner(this)
: createServerModuleRunner(this)
}

import<T = any>(url: string): Promise<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('running module runner inside a worker', () => {
dev: {
createEnvironment: (name, config) => {
return new DevEnvironment(name, config, {
runner: {
runnerOptions: {
transport: new RemoteEnvironmentTransport({
send: (data) => worker.postMessage(data),
onMessage: (handler) => worker.on('message', handler),
Expand Down
36 changes: 25 additions & 11 deletions playground/hmr-ssr/__tests__/hmr-ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import {
test,
vi,
} from 'vitest'
import type { InlineConfig, ViteDevServer } from 'vite'
import { createServer, createServerModuleRunner } from 'vite'
import type { InlineConfig, RunnableDevEnvironment, ViteDevServer } from 'vite'
import {
createRunnableDevEnvironment,
createServer,
createServerHotChannel,
createServerModuleRunner,
} from 'vite'
import type { ModuleRunner } from 'vite/module-runner'
import {
addFile,
Expand Down Expand Up @@ -1036,6 +1041,10 @@ async function setupModuleRunner(

globalThis.__HMR__ = initHmrState as any

const logger = new HMRMockLogger()
// @ts-expect-error not typed for HMR
globalThis.log = (...msg) => logger.log(...msg)

server = await createServer({
configFile: resolve(testDir, 'vite.config.ts'),
root: testDir,
Expand All @@ -1053,6 +1062,19 @@ async function setupModuleRunner(
},
preTransformRequests: false,
},
environments: {
ssr: {
dev: {
createEnvironment(name, config) {
return createRunnableDevEnvironment(name, config, {
runner: (env) =>
createServerModuleRunner(env, { hmr: { logger } }),
hot: createServerHotChannel(),
})
},
},
},
},
optimizeDeps: {
disabled: true,
noDiscovery: true,
Expand All @@ -1061,15 +1083,7 @@ async function setupModuleRunner(
...serverOptions,
})

const logger = new HMRMockLogger()
// @ts-expect-error not typed for HMR
globalThis.log = (...msg) => logger.log(...msg)

runner = createServerModuleRunner(server.environments.ssr, {
hmr: {
logger,
},
})
runner = (server.environments.ssr as RunnableDevEnvironment).runner

await waitForWatcher(server, waitForFile)

Expand Down

0 comments on commit ab90883

Please sign in to comment.