-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatchdelcache.js
160 lines (135 loc) · 4.06 KB
/
batchdelcache.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @file batch delete require cache
* @author cxtom
*/
const path = require('path')
const callsite = require('callsite')
function batchdelcache(modulePathArray, all = false, rootPath = '') {
function traverseChildren(mod, cb) {
const visitedChildiren = {}
visitedChildiren[mod.id] = 1
function run(m) {
visitedChildiren[m.id] = 1;
m.children = m.children.filter(child => {
// 不能 delete c++ child,否则报错
if (path.extname(child.filename) !== '.node' && !visitedChildiren[child.id]) {
run(child);
}
return false;
})
cb(m);
}
run(mod);
}
if (!Array.isArray(modulePathArray)) {
modulePathArray = [modulePathArray]
}
const modulesToClean = modulePathArray.map(findModule).map(p => require.cache[p]).filter(item => !!item)
const topModule = getTopModule(rootPath && findModule(rootPath))
const moduleInUse = findAllModuleInUse(modulesToClean, topModule)
for (const mod of modulesToClean) {
// 删除该模块的所有子引用
traverseChildren(mod, m => {
if (!moduleInUse.has(m)) {
delete require.cache[m.id];
}
});
// 防止内存泄露
/* istanbul ignore else */
if (mod.parent) {
if (all) {
clearFromTree(mod, topModule);
}
else {
mod.parent.children = mod.parent.children.filter(m => m.id !== mod.id);
}
}
}
}
function getTopModule(rootPath) {
let topModule = null
if (rootPath) {
try {
topModule = require.cache[require.resolve(rootPath)]
}
catch(e) {
throw Error('top module is not found.')
}
}
else {
topModule = module
while (topModule.parent) {
topModule = topModule.parent
}
}
return topModule
}
function clearFromTree(mod, topModule) {
traverseTree(topModule, m => m.id == mod.id); // true to delete
function traverseTree(root, cb) {
const visitedTreeChildren = {};
visitedTreeChildren[root.id] = 1;
function run(mod) {
mod.children = mod.children.filter(child => {
if (cb(child)) {
return false
}
else {
if(!visitedTreeChildren[child.id]) {
visitedTreeChildren[child.id] = 1;
run(child);
}
return true;
}
})
}
run(root);
}
}
/**
* 找到所有还在使用的模块
*/
function findAllModuleInUse(moduleRealPathArray, topModule) {
const moduleRealPathSet = new Set(moduleRealPathArray)
const moduleInUse = new Set()
moduleInUse.add(topModule)
function run(mod) {
for (const child of mod.children) {
if (moduleInUse.has(child) || moduleRealPathSet.has(child)) {
continue
}
moduleInUse.add(child)
run(child)
}
}
run(topModule)
return moduleInUse
}
function findModule(modulePath) {
//如果传入相对路径
if (/^\./.test(modulePath)) {
let visited = false
const stacks = callsite()
for (const stack of stacks) {
const filename = stack.getFileName()
/* istanbul ignore else */
if (filename == module.filename) {
visited = true
}
else if (!filename) {
continue
}
else if (filename !== module.filename && filename !== 'module.js' && visited) {
modulePath = path.resolve(path.dirname(filename), modulePath)
break
}
}
}
try {
return require.resolve(modulePath)
} catch (e) {
throw new Error(`[batchdelcache] ${e.stack}`);
}
}
module.exports = batchdelcache;
module.exports.default = batchdelcache;