File tree Expand file tree Collapse file tree 3 files changed +18
-9
lines changed
packages/core/src/server/runner Expand file tree Collapse file tree 3 files changed +18
-9
lines changed Original file line number Diff line number Diff line change 1
- import vm from 'node:vm' ;
1
+ import type { ModuleLinker , SourceTextModule } from 'node:vm' ;
2
2
3
3
export const asModule = async (
4
4
something : Record < string , any > ,
5
5
context : Record < string , any > ,
6
6
unlinked ?: boolean ,
7
- ) : Promise < vm . SourceTextModule > => {
8
- if ( something instanceof vm . Module ) {
7
+ ) : Promise < SourceTextModule > => {
8
+ const { Module, SyntheticModule } = await import ( 'node:vm' ) ;
9
+
10
+ if ( something instanceof Module ) {
9
11
return something ;
10
12
}
11
13
12
14
const exports = [ ...new Set ( [ 'default' , ...Object . keys ( something ) ] ) ] ;
13
15
14
- const m = new vm . SyntheticModule (
16
+ const m = new SyntheticModule (
15
17
exports ,
16
18
( ) => {
17
19
for ( const name of exports ) {
@@ -22,8 +24,11 @@ export const asModule = async (
22
24
context,
23
25
} ,
24
26
) ;
27
+
25
28
if ( unlinked ) return m ;
26
- await m . link ( ( ( ) => { } ) as ( ) => vm . Module ) ;
29
+
30
+ await m . link ( ( ( ) => { } ) as unknown as ModuleLinker ) ;
31
+
27
32
// @ts -expect-error copy from webpack
28
33
if ( m . instantiate ) m . instantiate ( ) ;
29
34
await m . evaluate ( ) ;
Original file line number Diff line number Diff line change 1
1
import { createRequire } from 'node:module' ;
2
2
import path from 'node:path' ;
3
- import vm from 'node:vm' ;
4
3
import { BasicRunner } from './basic' ;
5
4
import type {
6
5
BasicGlobalContext ,
@@ -87,6 +86,7 @@ export class CommonJsRunner extends BasicRunner {
87
86
88
87
protected createCjsRequirer ( ) : RunnerRequirer {
89
88
const requireCache = Object . create ( null ) ;
89
+ const vm = require ( 'node:vm' ) as typeof import ( 'node:vm' ) ;
90
90
91
91
return ( currentDirectory , modulePath , context = { } ) => {
92
92
const file = context . file || this . getFile ( modulePath , currentDirectory ) ;
Original file line number Diff line number Diff line change
1
+ import { createRequire } from 'node:module' ;
1
2
import path from 'node:path' ;
2
3
import { fileURLToPath , pathToFileURL } from 'node:url' ;
3
- import vm , { type SourceTextModule } from 'node:vm' ;
4
- import { asModule } from './asModule' ;
5
-
4
+ import type { SourceTextModule } from 'node:vm' ;
6
5
import { color } from '../../helpers' ;
6
+ import { asModule } from './asModule' ;
7
7
import { CommonJsRunner } from './cjs' ;
8
8
import { EsmMode , type RunnerRequirer } from './type' ;
9
9
10
+ const require = createRequire ( import . meta. url ) ;
11
+
10
12
export class EsmRunner extends CommonJsRunner {
11
13
protected createRunner ( ) : void {
12
14
super . createRunner ( ) ;
@@ -38,6 +40,8 @@ export class EsmRunner extends CommonJsRunner {
38
40
protected createEsmRequirer ( ) : RunnerRequirer {
39
41
const esmCache = new Map < string , SourceTextModule > ( ) ;
40
42
const esmIdentifier = this . _options . name ;
43
+ const vm = require ( 'node:vm' ) as typeof import ( 'node:vm' ) ;
44
+
41
45
return ( currentDirectory , modulePath , context = { } ) => {
42
46
if ( ! vm . SourceTextModule ) {
43
47
throw new Error (
You can’t perform that action at this time.
0 commit comments