资讯专栏INFORMATION COLUMN

require源码阅读

Batkid / 2575人阅读

require gitlab

mudule对象化

require最终会把每个模块都转化为对象

</>复制代码

  1. function Module(id, parent) {
  2. this.id = id;
  3. this.exports = {};
  4. this.parent = parent;
  5. updateChildren(parent, this, false);
  6. this.filename = null;
  7. this.loaded = false;
  8. this.children = [];
  9. }
require方法

用assert断言输入的合法性并调用_load方法还有一个调用_load的是

</>复制代码

  1. Module.runMain = function() {
  2. // Load the main module--the command line argument.
  3. Module._load(process.argv[1], null, true);
  4. // Handle any nextTicks added in the first tick of the program
  5. process._tickCallback();
  6. };

这个我不是特别确定,但基本确定是给node xxx.js 这条命令调用的

load方法

这里有很多是关于处理main的,核心的一段是

</>复制代码

  1. if (isMain) {
  2. process.mainModule = module;
  3. module.id = ".";
  4. }

这个佐证了上面runMain是给node xxx.js 这条命令调用的这个论点,另外main模块的id一定是 ".",并且parent一定是空。
另外在调用前会先查找模块缓存的是否存在。
以下代码为了简化删去main模块的处理

</>复制代码

  1. //创建没有原型的空对象
  2. Module._cache = Object.create(null);
  3. Module._pathCache = Object.create(null);
  4. Module._load = function(request, parent, isMain) {
  5. if (parent) {
  6. debug("Module._load REQUEST %s parent: %s", request, parent.id);
  7. }
  8. var filename = Module._resolveFilename(request, parent, isMain);
  9. //缓存中是否存在
  10. var cachedModule = Module._cache[filename];
  11. if (cachedModule) {
  12. updateChildren(parent, cachedModule, true);
  13. return cachedModule.exports;
  14. }
  15. //是否是native模块
  16. if (NativeModule.nonInternalExists(filename)) {
  17. debug("load native module %s", request);
  18. return NativeModule.require(filename);
  19. }
  20. //其他模块处理
  21. var module = new Module(filename, parent);
  22. Module._cache[filename] = module;
  23. tryModuleLoad(module, filename);
  24. return module.exports;
  25. };

这个部分说明了加载模块首先是判断模块名,之后是查找缓存,查找native 模块,然后是其他模块,最后的return是最为关键的,返回值永远是module的 exports

查找node_modules文件夹的规则

</>复制代码

  1. Module._nodeModulePaths = function(from) {
  2. // guarantee that "from" is absolute.
  3. from = path.resolve(from);
  4. // Return early not only to avoid unnecessary work, but to *avoid* returning
  5. // an array of two items for a root: [ "//node_modules", "/node_modules" ]
  6. if (from === "/")
  7. return ["/node_modules"];
  8. // note: this approach *only* works when the path is guaranteed
  9. // to be absolute. Doing a fully-edge-case-correct path.split
  10. // that works on both Windows and Posix is non-trivial.
  11. const paths = [];
  12. var p = 0;
  13. var last = from.length;
  14. for (var i = from.length - 1; i >= 0; --i) {
  15. const code = from.charCodeAt(i);
  16. if (code === 47//*/*/) {
  17. if (p !== nmLen)
  18. paths.push(from.slice(0, last) + "/node_modules");
  19. last = i;
  20. p = 0;
  21. } else if (p !== -1) {
  22. if (nmChars[p] === code) {
  23. ++p;
  24. } else {
  25. p = -1;
  26. }
  27. }
  28. }

从from开始逐层向上查找node_modules文件夹

Module._extensions

js,json,node,mjs
每个后缀的文件都有对应的打开方式
js 清除可能的BOM头后加载
json json Parse
node .node 这是C/C++编写的扩展文件,通过dlopen()方法加载最后编译生成的文件,可以当作是一个系统调用

findPath

这部分代码比较多,只看一段注释即可
// given a module name, and a list of paths to test, returns the first
// matching file in the following precedence.
//
// require("a.")
// -> a.
//
// require("a")
// -> a
// -> a.
// -> a/index.

package.json

node会去寻找路径中的package.json文件并且会加载其中的main,并放入packagecache中,用main中指定的文件再去确认绝对路径然后加载

</>复制代码

  1. function readPackage(requestPath) {
  2. const entry = packageMainCache[requestPath];
  3. if (entry)
  4. return entry;
  5. const jsonPath = path.resolve(requestPath, "package.json");
  6. const json = internalModuleReadFile(path.toNamespacedPath(jsonPath));
  7. if (json === undefined) {
  8. return false;
  9. }
  10. try {
  11. var pkg = packageMainCache[requestPath] = JSON.parse(json).main;
  12. } catch (e) {
  13. e.path = jsonPath;
  14. e.message = "Error parsing " + jsonPath + ": " + e.message;
  15. throw e;
  16. }
  17. return pkg;
  18. }
compile

如何查找到的文件基本清楚了,之后就是最常用的js的compile了
js模块的外层一定会被套上

</>复制代码

  1. Module.wrapper = [
  2. "(function (exports, require, module, __filename, __dirname) { ",
  3. "
  4. });"
  5. ];

之后涉及了断点的处理,是在vm模块中的Srcript对象中的runInThisContext

后面有一句说明了为什么node中的所有文件也可以拥有Module的各种方法和特性 require 包括那些并不是main的文件,另外所有的模块也是公用的模块缓存,利用Module require中的return Module._load(path, this, /* isMain */ false);把自己的this作为parent作为Module对象的parent
var require = internalModule.makeRequireFunction(this);
makeRequireFunction的代码

</>复制代码

  1. // Invoke with makeRequireFunction(module) where |module| is the Module object
  2. // to use as the context for the require() function.
  3. function makeRequireFunction(mod) {
  4. const Module = mod.constructor;
  5. function require(path) {
  6. try {
  7. exports.requireDepth += 1;
  8. return mod.require(path);
  9. } finally {
  10. exports.requireDepth -= 1;
  11. }
  12. }
  13. function resolve(request, options) {
  14. return Module._resolveFilename(request, mod, false, options);
  15. }
  16. require.resolve = resolve;
  17. function paths(request) {
  18. return Module._resolveLookupPaths(request, mod, true);
  19. }
  20. resolve.paths = paths;
  21. require.main = process.mainModule;
  22. // Enable support to add extra extension types.
  23. require.extensions = Module._extensions;
  24. require.cache = Module._cache;
  25. return require;
  26. }

执行compiledWrapper,对应wrapper插入的js
result = compiledWrapper.call(this.exports, this.exports, require, this, filename, dirname);
首先是compiledWrapper的this绑定在了Module自己exports上,自己的exports也作为了参数被注入,相当于隐式的赋值exports就是compiledWrapper中的exports了这个就成了对外唯一的输出了,其他的所有值都成了compiledWrapper的私有不会污染全局,require作为参数被注入,另外就是文件名 和路径的注入

从上述处理加载的过程我们可以发现只要有require node就会继续加载,另外CommonJs同步的特点也是在这体现出来的

对照学习

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/93763.html

相关文章

  • koa源码阅读之目录结构与辅助库相关

    摘要:从一个对象里面提取需要的属性这篇文章一直想写了还想起那一夜我看到白天的代码,实在太美了。 koa源码lib主要文件有 application.js context.js request.js response.js application.js koa主要的逻辑处理代码整个koa的处理 context.js 将req,res方法 挂载在这,生成ctx上下文对象 requests....

    sherlock221 评论0 收藏0
  • 阅读sea.js源码小结

    摘要:依赖信息是一个数组,比如上面的依赖数组是源码如下是利用正则解析依赖的一个函数时间出发函数主要看这个部分注释是防止拷贝该时间的回调函数,防止修改,困惑了一下。对的赋值需要同步执行,不能放在回调函数里。 sea.js想解决的问题 恼人的命名冲突 烦琐的文件依赖 对应带来的好处 Sea.js 带来的两大好处: 通过 exports 暴露接口。这意味着不需要命名空间了,更不需要全局变量。...

    chavesgu 评论0 收藏0
  • 一步步去阅读koa源码,整体架构分析

    摘要:阅读好的框架的源码有很多好处,从大神的视角去理解整个框架的设计思想。使用其实某个框架阅读源码的时候,首先我们要会去用这个框架,因为用了我们才知道,某个是怎么用,哪里有坑,哪里设计的精妙。 阅读好的框架的源码有很多好处,从大神的视角去理解整个框架的设计思想。大到架构设计,小到可取的命名风格,还有设计模式、实现某类功能使用到的数据结构和算法等等。 使用koa 其实某个框架阅读源码的时候,首...

    haoguo 评论0 收藏0
  • 一步步去阅读koa源码,整体架构分析

    摘要:阅读好的框架的源码有很多好处,从大神的视角去理解整个框架的设计思想。使用其实某个框架阅读源码的时候,首先我们要会去用这个框架,因为用了我们才知道,某个是怎么用,哪里有坑,哪里设计的精妙。 阅读好的框架的源码有很多好处,从大神的视角去理解整个框架的设计思想。大到架构设计,小到可取的命名风格,还有设计模式、实现某类功能使用到的数据结构和算法等等。 使用koa 其实某个框架阅读源码的时候,首...

    chaos_G 评论0 收藏0
  • Koa源码阅读笔记(2) -- compose

    摘要:于是抱着知其然也要知其所以然的想法,开始阅读的源代码。问题读源代码时,自然是带着诸多问题的。源代码如下在被处理完后,每当有新请求,便会调用,去处理请求。接下来会继续写一些阅读笔记,因为看的源代码确实是获益匪浅。 本笔记共四篇Koa源码阅读笔记(1) -- coKoa源码阅读笔记(2) -- composeKoa源码阅读笔记(3) -- 服务器の启动与请求处理Koa源码阅读笔记(4) -...

    roland_reed 评论0 收藏0

发表评论

0条评论

Batkid

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<