/
index.js
79 lines (70 loc) · 2.04 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const path = require('path')
const fs = require('fs')
const vm = require('vm')
const _require = (filePath) => Module._load(filePath)
class Module {
constructor(id) {
this.id = id
this.exports = {}
}
static _extensions = {
'.js': (module) => {
let scrtptText = fs.readFileSync(module.id, 'utf-8')
let fnStr = module._warp(scrtptText)
let fn = vm.runInThisContext(fnStr)
let exports = module.exports
let require = _require
let _filename = module.id
let _dirname = path.dirname(module.id)
fn.call(exports, exports, require, module, _filename, _dirname)
},
'.json': (module) => {
let text = fs.readFileSync(module.id)
module.exports = JSON.parse(text)
}
}
static _resolveFilename(filePath) {
const _fpath = path.resolve(__dirname, filePath)
const _exists = fs.existsSync(_fpath)
if (_exists) return _fpath
const _extensionKeys = Object.keys(this._extensions)
for (let i = 0; i < _extensionKeys.length; i++) {
let currentFilePath = filePath + _extensionKeys[i]
if (fs.existsSync(currentFilePath)) return currentFilePath
}
throw new Error('module can not find')
}
static _catch() { }
static _load(filePath) {
const _abspath = this._resolveFilename(filePath)
const _preCacheModule = this._catch[_abspath]
if (_preCacheModule) {
return _preCacheModule.exports
}
let module = new this(_abspath)
this._catch[_abspath] = module
module.load(_abspath)
return module.exports
}
load(absPath) {
const extName = path.extname(absPath)
this.constructor._extensions[extName](this)
}
_warp(scriptText) {
let arr = [
'(function(exports, require, module, __filename, __dirname) {',
scriptText,
'})'
]
return arr.join('')
}
}
// const module1 = require('./module1')
// console.log(module1.content)
const module1 = _require('./module1')
console.log(module1)
// 多次引入重复调用
_require('./module1')
_require('./module1')
_require('./module1')
_require('./module1')