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