node 模块化 require expores,简易实现原理。

news/2024/7/2 23:03:16

为了更好的理解 Node.js 的 require 实现机制,我实现了一个简易的版本。我们node index.js的时候就是require('./index.js'),话不多说我们直接上代码:

  • 目录
  • index.js
  • a.js
  • b.js
  • c.js

index.js

'use strict'function $require(filepath) {const fs = require('fs');const filename = __dirname + '/' + filepath; //引入的路径let code = fs.readFileSync(filename, 'utf8');//定义容器let module = {exports: {}};let exports = module.exports;//代码拼接code = '(function($require, module, exports, __dirname, filename) {' +code +'})($require, module, exports, __dirname, filename);'//执行代码eval(code);//返回模块return module.exports;
}const log = $require('a.js')
log.loga();
log.logb();
$require('c.js').logc();
复制代码

a.js

'use strict'
const b = $require('b.js');
module.exports.loga = function() {console.log('我是a');
}
module.exports.logb = b;复制代码

b.js

'use strict'module.exports = function() {console.log('我是a->b');
}复制代码

c.js

'use strict'
module.exports.logc = function() {console.log('我是c');
}复制代码

我们运行:试一下 node index,js 输出如下:

我是a
我是a->b
我是c
复制代码

现在我们的require 不带缓存,每一次都会读取文件,所以我们key value 形式缓存。

index.js修改如下:

'use strict'function $require(filepath) {const fs = require('fs');const filename = __dirname + '/' + filepath; //引入的路径//缓存$require.cache = $require.cache || {};if ($require.cache[filename]) {console.log(filepath + ':缓存了')return $require.cache[filename].exports;}let code = fs.readFileSync(filename, 'utf8');//定义容器let module = {exports: {}};let exports = module.exports;//代码拼接code = '(function($require, module, exports, __dirname, __filename) {' +code +'})($require, module, exports, __dirname, filename);'//执行代码eval(code);//缓存$require.cache[filename] = module;//返回模块return module.exports;
}
$require('c.js').logc();
$require('c.js').logc();
复制代码

输出如下:

我是c
c.js:缓存了
我是c
复制代码

git : https://github.com/hi363138911/node_require //具体源码分析 alinode:https://alinode.aliyun.com/blog/35

个人博客: http://www.liangtongzhuo.com

转载于:https://juejin.im/post/5a31f14951882506fd588f5b


http://lihuaxi.xjx100.cn/news/239486.html

相关文章

电子文件归档为什么非云不可

本文讲的是电子文件归档为什么非云不可华为云为企业搭建功能强大的电子文件归档系统平台,一站式满足文件存储与管理、协同分享、移动办公等不同的业务需求。打造安全、高效、便捷的文件归档环境,帮助企业节省运营成本,优化管理流程&#xff0…

react 快速上手开发_React中测试驱动开发的快速指南

react 快速上手开发by Michał Baranowski通过MichałBaranowski React中测试驱动开发的快速指南 (A quick guide to test-driven development in React) Following the principles of Test-Driven Development (TDD) when writing a front-end React app might seem more dif…

前端部分面试题整理,欢迎补充

1.ng中如何配置路由,$scope和$rootscope的原理ng中如何配置路由?1)使用内置路由模块ng-routevar app angular.module(ngRouteExample, [ngRoute]).controller(MainController, function($scope) {}).config(function($routeProvider, $locationProvider) {$routeP…

远程协助软件开发_这是我从事远程软件开发人员工作的主要技巧

远程协助软件开发by Colin Morgan通过科林摩根(Colin Morgan) 这是我从事远程软件开发人员工作的主要技巧 (Here are the top tips I’ve used to land a remote software developer job) Applying for a remote software developer job means you are voluntarily choosing t…

linux查找文件

find . | grep xyz 将当前目录下(包括子目录)的文件名中含有xyz的文件过滤出来 find . | xargs grep xyz 将当前目录下(包括子目录)的文件内容中含有xyz的行过滤出来 转载于:https://www.cnblogs.com/anovana/p/8036032.html

mysql查询解析过程_MySQL查询执行过程详解

查询是用户通过设置某些查询条件,从表或其他查询中选取全部或者部分数据,以表的形式显示数据供用户浏览。查询是一个独立的、功能强大的、具有计算功能和条件检索功能的数据库对象。MySQL数据库中,MySQL查询同样是数据库的核心操作&#xff0…

Java云托管服务的开支削减策略

\摘要\随着项目不断扩大,你需要将其迁移到更大的虚拟机上。但如果新虚拟机环境超出了你的需求则会产生额外开支。\相比虚拟机,容器具有更小的粒度,并且无需重启运行中的实例即可垂直扩展。\单体应用和历史遗留应用无需更改配置,即…

flask url构建_如何为生产构建构建Flask-RESTPlus Web服务

flask url构建by Greg Obinna由格雷格奥比纳(Greg Obinna) 如何为生产构建构建Flask-RESTPlus Web服务 (How to structure a Flask-RESTPlus web service for production builds) In this guide I’ll show you a step by step approach for structuring a Flask RESTPlus web…