@@ -224,10 +224,9 @@ function Module(id = '', parent) {
224
224
if ( policy ?. manifest ) {
225
225
const moduleURL = pathToFileURL ( id ) ;
226
226
redirects = policy . manifest . getDependencyMapper ( moduleURL ) ;
227
+ // TODO(rafaelgss): remove the necessity of this branch
228
+ setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
227
229
}
228
- setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
229
- // Loads a module at the given file path. Returns that module's
230
- // `exports` property.
231
230
this [ require_private_symbol ] = internalRequire ;
232
231
}
233
232
@@ -1080,6 +1079,23 @@ Module.prototype.load = function(filename) {
1080
1079
esmLoader . cjsCache . set ( this , exports ) ;
1081
1080
} ;
1082
1081
1082
+ // Loads a module at the given file path. Returns that module's
1083
+ // `exports` property.
1084
+ // Note: when using the experimental policy mechanism this function is overridden
1085
+ Module . prototype . require = function ( id ) {
1086
+ validateString ( id , 'id' ) ;
1087
+ if ( id === '' ) {
1088
+ throw new ERR_INVALID_ARG_VALUE ( 'id' , id ,
1089
+ 'must be a non-empty string' ) ;
1090
+ }
1091
+ requireDepth ++ ;
1092
+ try {
1093
+ return Module . _load ( id , this , /* isMain */ false ) ;
1094
+ } finally {
1095
+ requireDepth -- ;
1096
+ }
1097
+ } ;
1098
+
1083
1099
// Resolved path to process.argv[1] will be lazily placed here
1084
1100
// (needed for setting breakpoint when called with --inspect-brk)
1085
1101
let resolvedArgv ;
@@ -1127,10 +1143,12 @@ function wrapSafe(filename, content, cjsModuleInstance) {
1127
1143
// Returns exception, if any.
1128
1144
Module . prototype . _compile = function ( content , filename ) {
1129
1145
let moduleURL ;
1130
- if ( policy ?. manifest ) {
1146
+ let redirects ;
1147
+ const manifest = policy ?. manifest ;
1148
+ if ( manifest ) {
1131
1149
moduleURL = pathToFileURL ( filename ) ;
1132
- policy . manifest . getDependencyMapper ( moduleURL ) ;
1133
- policy . manifest . assertIntegrity ( moduleURL , content ) ;
1150
+ redirects = manifest . getDependencyMapper ( moduleURL ) ;
1151
+ manifest . assertIntegrity ( moduleURL , content ) ;
1134
1152
}
1135
1153
1136
1154
maybeCacheSourceMap ( filename , content , this ) ;
@@ -1160,17 +1178,18 @@ Module.prototype._compile = function(content, filename) {
1160
1178
}
1161
1179
}
1162
1180
const dirname = path . dirname ( filename ) ;
1181
+ const require = makeRequireFunction ( this , redirects ) ;
1163
1182
let result ;
1164
1183
const exports = this . exports ;
1165
1184
const thisValue = exports ;
1166
1185
const module = this ;
1167
1186
if ( requireDepth === 0 ) statCache = new SafeMap ( ) ;
1168
1187
if ( inspectorWrapper ) {
1169
1188
result = inspectorWrapper ( compiledWrapper , thisValue , exports ,
1170
- module . require , module , filename , dirname ) ;
1189
+ require , module , filename , dirname ) ;
1171
1190
} else {
1172
1191
result = ReflectApply ( compiledWrapper , thisValue ,
1173
- [ exports , module . require , module , filename , dirname ] ) ;
1192
+ [ exports , require , module , filename , dirname ] ) ;
1174
1193
}
1175
1194
hasLoadedAnyUserCJSModule = true ;
1176
1195
if ( requireDepth === 0 ) statCache = null ;
0 commit comments