/
cjs.js
58 lines (46 loc) · 1015 Bytes
/
cjs.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
export function isExternalDependency (id) {
return /[/\\]node_modules[/\\]/.test(id)
}
export function clearRequireCache (id) {
let entry
try {
entry = require.cache[id]
} catch (e) {
}
if (!entry || isExternalDependency(id)) {
delete require.cache[id]
return
}
if (entry.parent) {
entry.parent.children = entry.parent.children.filter(e => e.id !== id)
}
for (const child of entry.children) {
clearRequireCache(child.id)
}
delete require.cache[id]
}
export function scanRequireTree (id, files = new Set()) {
let entry
try {
entry = require.cache[id]
} catch (e) {
}
if (!entry || isExternalDependency(id) || files.has(id)) {
files.add(id)
return files
}
files.add(entry.id)
for (const child of entry.children) {
scanRequireTree(child.id, files)
}
return files
}
export function tryRequire (id) {
try {
return require(id)
} catch (e) {
}
}
export function getPKG (id) {
return tryRequire(id + '/package.json')
}