module: use kNodeModulesRE to detect node_modules · nodejs/node@d8c34ce · GitHub
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

4 files changed

Lines changed: 11 additions & 15 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 1 addition & 1 deletion

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 & 9 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 {
@@ -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

Lines changed: 6 additions & 1 deletion

0 commit comments

Comments
 (0)