node.js require 实现机制初窥;pomelo代码分析5----------- pomel-loader模块
这篇主要介绍node.js的模块加载机制和代码分析。顺带提一下pomelo-loader。
下面我们来介绍pomelo的loader模块,
npmjs.org上面介绍如下:
pomelo中使用Convention over Configuration的形式管理工程目录,不同的功能按约定放在不同的目录下。pomelo-loader为pomelo提供了按目录加载模块的功能。
http://www.4shared.com/mp3/t6ROiYfHce/20140123.html
http://www.4shared.com/mp3/cpcsTlq6ce/20140123.html
http://www.4shared.com/mp3/iaTqzYFLba/20140123.html
http://www.4shared.com/mp3/-14swvGuce/20140123.html
http://www.4shared.com/mp3/WFqmpV-Yba/20140123.html
http://www.4shared.com/mp3/_QVEctJVce/20140123.html
http://www.4shared.com/mp3/GPdKbOu_ba/20140123.html
http://www.4shared.com/mp3/TTweYSSlce/20140123.html
http://www.4shared.com/mp3/zgRH3b0Cba/20140123.html
http://www.4shared.com/mp3/33rxJFX3ce/20140123.html
http://www.4shared.com/mp3/LIESTUwQba/20140123.html
http://www.4shared.com/mp3/CZhfa5mpba/20140123.html
http://www.4shared.com/mp3/QT1sFuWgba/20140123.html
http://www.4shared.com/mp3/s1M6XqAlba/20140123.html
http://www.4shared.com/mp3/ZorUwpN0ba/20140123.html
http://www.4shared.com/mp3/YkSqfYFGce/20140123.html
http://www.4shared.com/mp3/uYtS-VM-ba/20140123.html
http://www.4shared.com/mp3/NJewkiHbba/20140123.html
http://www.4shared.com/mp3/stnZvAyKba/20140123.html
http://www.4shared.com/mp3/AH26PuAjba/20140123.html
http://www.4shared.com/mp3/BgMKHWuhba/20140123.html
http://www.4shared.com/mp3/1CDYxo4Kce/20140123.html
http://www.4shared.com/mp3/C3GlnJEYce/20140123.html
http://www.4shared.com/mp3/13y_mDlsba/20140123.html
http://www.4shared.com/mp3/7Xxl7C-nce/20140123.html
http://www.4shared.com/mp3/yXdn67U9ba/20140123.html
http://www.4shared.com/mp3/YR7y41bDce/20140123.html
http://www.4shared.com/mp3/L_FW8UhUce/20140123.html
http://www.4shared.com/mp3/bF3g0unmce/20140123.html
http://www.4shared.com/mp3/Lir4oVPYba/20140123.html
http://www.4shared.com/mp3/NIZb0hm8ce/20140123.html
http://www.4shared.com/mp3/9Vi7gzQqce/20140123.html
http://www.4shared.com/mp3/8HI9Jd73ce/20140123.html
http://www.4shared.com/mp3/ZqvcomScba/20140123.html
http://www.4shared.com/mp3/3-O4iXjrba/20140123.html
http://www.4shared.com/mp3/iI1Yquidba/20140123.html
http://www.4shared.com/mp3/r-6jNf-yba/20140123.html
http://www.4shared.com/mp3/doPOkXLrba/20140123.html
http://www.4shared.com/mp3/ig474Kknce/20140123.html
代码如下:
129 var requireUncached = function(module){
130 delete require.cache[require.resolve(module)]
131 return require(module)
132 }
从这个模块的目录可以看到,实际上pomelo-loader是完全独立的模块不依赖于其他模块,而且lib下面只有一个js文件。而且这个js才132行。
基本功能就是扫描某个目录,看后缀名,最终调用require来循环加载相应文件而已。
http://www.4shared.com/mp3/JIwHe9dqba/20140123.html
http://www.4shared.com/mp3/ZmkB-zIEce/20140123.html
http://www.4shared.com/mp3/L4u3PCzjba/20140123.html
http://www.4shared.com/mp3/QJujI8Thba/20140123.html
http://www.4shared.com/mp3/PIMocrVpce/20140123.html
http://www.4shared.com/mp3/WwLIUqKkba/20140123.html
http://www.4shared.com/mp3/8QHcZt87ce/20140123.html
http://www.4shared.com/mp3/Zz5kUvUAba/20140123.html
http://www.4shared.com/mp3/U8RxwIHKba/20140123.html
http://www.4shared.com/mp3/9zIp9JF-ba/20140123.html
http://www.4shared.com/mp3/rNAc_n8Eba/20140123.html
http://www.4shared.com/mp3/qlBNiEfHba/20140123.html
http://www.4shared.com/mp3/p2uSISMhba/20140123.html
http://www.4shared.com/mp3/DK4TEfNwce/20140123.html
http://www.4shared.com/mp3/pePqLXFyba/20140123.html
http://www.4shared.com/mp3/GsNxiHbrba/20140123.html
http://www.4shared.com/mp3/F5cZcyO_ba/20140123.html
http://www.4shared.com/mp3/RXAi_T9Eba/20140123.html
http://www.4shared.com/mp3/ylNfebnqba/20140123.html
http://www.4shared.com/mp3/MwtBhCYaba/20140123.html
http://www.4shared.com/mp3/ZYjbU5RAba/20140123.html
http://www.4shared.com/mp3/NFIJ4Je9ba/20140123.html
http://www.4shared.com/mp3/g4NkorUsba/20140123.html
http://www.4shared.com/mp3/IC9_NM7Oce/20140123.html
http://www.4shared.com/mp3/0jn0IQONba/20140123.html
http://www.4shared.com/mp3/4vJpeGpace/20140123.html
http://www.4shared.com/mp3/soM2YqPlba/20140123.html
http://www.4shared.com/mp3/CTMp5GFnce/20140123.html
http://www.4shared.com/mp3/Bq0vrerdce/20140123.html
http://www.4shared.com/mp3/_NIZ2JO8ce/20140123.html
http://www.4shared.com/mp3/lEtZdC4_ba/20140123.html
http://www.4shared.com/mp3/p2nBPAsPce/20140123.html
http://www.4shared.com/mp3/prCTq-gJba/20140123.html
http://www.4shared.com/mp3/ivKlqGodce/20140123.html
我们真正要看的才刚刚开始:
try 大家已经配置好调试环境 ;
exception 可以看看我前面介绍node-inspector的ZZ文章。
final Node-inspector + chrome-debuger真是太tm好用了!! 牛!
我们用pomelo-rpc这个模块下面的server.js这个程序来启动调试的,大家完全可以随便弄个文件,只要它调用pomelo-loader的loader()函数即可。或者自己写2行代码的js来测试。太基础,就不展开了。有问题的童鞋直接M我吧。
OK,这里假设大家已经break到130行。并且Node-inspector一切运作正常。
这个时候大家鼠标放到require.cache 这个变量上,可以看到这个Object的内容,在我这里有几十条。这些都是当前上下文已经加载的module。
http://www.4shared.com/mp3/3AjMckLFce/20140123.html
http://www.4shared.com/mp3/DpBhEETMba/20140123.html
http://www.4shared.com/mp3/wY3qN1_yba/20140123.html
http://www.4shared.com/mp3/FJdMIiLhba/20140123.html
http://www.4shared.com/mp3/Fs8RTMwQba/20140123.html
http://www.4shared.com/mp3/LrM4nXzsba/20140123.html
http://www.4shared.com/mp3/dMGDXdt7ce/20140123.html
http://www.4shared.com/mp3/r2mi1QDSba/20140123.html
http://www.4shared.com/mp3/2MJ1Gajhba/20140123.html
http://www.4shared.com/mp3/0hcMiifbce/20140123.html
http://www.4shared.com/mp3/gr6AkwIHce/20140123.html
http://www.4shared.com/mp3/h9iY1jyCba/20140123.html
http://www.4shared.com/mp3/CxUDkdJXce/20140123.html
http://www.4shared.com/mp3/vM9S92wUba/20140123.html
http://www.4shared.com/mp3/JQFlEFU2ce/20140123.html
http://www.4shared.com/mp3/eG2nlf_rce/20140123.html
http://www.4shared.com/mp3/d8iUYjNBce/20140123.html
http://www.4shared.com/mp3/zxM9hb7qba/20140123.html
http://www.4shared.com/mp3/So7lvMEKce/20140123.html
http://www.4shared.com/mp3/259K_20Uba/20140123.html
http://www.4shared.com/mp3/EUhxCzbwba/20140123.html
http://www.4shared.com/mp3/rzYSh02ace/20140123.html
http://www.4shared.com/mp3/HBD82UQSce/20140123.html
http://www.4shared.com/mp3/yoEqdV_Vce/20140123.html
http://www.4shared.com/mp3/1TpQAlM1ce/20140123.html
http://www.4shared.com/mp3/9AVJvJu0ce/20140123.html
http://www.4shared.com/mp3/D0jx7zXDce/20140123.html
http://www.4shared.com/mp3/46_ACxc2ce/20140123.html
http://www.4shared.com/mp3/sT9A4wcTba/20140123.html
http://www.4shared.com/mp3/D7kPl61Hce/20140123.html
http://www.4shared.com/mp3/zdxvn9qsce/20140123.html
我们在把这个Object打印出来可以看到都是我们工程里面对应的js文件,也包括各个子module的。(很长很长,我剪切一部分出来看)
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/index.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/lib/rpc-client/client.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/lib/rpc-client/mailbox.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/lib/rpc-client/mailboxes/blackhole.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/lib/rpc-client/mailboxes/ws-mailbox.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/lib/rpc-client/mailstation.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/lib/rpc-client/router.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/lib/rpc-server/acceptor.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/lib/rpc-server/acceptors/tcp-acceptor.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/lib/rpc-server/acceptors/ws-acceptor.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/pomelo-logger/node_modules/log4js/lib/connect-logger.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/pomelo-logger/node_modules/log4js/lib/date_format.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/pomelo-logger/node_modules/log4js/lib/layouts.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/pomelo-logger/node_modules/log4js/lib/levels.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/pomelo-logger/node_modules/log4js/lib/log4js.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/pomelo-logger/node_modules/log4js/lib/logger.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/socket.io-client/bin/builder.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/socket.io-client/lib/events.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/socket.io-client/lib/io.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/socket.io-client/lib/json.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/socket.io-client/lib/namespace.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/socket.io-client/lib/parser.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/socket.io-client/lib/socket.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/socket.io-client/lib/transport.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/socket.io-client/lib/transports/websocket.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/socket.io-client/lib/transports/xhr-polling.js: Module
- /home/chenee/myCode/NodeJS/test/node_modules/pomelo-rpc/node_modules/socket.io-client/lib/transports/xhr.js: Module
我们step into这个调用会跑到一个叫做Module.js的文件里面。这个文件在chrome-debuger左边的Sources目录里面看不到它所在的目录,只能看到是处于顶层的。
其实这个就是Node的源码lib中间的文件,
http://www.4shared.com/mp3/AE_rjiqbce/20140123.html
http://www.4shared.com/mp3/X3bUUmNkce/20140123.html
http://www.4shared.com/mp3/eFxgisnHce/20140123.html
http://www.4shared.com/mp3/FprWJW3hce/20140123.html
http://www.4shared.com/mp3/9dAsWsaRba/20140123.html
http://www.4shared.com/mp3/ZwpMemface/20140123.html
http://www.4shared.com/mp3/bJjUMoNOce/20140123.html
http://www.4shared.com/mp3/D1MZ7L4Dce/20140123.html
http://www.4shared.com/mp3/u1OCA2ILba/20140123.html
http://www.4shared.com/mp3/xlaGpSZcba/20140123.html
http://www.4shared.com/mp3/LYCYyti1ce/20140123.html
http://www.4shared.com/mp3/HVoL3_08ce/20140123.html
http://www.4shared.com/mp3/fVy6wsQKba/20140123.html
http://www.4shared.com/mp3/O5uezXCvba/20140123.html
http://www.4shared.com/mp3/O9QF5MtTba/20140123.html
http://www.4shared.com/mp3/yoOin2DIce/20140123.html
http://www.4shared.com/mp3/WNosYpG7ce/20140123.html
http://www.4shared.com/mp3/FcZj4ionba/20140123.html
http://www.4shared.com/mp3/kzmvn_qYba/20140123.html
http://www.4shared.com/mp3/Ha5IwAKKba/20140123.html
http://www.4shared.com/mp3/SkkKNU0Oce/20140123.html
http://www.4shared.com/mp3/qXeXr9xtba/20140123.html
http://www.4shared.com/mp3/7ZQgWsnRce/20140123.html
http://www.4shared.com/mp3/UP1xDLrwce/20140123.html
http://www.4shared.com/mp3/JGkORQ1Pce/20140123.html
http://www.4shared.com/mp3/d9nyHRr3ba/20140123.html
http://www.4shared.com/mp3/mI9rAhG1ba/20140123.html
http://www.4shared.com/mp3/BCvBzDsvce/20140123.html
http://www.4shared.com/mp3/ryHbxa8ece/20140123.html
http://www.4shared.com/mp3/4_6uW2aRce/20140123.html
http://www.4shared.com/mp3/YyMhMHTyba/20140123.html
http://www.4shared.com/mp3/GBtq2SVmce/20140123.html
http://www.4shared.com/mp3/ZdAYFo4Mce/20140123.html
http://www.4shared.com/mp3/x7HoDqFgce/20140123.html
http://www.4shared.com/mp3/utlJYAKRba/20140123.html
http://www.4shared.com/mp3/dpWOa0f_ce/20140123.html
http://www.4shared.com/mp3/VdLdmHmsce/20140123.html
http://www.4shared.com/mp3/wGPLucfKba/20140123.html
http://www.4shared.com/mp3/2RF61pA8ce/20140123.html
http://www.4shared.com/mp3/cQlT06gSce/20140123.html
http://www.4shared.com/mp3/BaQ_AIRLba/20140123.html
http://www.4shared.com/mp3/dEW4b0z_ce/20140123.html
http://www.4shared.com/mp3/cMUxIJH3ba/20140123.html
http://www.4shared.com/mp3/L7YoNu_iba/20140123.html
http://www.4shared.com/mp3/Ati185iEce/20140123.html
http://www.4shared.com/mp3/2Hx_WDlBce/20140123.html
http://www.4shared.com/mp3/xISJ_I0Uba/20140123.html
http://www.4shared.com/mp3/D7S4eZjWce/20140123.html
http://www.4shared.com/mp3/Ve9Gh3ZWce/20140123.html
http://www.4shared.com/mp3/xaM-vrBuce/20140123.html
http://www.4shared.com/mp3/Ef-4w3P0ba/20140123.html
http://www.4shared.com/mp3/ksaJ4Be9ce/20140123.html
http://www.4shared.com/mp3/Ap4g2vTCce/20140123.html
http://www.4shared.com/mp3/35olHBE3ce/20140123.html
http://www.4shared.com/mp3/VbPTdc3Fce/20140123.html
http://www.4shared.com/mp3/vEfCjPsrba/20140123.html
http://www.4shared.com/mp3/oqpCMRI4ce/20140123.html
http://www.4shared.com/mp3/2xcQwiMece/20140123.html
http://www.4shared.com/mp3/RTUBBJY1ce/20140123.html
http://www.4shared.com/mp3/sw-7vNA0ba/20140123.html
http://www.4shared.com/mp3/grwitT9tce/20140123.html
http://www.4shared.com/mp3/P670S2iQba/20140123.html
http://www.4shared.com/mp3/DKdzN4Xice/20140123.html
http://www.4shared.com/mp3/ERXEhuZaba/20140123.html
- ok,这个文件大概500行,东西不多,其实就是Module这个类的实现:包括操作和记录而已。
- 这个文件看一遍,我们就对Node.js的module,export{},这些基本概念有了深入和透彻的理解。所以很值得看。
- 374 Module.prototype._compile = function(content, filename) {
- .......
- 439 var compiledWrapper = runInThisContext(wrapper, filename, true);
- .......
- }
- 455 var args = [self.exports, require, self, filename, dirname];
- 456 return compiledWrapper.apply(self.exports, args);
- (function (exports, require, module, __filename, __dirname) {
- //原文件内容
- };)
可以看出原先困惑我们的exports是在这里实现的。
OK真相大白,还不白的多看2眼代码,尤其是这个函数。
node.js require 实现机制初窥;pomelo代码分析5----------- pomel-loader模块相关推荐
- node.js require()缓存-可能无效?
本文翻译自:node.js require() cache - possible to invalidate? From the node.js documentation: 从node.js文档中: ...
- node中怎样将css导入到html,CSS无法使用Node.js加载到我的HTML代码中
我想通过Node.js使用express()函数在localhost:3000中将CSS添加到我的HTML中. 不幸的是,有些奇怪.我一步一步按照教程中的步骤,但仍然无法加载我的CSS.我的style ...
- JS逆向--PyExecJS基本用法--网易云音乐逆向思路,node.js安装教程,逆向思路,逆向分析,加密机制,RSA,AES加密算法,加密算法啊破解,js引擎,定位数据包,分析栈结构,无痕窗口
文章目录 前言 一.JS逆向以及PyExecJS模块介绍 1.JS逆向 2.PyEecJS 二.使用步骤 1.环境安装 安装PyExecJS模块 安装node.js开发环境(官网链接 https:// ...
- node.js require 自动执行脚本 并生成html,利用node.js实现自动生成前端项目组件的方法详解...
本文主要给大家介绍了关于利用node.js实现自动生成前端项目组件的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: 脚本编写背景 写这个小脚本的初衷是,项目本身添加一个组件太 ...
- node.js中exports与module.exports的区别分析
前言 关于Node.js中的exports和module.exports,很多时候都比较容易让人混淆,弄不清楚两者间的区别.那么我们就从头开始理清这两者之间的关系. 来源 在开发Node.js应用的时 ...
- node.js require 自动执行脚本 并生成html,从HTML页面执行Nodejs脚本?
小编典典 我使用普通的JS而非咖啡脚本,因此这是每个Fosco注释(称为server.js)的示例: var express = require('express'), list = require( ...
- node.js require 自动执行脚本 并生成html,nodejs 执行脚本并实时输出
接到需求 需要一个服务来执行shell脚本,要求可以实时打印shell脚本执行的过程,并看到脚本执行的结果. 明确任务目标: 这是一个web服务,需要执行shell脚本 当一个脚本执行的时候,再次发送 ...
- node.js学习总结:node.js的内置模块,模块化,npm与包 express,前后端身份认证 JWT认证机制
node.js学习总结 什么是node.js node.js的内置模块 fs系统模块 path路径模块 http模块 模块化 npm与包 express express路由 express+mysql ...
- node.js + express 初体验【hello world】
[node.js] 一个神奇的XX 呵呵 :) 不知道怎么形容他才好! [express] 是node.js 开发web应用程序的框架 开发环境:XP 大家共同进步吧 :) 一:前期准备: ...
- require()------node js
require() 是Node JS中的一个全局函数,通过该函数可以引入模块,JSON,或本地文件.可以从node_modules引入模块.可以使用相对路径. 引入本地模块或json文件,路径会根据当 ...
最新文章
- ECCV2020 | 北京大学提出RGB-D语义分割新网络,多模态信息融合
- 比特币现金之城——北昆士兰
- mfc调取摄像头显示并截图_前摄后录,让行车安全再次提升:70迈智能后视镜后摄像头体验...
- Linux下的I/O多路复用select,poll,epoll浅析
- oracle新建用户sql局域,oracle 新建数据库,及创建用户,表空间,sqlplus导入导出数据库...
- 插入排序---希尔插入排序算法(Javascript版)
- 童年的飞秋大门已徐徐向我关闭
- iOS 封装跑马灯和轮播效果
- HTML5第6章上机,第6章__HTML5的表单.ppt
- [转]模块化——Common规范及Node模块实现
- 在计算机上采用线性同余法,随机数生成算法 —— 线性同余法
- ITIL 2011服务管理与认证读书笔记一——IT服务管理与ITIL
- web项目对接钉钉扫码登录
- iOS锁屏控制音乐播放
- 坤宝德万达打造命运共同体万达茂天樾之夜.时光发声全国巡回演唱会南宁站圆满结束
- 华为watchgt2更新鸿蒙,华为 WATCH GT 2 Pro 发布:陶瓷背壳、支持血氧检测、新的鸿蒙应用等...
- Bear and Five Cards-Codeforces680A
- ABP应用开发(Step by Step)-下篇
- $.each()和$().each
- 功能: Form窗口最大化时,控件相对位置变化的问题 (学习日记2016-12-23)