@@ -235,10 +235,9 @@ function Module(id = '', parent) {
235
235
if ( policy ?. manifest ) {
236
236
const moduleURL = pathToFileURL ( id ) ;
237
237
redirects = policy . manifest . getDependencyMapper ( moduleURL ) ;
238
+ // TODO(rafaelgss): remove the necessity of this branch
239
+ setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
238
240
}
239
- setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
240
- // Loads a module at the given file path. Returns that module's
241
- // `exports` property.
242
241
this [ require_private_symbol ] = internalRequire ;
243
242
}
244
243
@@ -1128,6 +1127,23 @@ Module.prototype.load = function(filename) {
1128
1127
esmLoader . cjsCache . set ( this , exports ) ;
1129
1128
} ;
1130
1129
1130
+ // Loads a module at the given file path. Returns that module's
1131
+ // `exports` property.
1132
+ // Note: when using the experimental policy mechanism this function is overridden
1133
+ Module . prototype . require = function ( id ) {
1134
+ validateString ( id , 'id' ) ;
1135
+ if ( id === '' ) {
1136
+ throw new ERR_INVALID_ARG_VALUE ( 'id' , id ,
1137
+ 'must be a non-empty string' ) ;
1138
+ }
1139
+ requireDepth ++ ;
1140
+ try {
1141
+ return Module . _load ( id , this , /* isMain */ false ) ;
1142
+ } finally {
1143
+ requireDepth -- ;
1144
+ }
1145
+ } ;
1146
+
1131
1147
// Resolved path to process.argv[1] will be lazily placed here
1132
1148
// (needed for setting breakpoint when called with --inspect-brk)
1133
1149
let resolvedArgv ;
@@ -1191,10 +1207,12 @@ function wrapSafe(filename, content, cjsModuleInstance) {
1191
1207
// Returns exception, if any.
1192
1208
Module . prototype . _compile = function ( content , filename ) {
1193
1209
let moduleURL ;
1194
- if ( policy ?. manifest ) {
1210
+ let redirects ;
1211
+ const manifest = policy ?. manifest ;
1212
+ if ( manifest ) {
1195
1213
moduleURL = pathToFileURL ( filename ) ;
1196
- policy . manifest . getDependencyMapper ( moduleURL ) ;
1197
- policy . manifest . assertIntegrity ( moduleURL , content ) ;
1214
+ redirects = manifest . getDependencyMapper ( moduleURL ) ;
1215
+ manifest . assertIntegrity ( moduleURL , content ) ;
1198
1216
}
1199
1217
1200
1218
const compiledWrapper = wrapSafe ( filename , content , this ) ;
@@ -1223,17 +1241,18 @@ Module.prototype._compile = function(content, filename) {
1223
1241
}
1224
1242
}
1225
1243
const dirname = path . dirname ( filename ) ;
1244
+ const require = makeRequireFunction ( this , redirects ) ;
1226
1245
let result ;
1227
1246
const exports = this . exports ;
1228
1247
const thisValue = exports ;
1229
1248
const module = this ;
1230
1249
if ( requireDepth === 0 ) statCache = new SafeMap ( ) ;
1231
1250
if ( inspectorWrapper ) {
1232
1251
result = inspectorWrapper ( compiledWrapper , thisValue , exports ,
1233
- module . require , module , filename , dirname ) ;
1252
+ require , module , filename , dirname ) ;
1234
1253
} else {
1235
1254
result = ReflectApply ( compiledWrapper , thisValue ,
1236
- [ exports , module . require , module , filename , dirname ] ) ;
1255
+ [ exports , require , module , filename , dirname ] ) ;
1237
1256
}
1238
1257
hasLoadedAnyUserCJSModule = true ;
1239
1258
if ( requireDepth === 0 ) statCache = null ;
0 commit comments