Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ function reportModuleNotFoundToWatchMode(basePath, extensions) {
}
}

const kPaths = Symbol('kPaths');
const lazyPathsGetter = {
__proto__: null,
enumerable: true,
get() { return this[kPaths] ?? Module._nodeModulePaths(this.path); },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could also just replace this.paths with a value property once computed so subsequent accesses are not invoking a getter again & the property type change is less observable from the user land.

set(v) {
// We set paths directly in various places.
this[kPaths] = v;
},
};


/** @type {Map<Module, Module>} */
const moduleParentCache = new SafeWeakMap();
/**
Expand All @@ -265,6 +277,11 @@ function Module(id = '', parent) {
this.loaded = false;
this.children = [];
let redirects;

// Lazily compute paths to save on memory. Defined as an own-property for
// backwards compatibility reasons.
ObjectDefineProperty(this, 'paths', lazyPathsGetter);

const manifest = policy()?.manifest;
if (manifest) {
const moduleURL = pathToFileURL(id);
Expand Down Expand Up @@ -856,9 +873,10 @@ Module._resolveLookupPaths = function(request, parent) {

/** @type {string[]} */
let paths;
if (parent?.paths?.length) {
const parentPaths = parent?.paths;
if (parentPaths?.length) {
paths = ArrayPrototypeSlice(modulePaths);
ArrayPrototypeUnshiftApply(paths, parent.paths);
ArrayPrototypeUnshiftApply(paths, parentPaths);
} else {
paths = modulePaths;
}
Expand Down Expand Up @@ -1083,7 +1101,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {

for (let i = 0; i < options.paths.length; i++) {
const path = options.paths[i];
fakeParent.paths = Module._nodeModulePaths(path);
fakeParent.path = path;
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);

for (let j = 0; j < lookupPaths.length; j++) {
Expand Down Expand Up @@ -1201,7 +1219,6 @@ Module.prototype.load = function(filename) {

assert(!this.loaded);
this.filename = filename;
this.paths = Module._nodeModulePaths(path.dirname(filename));

const extension = findLongestRegisteredExtension(filename);
// allow .mjs to be overridden
Expand Down Expand Up @@ -1492,7 +1509,6 @@ function createRequireFromPath(filename) {
const m = new Module(proxyPath);
m.filename = proxyPath;

m.paths = Module._nodeModulePaths(m.path);
return makeRequireFunction(m, null);
}

Expand Down Expand Up @@ -1579,6 +1595,7 @@ Module._preloadModules = function(requests) {
isPreloading = false;
throw e;
}
parent.paths = [];
}
for (let n = 0; n < requests.length; n++) {
internalRequire(parent, requests[n]);
Expand Down
1 change: 0 additions & 1 deletion lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,6 @@ function resolveAsCommonJS(specifier, parentURL) {
try {
const parent = fileURLToPath(parentURL);
const tmpModule = new CJSModule(parent, null);
tmpModule.paths = CJSModule._nodeModulePaths(parent);

let found = CJSModule._resolveFilename(specifier, tmpModule, false);

Expand Down
2 changes: 0 additions & 2 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ function cjsPreparseModuleExports(filename, source) {
if (!loaded) {
module = new CJSModule(filename);
module.filename = filename;
module.paths = CJSModule._nodeModulePaths(module.path);
CJSModule._cache[filename] = module;
}

Expand All @@ -410,7 +409,6 @@ function cjsPreparseModuleExports(filename, source) {

if (reexports.length) {
module.filename = filename;
module.paths = CJSModule._nodeModulePaths(module.path);
for (let i = 0; i < reexports.length; i++) {
const reexport = reexports[i];
let resolved;
Expand Down
1 change: 0 additions & 1 deletion lib/internal/util/embedding.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ function embedderRunCjs(contents) {

const customModule = new Module(filename, null);
customModule.filename = filename;
customModule.paths = Module._nodeModulePaths(customModule.path);

const customExports = customModule.exports;

Expand Down