源码如下:
let path = require("path");
let fs = require("fs");
let vm = require("vm");
class BNTangModule {
constructor(id) {
// 保存当前模块的绝对路径
this.id = id;
this.exports = {};
}
}
BNTangModule._cache = {};
BNTangModule._extensions = {
".js": function (module) {
// 1.读取JS代码
let script = fs.readFileSync(module.id);
// 2.将JS代码包裹到函数中
/*
(function (exports, require, module, __filename, __dirname) {
exports.name = "BNTang";
});
* */
let strScript = BNTangModule.wrapper[0] + script + BNTangModule.wrapper[1];
// 3.将字符串转换成JS代码
let jsScript = vm.runInThisContext(strScript);
// 4.执行转换之后的JS代码
// var args = [this.exports, require, module, filename, dirname];
// var result = compiledWrapper.call(this.exports, args);
jsScript.call(module.exports, module.exports);
},
".json": function (module) {
let json = fs.readFileSync(module.id);
module.exports = JSON.parse(json);
}
};
BNTangModule.wrapper = [
'(function (exports, require, module, __filename, __dirname) { ',
'\n});'
];
function bnTangRequire(filePath) {
// 1.将传入的相对路径转换成绝对路径
let absPath = path.join(__dirname, filePath);
// 2.尝试从缓存中获取当前的模块
let cachedModule = BNTangModule._cache[absPath];
if (cachedModule) {
return cachedModule.exports;
}
// 3.如果没有缓存就自己创建一个BNTangModule对象, 并缓存起来
let module = new BNTangModule(absPath);
BNTangModule._cache[absPath] = module;
// 4.利用tryModuleLoad方法加载模块
tryModuleLoad(module);
// 5.返回模块的exports
return module.exports
}
function tryModuleLoad(module) {
// 1.取出模块后缀
let extName = path.extname(module.id);
BNTangModule._extensions[extName](module);
}
let aModule = bnTangRequire("./person.json");
// let aModule = bnTangRequire("./02-a.js");
console.log(aModule);
person.json
{
"name": "BNTang",
"age": "33",
"gender": "male"
}
运行结果如下所示: