@@ -224,10 +224,9 @@ function Module(id = '', parent) {
224224 if ( policy ?. manifest ) {
225225 const moduleURL = pathToFileURL ( id ) ;
226226 redirects = policy . manifest . getDependencyMapper ( moduleURL ) ;
227+ // TODO(rafaelgss): remove the necessity of this branch
228+ setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
227229 }
228- setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
229- // Loads a module at the given file path. Returns that module's
230- // `exports` property.
231230 this [ require_private_symbol ] = internalRequire ;
232231}
233232
@@ -1080,6 +1079,23 @@ Module.prototype.load = function(filename) {
10801079 esmLoader . cjsCache . set ( this , exports ) ;
10811080} ;
10821081
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+
10831099// Resolved path to process.argv[1] will be lazily placed here
10841100// (needed for setting breakpoint when called with --inspect-brk)
10851101let resolvedArgv ;
@@ -1127,10 +1143,12 @@ function wrapSafe(filename, content, cjsModuleInstance) {
11271143// Returns exception, if any.
11281144Module . prototype . _compile = function ( content , filename ) {
11291145 let moduleURL ;
1130- if ( policy ?. manifest ) {
1146+ let redirects ;
1147+ const manifest = policy ?. manifest ;
1148+ if ( manifest ) {
11311149 moduleURL = pathToFileURL ( filename ) ;
1132- policy . manifest . getDependencyMapper ( moduleURL ) ;
1133- policy . manifest . assertIntegrity ( moduleURL , content ) ;
1150+ redirects = manifest . getDependencyMapper ( moduleURL ) ;
1151+ manifest . assertIntegrity ( moduleURL , content ) ;
11341152 }
11351153
11361154 maybeCacheSourceMap ( filename , content , this ) ;
@@ -1160,17 +1178,18 @@ Module.prototype._compile = function(content, filename) {
11601178 }
11611179 }
11621180 const dirname = path . dirname ( filename ) ;
1181+ const require = makeRequireFunction ( this , redirects ) ;
11631182 let result ;
11641183 const exports = this . exports ;
11651184 const thisValue = exports ;
11661185 const module = this ;
11671186 if ( requireDepth === 0 ) statCache = new SafeMap ( ) ;
11681187 if ( inspectorWrapper ) {
11691188 result = inspectorWrapper ( compiledWrapper , thisValue , exports ,
1170- module . require , module , filename , dirname ) ;
1189+ require , module , filename , dirname ) ;
11711190 } else {
11721191 result = ReflectApply ( compiledWrapper , thisValue ,
1173- [ exports , module . require , module , filename , dirname ] ) ;
1192+ [ exports , require , module , filename , dirname ] ) ;
11741193 }
11751194 hasLoadedAnyUserCJSModule = true ;
11761195 if ( requireDepth === 0 ) statCache = null ;
0 commit comments