Skip to content

Commit bdd590b

Browse files
authored
module: use kNodeModulesRE to detect node_modules
This is faster and more consistent with other places using the regular expression to detect node_modules. PR-URL: #55243 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Marco Ippolito <[email protected]>
1 parent d2ad9b4 commit bdd590b

File tree

4 files changed

+11
-16
lines changed

4 files changed

+11
-16
lines changed

lib/internal/modules/cjs/loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ const { pathToFileURL, fileURLToPath, isURL } = require('internal/url');
124124
const {
125125
pendingDeprecate,
126126
emitExperimentalWarning,
127+
isUnderNodeModules,
127128
kEmptyObject,
128129
setOwnProperty,
129130
getLazy,
@@ -146,7 +147,6 @@ const { safeGetenv } = internalBinding('credentials');
146147
const {
147148
getCjsConditions,
148149
initializeCjsConditions,
149-
isUnderNodeModules,
150150
loadBuiltinModule,
151151
makeRequireFunction,
152152
setHasStartedUserCJSExecution,

lib/internal/modules/esm/load.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
const {
44
RegExpPrototypeExec,
55
} = primordials;
6-
const { kEmptyObject } = require('internal/util');
6+
const {
7+
isUnderNodeModules,
8+
kEmptyObject,
9+
} = require('internal/util');
710

811
const { defaultGetFormat } = require('internal/modules/esm/get_format');
912
const { validateAttributes, emitImportAssertionWarning } = require('internal/modules/esm/assert');
@@ -14,9 +17,6 @@ const defaultType =
1417
getOptionValue('--experimental-default-type');
1518

1619
const { Buffer: { from: BufferFrom } } = require('buffer');
17-
const {
18-
isUnderNodeModules,
19-
} = require('internal/modules/helpers');
2020

2121
const { URL } = require('internal/url');
2222
const {

lib/internal/modules/helpers.js

-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const {
44
ArrayPrototypeForEach,
5-
ArrayPrototypeIncludes,
65
ObjectDefineProperty,
76
ObjectFreeze,
87
ObjectPrototypeHasOwnProperty,
@@ -11,7 +10,6 @@ const {
1110
StringPrototypeCharCodeAt,
1211
StringPrototypeIncludes,
1312
StringPrototypeSlice,
14-
StringPrototypeSplit,
1513
StringPrototypeStartsWith,
1614
} = primordials;
1715
const {
@@ -387,13 +385,6 @@ function stripTypeScriptTypes(source, filename) {
387385
return `${code}\n\n//# sourceURL=${filename}`;
388386
}
389387

390-
function isUnderNodeModules(filename) {
391-
const resolvedPath = path.resolve(filename);
392-
const normalizedPath = path.normalize(resolvedPath);
393-
const splitPath = StringPrototypeSplit(normalizedPath, path.sep);
394-
return ArrayPrototypeIncludes(splitPath, 'node_modules');
395-
}
396-
397388
/** @type {import('internal/util/types')} */
398389
let _TYPES = null;
399390
/**
@@ -494,7 +485,6 @@ module.exports = {
494485
getCjsConditions,
495486
getCompileCacheDir,
496487
initializeCjsConditions,
497-
isUnderNodeModules,
498488
loadBuiltinModule,
499489
makeRequireFunction,
500490
normalizeReferrerURL,

lib/internal/util.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,10 @@ function spliceOne(list, index) {
484484

485485
const kNodeModulesRE = /^(?:.*)[\\/]node_modules[\\/]/;
486486

487+
function isUnderNodeModules(filename) {
488+
return filename && (RegExpPrototypeExec(kNodeModulesRE, filename) !== null);
489+
}
490+
487491
let getStructuredStackImpl;
488492

489493
function lazyGetStructuredStack() {
@@ -531,7 +535,7 @@ function isInsideNodeModules() {
531535
) {
532536
continue;
533537
}
534-
return RegExpPrototypeExec(kNodeModulesRE, filename) !== null;
538+
return isUnderNodeModules(filename);
535539
}
536540
}
537541
return false;
@@ -911,6 +915,7 @@ module.exports = {
911915
guessHandleType,
912916
isError,
913917
isInsideNodeModules,
918+
isUnderNodeModules,
914919
isMacOS,
915920
isWindows,
916921
join,

0 commit comments

Comments
 (0)