@@ -194,10 +194,9 @@ function Module(id = '', parent) {
194194 if ( policy ?. manifest ) {
195195 const moduleURL = pathToFileURL ( id ) ;
196196 redirects = policy . manifest . getDependencyMapper ( moduleURL ) ;
197+ // TODO(rafaelgss): remove the necessity of this branch
198+ setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
197199 }
198- setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
199- // Loads a module at the given file path. Returns that module's
200- // `exports` property.
201200 this [ require_private_symbol ] = internalRequire ;
202201}
203202
@@ -990,6 +989,23 @@ Module.prototype.load = function(filename) {
990989 ESMLoader . cjsCache . set ( this , exports ) ;
991990} ;
992991
992+ // Loads a module at the given file path. Returns that module's
993+ // `exports` property.
994+ // When using the experimental policy mechanism this function is overridden
995+ Module . prototype . require = function ( id ) {
996+ validateString ( id , 'id' ) ;
997+ if ( id === '' ) {
998+ throw new ERR_INVALID_ARG_VALUE ( 'id' , id ,
999+ 'must be a non-empty string' ) ;
1000+ }
1001+ requireDepth ++ ;
1002+ try {
1003+ return Module . _load ( id , this , /* isMain */ false ) ;
1004+ } finally {
1005+ requireDepth -- ;
1006+ }
1007+ } ;
1008+
9931009// Resolved path to process.argv[1] will be lazily placed here
9941010// (needed for setting breakpoint when called with --inspect-brk)
9951011let resolvedArgv ;
@@ -1050,10 +1066,12 @@ function wrapSafe(filename, content, cjsModuleInstance) {
10501066// Returns exception, if any.
10511067Module . prototype . _compile = function ( content , filename ) {
10521068 let moduleURL ;
1053- if ( policy ?. manifest ) {
1069+ let redirects ;
1070+ const manifest = policy ?. manifest ;
1071+ if ( manifest ) {
10541072 moduleURL = pathToFileURL ( filename ) ;
1055- policy . manifest . getDependencyMapper ( moduleURL ) ;
1056- policy . manifest . assertIntegrity ( moduleURL , content ) ;
1073+ redirects = manifest . getDependencyMapper ( moduleURL ) ;
1074+ manifest . assertIntegrity ( moduleURL , content ) ;
10571075 }
10581076
10591077 maybeCacheSourceMap ( filename , content , this ) ;
@@ -1083,17 +1101,18 @@ Module.prototype._compile = function(content, filename) {
10831101 }
10841102 }
10851103 const dirname = path . dirname ( filename ) ;
1104+ const require = makeRequireFunction ( this , redirects ) ;
10861105 let result ;
10871106 const exports = this . exports ;
10881107 const thisValue = exports ;
10891108 const module = this ;
10901109 if ( requireDepth === 0 ) statCache = new SafeMap ( ) ;
10911110 if ( inspectorWrapper ) {
10921111 result = inspectorWrapper ( compiledWrapper , thisValue , exports ,
1093- module . require , module , filename , dirname ) ;
1112+ require , module , filename , dirname ) ;
10941113 } else {
10951114 result = ReflectApply ( compiledWrapper , thisValue ,
1096- [ exports , module . require , module , filename , dirname ] ) ;
1115+ [ exports , require , module , filename , dirname ] ) ;
10971116 }
10981117 hasLoadedAnyUserCJSModule = true ;
10991118 if ( requireDepth === 0 ) statCache = null ;
0 commit comments