@@ -50,7 +50,7 @@ function evalModule(source, print) {
50
50
return handleMainPromise ( loadESM ( ( loader ) => loader . eval ( source ) ) ) ;
51
51
}
52
52
53
- function evalScript ( name , body , breakFirstLine , print ) {
53
+ function evalScript ( name , body , breakFirstLine , print , shouldLoadESM = false ) {
54
54
const CJSModule = require ( 'internal/modules/cjs/loader' ) . Module ;
55
55
const { kVmBreakFirstLineSymbol } = require ( 'internal/util' ) ;
56
56
const { pathToFileURL } = require ( 'url' ) ;
@@ -62,36 +62,45 @@ function evalScript(name, body, breakFirstLine, print) {
62
62
module . filename = path . join ( cwd , name ) ;
63
63
module . paths = CJSModule . _nodeModulePaths ( cwd ) ;
64
64
65
+ const { handleMainPromise } = require ( 'internal/modules/run_main' ) ;
65
66
const asyncESM = require ( 'internal/process/esm_loader' ) ;
66
67
const baseUrl = pathToFileURL ( module . filename ) . href ;
68
+ const { loadESM } = asyncESM ;
69
+
70
+ const runScript = ( ) => {
71
+ // Create wrapper for cache entry
72
+ const script = `
73
+ globalThis.module = module;
74
+ globalThis.exports = exports;
75
+ globalThis.__dirname = __dirname;
76
+ globalThis.require = require;
77
+ return (main) => main();
78
+ ` ;
79
+ globalThis . __filename = name ;
80
+ RegExpPrototypeExec ( / ^ / , '' ) ; // Necessary to reset RegExp statics before user code runs.
81
+ const result = module . _compile ( script , `${ name } -wrapper` ) ( ( ) =>
82
+ require ( 'vm' ) . runInThisContext ( body , {
83
+ filename : name ,
84
+ displayErrors : true ,
85
+ [ kVmBreakFirstLineSymbol ] : ! ! breakFirstLine ,
86
+ importModuleDynamically ( specifier , _ , importAssertions ) {
87
+ const loader = asyncESM . esmLoader ;
88
+ return loader . import ( specifier , baseUrl , importAssertions ) ;
89
+ } ,
90
+ } ) ) ;
91
+ if ( print ) {
92
+ const { log } = require ( 'internal/console/global' ) ;
93
+ log ( result ) ;
94
+ }
67
95
68
- // Create wrapper for cache entry
69
- const script = `
70
- globalThis.module = module;
71
- globalThis.exports = exports;
72
- globalThis.__dirname = __dirname;
73
- globalThis.require = require;
74
- return (main) => main();
75
- ` ;
76
- globalThis . __filename = name ;
77
- RegExpPrototypeExec ( / ^ / , '' ) ; // Necessary to reset RegExp statics before user code runs.
78
- const result = module . _compile ( script , `${ name } -wrapper` ) ( ( ) =>
79
- require ( 'vm' ) . runInThisContext ( body , {
80
- filename : name ,
81
- displayErrors : true ,
82
- [ kVmBreakFirstLineSymbol ] : ! ! breakFirstLine ,
83
- importModuleDynamically ( specifier , _ , importAssertions ) {
84
- const loader = asyncESM . esmLoader ;
85
- return loader . import ( specifier , baseUrl , importAssertions ) ;
86
- } ,
87
- } ) ) ;
88
- if ( print ) {
89
- const { log } = require ( 'internal/console/global' ) ;
90
- log ( result ) ;
91
- }
96
+ if ( origModule !== undefined )
97
+ globalThis . module = origModule ;
98
+ } ;
92
99
93
- if ( origModule !== undefined )
94
- globalThis . module = origModule ;
100
+ if ( shouldLoadESM ) {
101
+ return handleMainPromise ( loadESM ( runScript ) ) ;
102
+ }
103
+ return runScript ( ) ;
95
104
}
96
105
97
106
const exceptionHandlerState = {
0 commit comments