forked from vitejs/vite
-
Notifications
You must be signed in to change notification settings - Fork 0
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
- Loading branch information
1 parent
e59e2ca
commit 02e904b
Showing
5 changed files
with
71 additions
and
36 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
30 changes: 0 additions & 30 deletions
30
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 { 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 } |