-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
cjs.js
98 lines (75 loc) · 2.05 KB
/
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
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
import { join } from 'path'
import _createRequire from 'create-require'
import jiti from 'jiti'
export const createRequire = (filename, useJiti = global.__NUXT_DEV__) => {
if (useJiti && typeof jest === 'undefined') {
return jiti(filename)
}
return _createRequire(filename)
}
const _require = createRequire()
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) {
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]
}
return _require.resolve(id, {
paths: [].concat(...(global.__NUXT_PREPATHS__ || []), paths || [], global.__NUXT_PATHS__ || [], process.cwd())
})
}
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)
}