@@ -12,6 +12,9 @@ import {
12
12
// @ts -ignore: experimental, not added to the types
13
13
SourceTextModule ,
14
14
// @ts -ignore: experimental, not added to the types
15
+ SyntheticModule ,
16
+ Context as VMContext ,
17
+ // @ts -ignore: experimental, not added to the types
15
18
Module as VMModule ,
16
19
compileFunction ,
17
20
} from 'vm' ;
@@ -337,6 +340,12 @@ class Runtime {
337
340
338
341
invariant ( context ) ;
339
342
343
+ if ( this . _resolver . isCoreModule ( modulePath ) ) {
344
+ const core = await this . _importCoreModule ( modulePath , context ) ;
345
+ this . _esmoduleRegistry . set ( cacheKey , core ) ;
346
+ return core ;
347
+ }
348
+
340
349
const transformedFile = this . transformFile ( modulePath , {
341
350
isInternalModule : false ,
342
351
supportsDynamicImport : true ,
@@ -1030,6 +1039,24 @@ class Runtime {
1030
1039
return require ( moduleName ) ;
1031
1040
}
1032
1041
1042
+ private _importCoreModule ( moduleName : string , context : VMContext ) {
1043
+ const required = this . _requireCoreModule ( moduleName ) ;
1044
+
1045
+ return new SyntheticModule (
1046
+ [ 'default' , ...Object . keys ( required ) ] ,
1047
+ function ( ) {
1048
+ // @ts -ignore: TS doesn't know what `this` is
1049
+ this . setExport ( 'default' , required ) ;
1050
+ Object . entries ( required ) . forEach ( ( [ key , value ] ) => {
1051
+ // @ts -ignore: TS doesn't know what `this` is
1052
+ this . setExport ( key , value ) ;
1053
+ } ) ;
1054
+ } ,
1055
+ // should identifier be `node://${moduleName}`?
1056
+ { context, identifier : moduleName } ,
1057
+ ) ;
1058
+ }
1059
+
1033
1060
private _getMockedNativeModule ( ) : typeof nativeModule . Module {
1034
1061
if ( this . _moduleImplementation ) {
1035
1062
return this . _moduleImplementation ;
@@ -1064,13 +1091,20 @@ class Runtime {
1064
1091
// should we implement the class ourselves?
1065
1092
class Module extends nativeModule . Module { }
1066
1093
1094
+ Object . entries ( nativeModule . Module ) . forEach ( ( [ key , value ] ) => {
1095
+ // @ts -ignore
1096
+ Module [ key ] = value ;
1097
+ } ) ;
1098
+
1067
1099
Module . Module = Module ;
1068
1100
1069
1101
if ( 'createRequire' in nativeModule ) {
1070
1102
Module . createRequire = createRequire ;
1071
1103
}
1072
1104
if ( 'createRequireFromPath' in nativeModule ) {
1073
- Module . createRequireFromPath = ( filename : string | URL ) => {
1105
+ Module . createRequireFromPath = function createRequireFromPath (
1106
+ filename : string | URL ,
1107
+ ) {
1074
1108
if ( typeof filename !== 'string' ) {
1075
1109
const error = new TypeError (
1076
1110
`The argument 'filename' must be string. Received '${ filename } '.${
@@ -1087,7 +1121,7 @@ class Runtime {
1087
1121
} ;
1088
1122
}
1089
1123
if ( 'syncBuiltinESMExports' in nativeModule ) {
1090
- Module . syncBuiltinESMExports = ( ) => { } ;
1124
+ Module . syncBuiltinESMExports = function syncBuiltinESMExports ( ) { } ;
1091
1125
}
1092
1126
1093
1127
this . _moduleImplementation = Module ;
0 commit comments