Skip to content

Commit d8c34ce

Browse files
joyeecheungruyadorno
authored andcommitted
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 Backport-PR-URL: #55217 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Refs: #52697
1 parent f1b9791 commit d8c34ce

File tree

4 files changed

+11
-15
lines changed

4 files changed

+11
-15
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

-9
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 {
@@ -380,12 +378,6 @@ function stripTypeScriptTypes(source, filename) {
380378
return `${code}\n\n//# sourceURL=${filename}`;
381379
}
382380

383-
function isUnderNodeModules(filename) {
384-
const resolvedPath = path.resolve(filename);
385-
const normalizedPath = path.normalize(resolvedPath);
386-
const splitPath = StringPrototypeSplit(normalizedPath, path.sep);
387-
return ArrayPrototypeIncludes(splitPath, 'node_modules');
388-
}
389381

390382
/**
391383
* Enable on-disk compiled cache for all user modules being complied in the current Node.js instance
@@ -486,7 +478,6 @@ module.exports = {
486478
getCjsConditions,
487479
getCompileCacheDir,
488480
initializeCjsConditions,
489-
isUnderNodeModules,
490481
loadBuiltinModule,
491482
makeRequireFunction,
492483
normalizeReferrerURL,

lib/internal/util.js

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

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

484+
function isUnderNodeModules(filename) {
485+
return filename && (RegExpPrototypeExec(kNodeModulesRE, filename) !== null);
486+
}
487+
484488
let getStructuredStackImpl;
485489

486490
function lazyGetStructuredStack() {
@@ -528,7 +532,7 @@ function isInsideNodeModules() {
528532
) {
529533
continue;
530534
}
531-
return RegExpPrototypeExec(kNodeModulesRE, filename) !== null;
535+
return isUnderNodeModules(filename);
532536
}
533537
}
534538
return false;
@@ -908,6 +912,7 @@ module.exports = {
908912
guessHandleType,
909913
isError,
910914
isInsideNodeModules,
915+
isUnderNodeModules,
911916
isMacOS,
912917
isWindows,
913918
join,

0 commit comments

Comments
 (0)