2019-10-01 11:04:24 +00:00
|
|
|
export function isExternalDependency (id) {
|
|
|
|
return /[/\\]node_modules[/\\]/.test(id)
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export function clearRequireCache (id) {
|
2020-01-21 12:51:58 +00:00
|
|
|
const entry = getRequireCacheItem(id)
|
|
|
|
|
|
|
|
if (!entry) {
|
2020-01-19 08:34:35 +00:00
|
|
|
delete require.cache[id]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-21 12:51:58 +00:00
|
|
|
if (isExternalDependency(id)) {
|
2019-04-11 10:04:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.parent) {
|
2019-04-24 16:13:59 +00:00
|
|
|
entry.parent.children = entry.parent.children.filter(e => e.id !== id)
|
2019-04-11 10:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const child of entry.children) {
|
|
|
|
clearRequireCache(child.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete require.cache[id]
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export function scanRequireTree (id, files = new Set()) {
|
2020-01-21 12:51:58 +00:00
|
|
|
const entry = getRequireCacheItem(id)
|
|
|
|
|
|
|
|
if (!entry) {
|
2020-01-19 08:34:35 +00:00
|
|
|
files.add(id)
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2020-01-21 12:51:58 +00:00
|
|
|
if (isExternalDependency(id) || files.has(id)) {
|
2019-04-11 10:04:21 +00:00
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
|
|
|
files.add(entry.id)
|
|
|
|
|
|
|
|
for (const child of entry.children) {
|
|
|
|
scanRequireTree(child.id, files)
|
|
|
|
}
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|
2019-09-26 12:10:54 +00:00
|
|
|
|
2020-01-21 12:51:58 +00:00
|
|
|
export function getRequireCacheItem (id) {
|
|
|
|
try {
|
|
|
|
return require.cache[id]
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 12:10:54 +00:00
|
|
|
export function tryRequire (id) {
|
|
|
|
try {
|
|
|
|
return require(id)
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPKG (id) {
|
|
|
|
return tryRequire(id + '/package.json')
|
|
|
|
}
|