@@ -230,10 +230,9 @@ function Module(id = '', parent) {
230
230
if ( manifest ) {
231
231
const moduleURL = pathToFileURL ( id ) ;
232
232
redirects = manifest . getDependencyMapper ( moduleURL ) ;
233
+ // TODO(rafaelgss): remove the necessity of this branch
234
+ setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
233
235
}
234
- setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
235
- // Loads a module at the given file path. Returns that module's
236
- // `exports` property.
237
236
this [ require_private_symbol ] = internalRequire ;
238
237
}
239
238
@@ -1144,6 +1143,23 @@ Module.prototype.load = function(filename) {
1144
1143
cascadedLoader . cjsCache . set ( this , exports ) ;
1145
1144
} ;
1146
1145
1146
+ // Loads a module at the given file path. Returns that module's
1147
+ // `exports` property.
1148
+ // Note: when using the experimental policy mechanism this function is overridden
1149
+ Module . prototype . require = function ( id ) {
1150
+ validateString ( id , 'id' ) ;
1151
+ if ( id === '' ) {
1152
+ throw new ERR_INVALID_ARG_VALUE ( 'id' , id ,
1153
+ 'must be a non-empty string' ) ;
1154
+ }
1155
+ requireDepth ++ ;
1156
+ try {
1157
+ return Module . _load ( id , this , /* isMain */ false ) ;
1158
+ } finally {
1159
+ requireDepth -- ;
1160
+ }
1161
+ } ;
1162
+
1147
1163
// Resolved path to process.argv[1] will be lazily placed here
1148
1164
// (needed for setting breakpoint when called with --inspect-brk)
1149
1165
let resolvedArgv ;
@@ -1212,10 +1228,11 @@ function wrapSafe(filename, content, cjsModuleInstance) {
1212
1228
// Returns exception, if any.
1213
1229
Module . prototype . _compile = function ( content , filename ) {
1214
1230
let moduleURL ;
1231
+ let redirects ;
1215
1232
const manifest = policy ( ) ?. manifest ;
1216
1233
if ( manifest ) {
1217
1234
moduleURL = pathToFileURL ( filename ) ;
1218
- manifest . getDependencyMapper ( moduleURL ) ;
1235
+ redirects = manifest . getDependencyMapper ( moduleURL ) ;
1219
1236
manifest . assertIntegrity ( moduleURL , content ) ;
1220
1237
}
1221
1238
@@ -1245,17 +1262,18 @@ Module.prototype._compile = function(content, filename) {
1245
1262
}
1246
1263
}
1247
1264
const dirname = path . dirname ( filename ) ;
1265
+ const require = makeRequireFunction ( this , redirects ) ;
1248
1266
let result ;
1249
1267
const exports = this . exports ;
1250
1268
const thisValue = exports ;
1251
1269
const module = this ;
1252
1270
if ( requireDepth === 0 ) statCache = new SafeMap ( ) ;
1253
1271
if ( inspectorWrapper ) {
1254
1272
result = inspectorWrapper ( compiledWrapper , thisValue , exports ,
1255
- module . require , module , filename , dirname ) ;
1273
+ require , module , filename , dirname ) ;
1256
1274
} else {
1257
1275
result = ReflectApply ( compiledWrapper , thisValue ,
1258
- [ exports , module . require , module , filename , dirname ] ) ;
1276
+ [ exports , require , module , filename , dirname ] ) ;
1259
1277
}
1260
1278
hasLoadedAnyUserCJSModule = true ;
1261
1279
if ( requireDepth === 0 ) statCache = null ;
0 commit comments