Nuxt/packages/utils/src/cjs.js

61 lines
1.0 KiB
JavaScript
Raw Normal View History

export function isExternalDependency (id) {
return /[/\\]node_modules[/\\]/.test(id)
}
export function clearRequireCache (id) {
let entry
try {
entry = require.cache[id]
} catch (e) {
delete require.cache[id]
return
}
if (!entry || isExternalDependency(id)) {
return
}
if (entry.parent) {
2019-04-24 16:13:59 +00:00
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) {
files.add(id)
return files
}
if (!entry || isExternalDependency(id) || files.has(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')
}