Skip to content

Commit af03df3

Browse files
committed
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: nodejs#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 549a843 commit af03df3

File tree

4 files changed

+10
-16
lines changed

4 files changed

+10
-16
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ const { safeGetenv } = internalBinding('credentials');
146146
const {
147147
getCjsConditions,
148148
initializeCjsConditions,
149-
isUnderNodeModules,
150149
loadBuiltinModule,
151150
makeRequireFunction,
152151
setHasStartedUserCJSExecution,

lib/internal/modules/esm/load.js

Lines changed: 4 additions & 4 deletions
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

Lines changed: 0 additions & 10 deletions
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
/**
398389
* Enable on-disk compiled cache for all user modules being complied in the current Node.js instance
399390
* after this method is called.
@@ -493,7 +484,6 @@ module.exports = {
493484
getCjsConditions,
494485
getCompileCacheDir,
495486
initializeCjsConditions,
496-
isUnderNodeModules,
497487
loadBuiltinModule,
498488
makeRequireFunction,
499489
normalizeReferrerURL,

lib/internal/util.js

Lines changed: 6 additions & 1 deletion
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)