require gitlabjavascript
require最终会把每一个模块都转化为对象html
function Module(id, parent) { this.id = id; this.exports = {}; this.parent = parent; updateChildren(parent, this, false); this.filename = null; this.loaded = false; this.children = []; }
用assert断言输入的合法性并调用_load方法还有一个调用_load的是java
Module.runMain = function() { // Load the main module--the command line argument. Module._load(process.argv[1], null, true); // Handle any nextTicks added in the first tick of the program process._tickCallback(); };
这个我不是特别肯定,但基本肯定是给node xxx.js 这条命令调用的node
这里有不少是关于处理main的,核心的一段是git
if (isMain) { process.mainModule = module; module.id = '.'; }
这个佐证了上面runMain是给node xxx.js 这条命令调用的这个论点,另外main模块的id必定是 '.',而且parent必定是空。
另外在调用前会先查找模块缓存的是否存在。
如下代码为了简化删去main模块的处理github
//建立没有原型的空对象 Module._cache = Object.create(null); Module._pathCache = Object.create(null); 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) { updateChildren(parent, cachedModule, true); return cachedModule.exports; } //是不是native模块 if (NativeModule.nonInternalExists(filename)) { debug('load native module %s', request); return NativeModule.require(filename); } //其余模块处理 var module = new Module(filename, parent); Module._cache[filename] = module; tryModuleLoad(module, filename); return module.exports; };
这个部分说明了加载模块首先是判断模块名,以后是查找缓存,查找native 模块,而后是其余模块,最后的return是最为关键的,返回值永远是module的 exportsjson
Module._nodeModulePaths = function(from) { // guarantee that 'from' is absolute. from = path.resolve(from); // Return early not only to avoid unnecessary work, but to *avoid* returning // an array of two items for a root: [ '//node_modules', '/node_modules' ] if (from === '/') return ['/node_modules']; // note: this approach *only* works when the path is guaranteed // to be absolute. Doing a fully-edge-case-correct path.split // that works on both Windows and Posix is non-trivial. const paths = []; var p = 0; var last = from.length; for (var i = from.length - 1; i >= 0; --i) { const code = from.charCodeAt(i); if (code === 47//*/*/) { if (p !== nmLen) paths.push(from.slice(0, last) + '/node_modules'); last = i; p = 0; } else if (p !== -1) { if (nmChars[p] === code) { ++p; } else { p = -1; } } }
从from开始逐层向上查找node_modules文件夹缓存
js,json,node,mjs
每一个后缀的文件都有对应的打开方式
js 清除可能的BOM头后加载
json json Parse
node .node 这是C/C++编写的扩展文件,经过dlopen()方法加载最后编译生成的文件,能够看成是一个系统调用app
这部分代码比较多,只看一段注释便可
// given a module name, and a list of paths to test, returns the first
// matching file in the following precedence.
//
// require("a.<ext>")
// -> a.<ext>
//
// require("a")
// -> a
// -> a.<ext>
// -> a/index.<ext>gitlab
node会去寻找路径中的package.json文件而且会加载其中的main,并放入packagecache中,用main中指定的文件再去确认绝对路径而后加载
function readPackage(requestPath) { const entry = packageMainCache[requestPath]; if (entry) return entry; const jsonPath = path.resolve(requestPath, 'package.json'); const json = internalModuleReadFile(path.toNamespacedPath(jsonPath)); if (json === undefined) { return false; } try { var pkg = packageMainCache[requestPath] = JSON.parse(json).main; } catch (e) { e.path = jsonPath; e.message = 'Error parsing ' + jsonPath + ': ' + e.message; throw e; } return pkg; }
如何查找到的文件基本清楚了,以后就是最经常使用的js的compile了
js模块的外层必定会被套上
Module.wrapper = [ '(function (exports, require, module, __filename, __dirname) { ', '\n});' ];
以后涉及了断点的处理,是在vm模块中的Srcript对象中的runInThisContext
后面有一句说明了为何node中的全部文件也能够拥有Module的各类方法和特性 require 包括那些并非main的文件,另外全部的模块也是公用的模块缓存,利用Module require中的return Module._load(path, this, /* isMain */ false);
把本身的this做为parent做为Module对象的parentvar require = internalModule.makeRequireFunction(this);
makeRequireFunction的代码
// Invoke with makeRequireFunction(module) where |module| is the Module object // to use as the context for the require() function. function makeRequireFunction(mod) { const Module = mod.constructor; function require(path) { try { exports.requireDepth += 1; return mod.require(path); } finally { exports.requireDepth -= 1; } } function resolve(request, options) { return Module._resolveFilename(request, mod, false, options); } require.resolve = resolve; function paths(request) { return Module._resolveLookupPaths(request, mod, true); } resolve.paths = paths; require.main = process.mainModule; // Enable support to add extra extension types. require.extensions = Module._extensions; require.cache = Module._cache; return require; }
执行compiledWrapper,对应wrapper插入的jsresult = compiledWrapper.call(this.exports, this.exports, require, this, filename, dirname);
首先是compiledWrapper的this绑定在了Module本身exports上,本身的exports也做为了参数被注入,至关于隐式的赋值exports就是compiledWrapper中的exports了这个就成了对外惟一的输出了,其余的全部值都成了compiledWrapper的私有不会污染全局,require做为参数被注入,另外就是文件名 和路径的注入
从上述处理加载的过程咱们能够发现只要有require node就会继续加载,另外CommonJs同步的特色也是在这体现出来的