forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequire-cache.ts
More file actions
46 lines (42 loc) · 1.24 KB
/
require-cache.ts
File metadata and controls
46 lines (42 loc) · 1.24 KB
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
import isError from '../../lib/is-error'
import { realpathSync } from '../../lib/realpath'
import { clearManifestCache } from '../load-manifest'
function deleteFromRequireCache(filePath: string) {
try {
filePath = realpathSync(filePath)
} catch (e) {
if (isError(e) && e.code !== 'ENOENT') throw e
}
const mod = require.cache[filePath]
if (mod) {
// remove the child reference from all parent modules
for (const parent of Object.values(require.cache)) {
if (parent?.children) {
const idx = parent.children.indexOf(mod)
if (idx >= 0) parent.children.splice(idx, 1)
}
}
// remove parent references from external modules
for (const child of mod.children) {
child.parent = null
}
delete require.cache[filePath]
return true
}
return false
}
export function deleteAppClientCache() {
deleteFromRequireCache(
require.resolve('next/dist/compiled/next-server/app-page.runtime.dev.js')
)
deleteFromRequireCache(
require.resolve(
'next/dist/compiled/next-server/app-page-experimental.runtime.dev.js'
)
)
}
export function deleteCache(filePath: string) {
// try to clear it from the fs cache
clearManifestCache(filePath)
deleteFromRequireCache(filePath)
}