-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonJS.js
51 lines (33 loc) · 1.3 KB
/
commonJS.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const fs = require('fs');
//因为模块在不同的文件中,需要file操作
//Because of modules in different files, we need the file operation
function require(moduleName) {
let content = fs.readFileSync(moduleName, 'utf8');
/*参数分析:由于模块文件内部形如:
require(xxxxxxx)
module.exports = xxxx
require()与module.exports在模块文件内并未被声明定义,故需要靠参数传入或者调用父级作用域内相关
模块内会出现require()嵌套,故require函数需要实现递归查找
*/
let fn = new Function('exports', 'module', 'require', content + '\n return module.exports')
/* 有关Function类 About Funtion Objection
var function_name = new function (arg1, arg2, ..., argN, function_body)
eg.
function sayHi(sName, sMessage) {
alert("Hello " + sName + sMessage);
}
等效于 Equal:
var sayHi = new Function("sName", "sMessage", "alert(\"Hello \" + sName + sMessage);");
*/
let module = {
exports: {}
}
/*
最终目标是暴露出module.exports的指代物
Final goal is to get module.exports' content
*/
return fn(module.exports, module, require)
}
//调用实现
let variable = require('./module.js')
console.log(variable)