@@ -96,6 +96,12 @@ const path = require('path');
96
96
const { sep } = path ;
97
97
const { internalModuleStat } = internalBinding ( 'fs' ) ;
98
98
const { safeGetenv } = internalBinding ( 'credentials' ) ;
99
+ const {
100
+ privateSymbols : {
101
+ require_private_symbol,
102
+ } ,
103
+ } = internalBinding ( 'util' ) ;
104
+
99
105
const {
100
106
getCjsConditions,
101
107
initializeCjsConditions,
@@ -151,6 +157,20 @@ let requireDepth = 0;
151
157
let statCache = null ;
152
158
let isPreloading = false ;
153
159
160
+ function internalRequire ( module , id ) {
161
+ validateString ( id , 'id' ) ;
162
+ if ( id === '' ) {
163
+ throw new ERR_INVALID_ARG_VALUE ( 'id' , id ,
164
+ 'must be a non-empty string' ) ;
165
+ }
166
+ requireDepth ++ ;
167
+ try {
168
+ return Module . _load ( id , module , /* isMain */ false ) ;
169
+ } finally {
170
+ requireDepth -- ;
171
+ }
172
+ }
173
+
154
174
function stat ( filename ) {
155
175
filename = path . toNamespacedPath ( filename ) ;
156
176
if ( statCache !== null ) {
@@ -205,6 +225,16 @@ function Module(id = '', parent) {
205
225
this . filename = null ;
206
226
this . loaded = false ;
207
227
this . children = [ ] ;
228
+ let redirects ;
229
+ const manifest = policy ( ) ?. manifest ;
230
+ if ( manifest ) {
231
+ const moduleURL = pathToFileURL ( id ) ;
232
+ redirects = manifest . getDependencyMapper ( moduleURL ) ;
233
+ }
234
+ setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
235
+ // Loads a module at the given file path. Returns that module's
236
+ // `exports` property.
237
+ this [ require_private_symbol ] = internalRequire ;
208
238
}
209
239
210
240
Module . _cache = { __proto__ : null } ;
@@ -927,6 +957,7 @@ Module._load = function(request, parent, isMain) {
927
957
928
958
if ( isMain ) {
929
959
process . mainModule = module ;
960
+ setOwnProperty ( module . require , 'main' , process . mainModule ) ;
930
961
module . id = '.' ;
931
962
}
932
963
@@ -1113,24 +1144,6 @@ Module.prototype.load = function(filename) {
1113
1144
cascadedLoader . cjsCache . set ( this , exports ) ;
1114
1145
} ;
1115
1146
1116
-
1117
- // Loads a module at the given file path. Returns that module's
1118
- // `exports` property.
1119
- Module . prototype . require = function ( id ) {
1120
- validateString ( id , 'id' ) ;
1121
- if ( id === '' ) {
1122
- throw new ERR_INVALID_ARG_VALUE ( 'id' , id ,
1123
- 'must be a non-empty string' ) ;
1124
- }
1125
- requireDepth ++ ;
1126
- try {
1127
- return Module . _load ( id , this , /* isMain */ false ) ;
1128
- } finally {
1129
- requireDepth -- ;
1130
- }
1131
- } ;
1132
-
1133
-
1134
1147
// Resolved path to process.argv[1] will be lazily placed here
1135
1148
// (needed for setting breakpoint when called with --inspect-brk)
1136
1149
let resolvedArgv ;
@@ -1199,11 +1212,10 @@ function wrapSafe(filename, content, cjsModuleInstance) {
1199
1212
// Returns exception, if any.
1200
1213
Module . prototype . _compile = function ( content , filename ) {
1201
1214
let moduleURL ;
1202
- let redirects ;
1203
1215
const manifest = policy ( ) ?. manifest ;
1204
1216
if ( manifest ) {
1205
1217
moduleURL = pathToFileURL ( filename ) ;
1206
- redirects = manifest . getDependencyMapper ( moduleURL ) ;
1218
+ manifest . getDependencyMapper ( moduleURL ) ;
1207
1219
manifest . assertIntegrity ( moduleURL , content ) ;
1208
1220
}
1209
1221
@@ -1233,18 +1245,17 @@ Module.prototype._compile = function(content, filename) {
1233
1245
}
1234
1246
}
1235
1247
const dirname = path . dirname ( filename ) ;
1236
- const require = makeRequireFunction ( this , redirects ) ;
1237
1248
let result ;
1238
1249
const exports = this . exports ;
1239
1250
const thisValue = exports ;
1240
1251
const module = this ;
1241
1252
if ( requireDepth === 0 ) statCache = new SafeMap ( ) ;
1242
1253
if ( inspectorWrapper ) {
1243
1254
result = inspectorWrapper ( compiledWrapper , thisValue , exports ,
1244
- require , module , filename , dirname ) ;
1255
+ module . require , module , filename , dirname ) ;
1245
1256
} else {
1246
1257
result = ReflectApply ( compiledWrapper , thisValue ,
1247
- [ exports , require , module , filename , dirname ] ) ;
1258
+ [ exports , module . require , module , filename , dirname ] ) ;
1248
1259
}
1249
1260
hasLoadedAnyUserCJSModule = true ;
1250
1261
if ( requireDepth === 0 ) statCache = null ;
@@ -1422,7 +1433,7 @@ Module._preloadModules = function(requests) {
1422
1433
}
1423
1434
}
1424
1435
for ( let n = 0 ; n < requests . length ; n ++ )
1425
- parent . require ( requests [ n ] ) ;
1436
+ internalRequire ( parent , requests [ n ] ) ;
1426
1437
isPreloading = false ;
1427
1438
} ;
1428
1439
0 commit comments