Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup: check internal modules first
  • Loading branch information
BridgeAR committed Jan 6, 2019
commit 5de8e71e31f33c29037309e32e3d06f8a6d0870a
31 changes: 11 additions & 20 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,19 +444,21 @@ Module._resolveLookupPaths = function(request, parent) {
};

const filenameCache = Object.create(null);

// Check the cache for the requested file.
// 1. If a module already exists in the cache: return its exports object.
// 2. If the module is native: call `NativeModule.require()` with the
// filename and return the result.
// 3. Otherwise, create a new module for the file and save it to the cache.
// Then have it load the file contents before returning its exports
// object.
Module._load = function(request, parent, isMain) {
if (NativeModule.nonInternalExists(request)) {
// Check for native modules first. If we find such name in the user module
// cache, bail out. This keeps the behavior backwards compatible. It was
// originally meant to allow mocking of whole modules by placing a native
// module name in there.
if (Module._cache[request] !== undefined)
return Module._cache[request].exports;
debug('load native module %s', request);
return NativeModule.require(request);
}

let identifier;
if (parent !== undefined) {
debug('Module._load REQUEST %s parent: %s', request, parent.id);

// Fast path for (lazy loaded) modules in the same directory. The indirect
// caching is required to allow cache invalidation without changing the old
// cache key names.
Expand All @@ -472,17 +474,6 @@ Module._load = function(request, parent, isMain) {
}
}

// Check for native modules first. If we find such name in the user module
// cache, bail out. This keeps the behavior backwards compatible. It was
// originally meant to allow mocking of whole modules by placing a native
// module name in there.
if (NativeModule.nonInternalExists(request)) {
if (Module._cache[request] !== undefined)
return Module._cache[request].exports;
debug('load native module %s', request);
return NativeModule.require(request);
}

const filename = Module._resolveFilename(request, parent, isMain);

const cachedModule = Module._cache[filename];
Expand Down