Created
February 3, 2017 18:37
-
-
Save ghaiklor/405ed06d4bdaec5d49f6cf8821195ac3 to your computer and use it in GitHub Desktop.
Module._load sources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 (parent) { | |
debug('Module._load REQUEST %s parent: %s', request, parent.id); | |
} | |
var filename = Module._resolveFilename(request, parent, isMain); | |
var cachedModule = Module._cache[filename]; | |
if (cachedModule) { | |
return cachedModule.exports; | |
} | |
if (NativeModule.nonInternalExists(filename)) { | |
debug('load native module %s', request); | |
return NativeModule.require(filename); | |
} | |
var module = new Module(filename, parent); | |
if (isMain) { | |
process.mainModule = module; | |
module.id = '.'; | |
} | |
Module._cache[filename] = module; | |
tryModuleLoad(module, filename); | |
return module.exports; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment