Skip to content

Commit

Permalink
feat: introduce RunnableDevEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Sep 24, 2024
1 parent e59e2ca commit 02e904b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 36 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { resolveBuildEnvironmentOptions, resolveBuilderOptions } from './build'
import type { ResolvedServerOptions, ServerOptions } from './server'
import { resolveServerOptions } from './server'
import { DevEnvironment } from './server/environment'
import { createNodeDevEnvironment } from './server/environments/nodeEnvironment'
import { createRunnableDevEnvironment } from './server/environments/runnableEnvironment'
import { createServerHotChannel } from './server/hmr'
import type { WebSocketServer } from './server/ws'
import type { PreviewOptions, ResolvedPreviewOptions } from './preview'
Expand Down Expand Up @@ -213,7 +213,7 @@ function defaultCreateSsrDevEnvironment(
name: string,
config: ResolvedConfig,
): DevEnvironment {
return createNodeDevEnvironment(name, config, {
return createRunnableDevEnvironment(name, config, {
hot: createServerHotChannel(),
})
}
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export { transformWithEsbuild } from './plugins/esbuild'
export { buildErrorMessage } from './server/middlewares/error'

export { RemoteEnvironmentTransport } from './server/environmentTransport'
export { createNodeDevEnvironment } from './server/environments/nodeEnvironment'
export {
createRunnableDevEnvironment,
isRunnableDevEnvironment,
type RunnableDevEnvironment,
} from './server/environments/runnableEnvironment'
export {
DevEnvironment,
type DevEnvironmentContext,
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/server/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import type { RemoteEnvironmentTransport } from './environmentTransport'
export interface DevEnvironmentContext {
hot: false | HotChannel
options?: EnvironmentOptions
runner?: FetchModuleOptions & {
runnerOptions?: FetchModuleOptions & {
transport?: RemoteEnvironmentTransport
}
depsOptimizer?: DepsOptimizer
Expand Down Expand Up @@ -123,8 +123,8 @@ export class DevEnvironment extends BaseEnvironment {
this._onCrawlEndCallbacks.forEach((cb) => cb())
})

this._ssrRunnerOptions = context.runner ?? {}
context.runner?.transport?.register(this)
this._ssrRunnerOptions = context.runnerOptions ?? {}
context.runnerOptions?.transport?.register(this)

this.hot.on('vite:invalidate', async ({ path, message }) => {
invalidateModule(this, {
Expand Down
30 changes: 0 additions & 30 deletions packages/vite/src/node/server/environments/nodeEnvironment.ts

This file was deleted.

61 changes: 61 additions & 0 deletions packages/vite/src/node/server/environments/runnableEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { ModuleRunner } from 'vite/module-runner'
import type { ResolvedConfig } from '../../config'
import type { DevEnvironmentContext } from '../environment'
import { DevEnvironment } from '../environment'
import { asyncFunctionDeclarationPaddingLineCount } from '../../../shared/utils'
import { createServerModuleRunner } from '../../ssr/runtime/serverModuleRunner'

export function createRunnableDevEnvironment(
name: string,
config: ResolvedConfig,
context: DevEnvironmentContext,
): DevEnvironment {
if (context.hot == null) {
throw new Error(
'`hot` is a required option. Either explicitly opt out of HMR by setting `hot: false` or provide a hot channel.',
)
}

return new RunnableDevEnvironment(name, config, {
...context,
runnerOptions: {
processSourceMap(map) {
// this assumes that "new AsyncFunction" is used to create the module
return Object.assign({}, map, {
mappings:
';'.repeat(asyncFunctionDeclarationPaddingLineCount) + map.mappings,
})
},
...context.runnerOptions,
},
})
}

export interface RunnableDevEnvironmentContext extends DevEnvironmentContext {
runner?: ModuleRunner
}

export function isRunnableDevEnvironment(
environment: DevEnvironment,
): environment is RunnableDevEnvironment {
return environment instanceof RunnableDevEnvironment
}

class RunnableDevEnvironment extends DevEnvironment {
public readonly runner: ModuleRunner

constructor(
name: string,
config: ResolvedConfig,
context: RunnableDevEnvironmentContext,
) {
super(name, config, context)
this.runner = context.runner || createServerModuleRunner(this)
}

import<T = any>(url: string): Promise<T> {
return this.runner.import<T>(url)
}
}

export type { RunnableDevEnvironment }

0 comments on commit 02e904b

Please sign in to comment.