-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce RunnableDevEnvironment (#18190)
- Loading branch information
1 parent
34041b9
commit fb292f2
Showing
8 changed files
with
128 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
packages/vite/src/node/server/environments/nodeEnvironment.ts
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
packages/vite/src/node/server/environments/runnableEnvironment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { createServerModuleRunner } from '../../ssr/runtime/serverModuleRunner' | ||
import type { HotChannel } from '../hmr' | ||
import { createServerHotChannel } from '../hmr' | ||
import type { Environment } from '../../environment' | ||
|
||
export function createRunnableDevEnvironment( | ||
name: string, | ||
config: ResolvedConfig, | ||
context: RunnableDevEnvironmentContext = {}, | ||
): DevEnvironment { | ||
if (context.hot == null) { | ||
context.hot = createServerHotChannel() | ||
} | ||
|
||
return new RunnableDevEnvironment(name, config, context) | ||
} | ||
|
||
export interface RunnableDevEnvironmentContext | ||
extends Omit<DevEnvironmentContext, 'hot'> { | ||
runner?: (environment: RunnableDevEnvironment) => ModuleRunner | ||
hot?: false | HotChannel | ||
} | ||
|
||
export function isRunnableDevEnvironment( | ||
environment: Environment, | ||
): environment is RunnableDevEnvironment { | ||
return environment instanceof RunnableDevEnvironment | ||
} | ||
|
||
class RunnableDevEnvironment extends DevEnvironment { | ||
private _runner: ModuleRunner | undefined | ||
private _runnerFactory: | ||
| ((environment: RunnableDevEnvironment) => ModuleRunner) | ||
| undefined | ||
|
||
constructor( | ||
name: string, | ||
config: ResolvedConfig, | ||
context: RunnableDevEnvironmentContext, | ||
) { | ||
super(name, config, context as DevEnvironmentContext) | ||
this._runnerFactory = context.runner | ||
} | ||
|
||
get runner(): ModuleRunner { | ||
if (this._runner) { | ||
return this._runner | ||
} | ||
if (this._runnerFactory) { | ||
this._runner = this._runnerFactory(this) | ||
return this._runner | ||
} | ||
return createServerModuleRunner(this) | ||
} | ||
} | ||
|
||
export type { RunnableDevEnvironment } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters