Nuxt/packages/utils/src/cjs.js

90 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-06-03 19:34:10 +00:00
import { join } from 'path'
import createRequire from 'create-require'
const _require = createRequire()
2020-06-03 19:34:10 +00:00
export function isHMRCompatible (id) {
return !/[/\\]mongoose[/\\/]/.test(id)
}
export function isExternalDependency (id) {
return /[/\\]node_modules[/\\]/.test(id)
}
export function clearRequireCache (id) {
if (isExternalDependency(id) && isHMRCompatible(id)) {
return
}
const entry = getRequireCacheItem(id)
if (!entry) {
delete _require.cache[id]
return
}
if (entry.parent) {
2019-04-24 16:13:59 +00:00
entry.parent.children = entry.parent.children.filter(e => e.id !== id)
}
// Needs to be cleared before children, to protect against circular deps (#7966)
delete _require.cache[id]
for (const child of entry.children) {
clearRequireCache(child.id)
}
}
export function scanRequireTree (id, files = new Set()) {
if (isExternalDependency(id) || files.has(id)) {
return files
}
const entry = getRequireCacheItem(id)
if (!entry) {
files.add(id)
return files
}
files.add(entry.id)
for (const child of entry.children) {
scanRequireTree(child.id, files)
}
return files
}
export function getRequireCacheItem (id) {
try {
return _require.cache[id]
} catch (e) {
}
}
export function resolveModule (id, paths) {
if (typeof paths === 'string') {
paths = [paths]
}
2020-12-22 20:05:25 +00:00
return _require.resolve(id, {
paths: [].concat(paths || [], global.__NUXT_PATHS__ || [])
2020-12-22 20:05:25 +00:00
})
}
export function requireModule (id, paths) {
return _require(resolveModule(id, paths))
}
export function tryRequire (id, paths) {
try { return requireModule(id, paths) } catch (e) { }
}
export function tryResolve (id, paths) {
try { return resolveModule(id, paths) } catch (e) { }
}
export function getPKG (id, paths) {
return tryRequire(join(id, 'package.json'), paths)
}