1 // Thanks: https://github.com/dwyl/decache
2 // We reuse this file to also uncache plugin base path
4 import { extname } from 'path'
6 function decachePlugin (pluginPath: string, libraryPath: string) {
7 const moduleName = find(libraryPath)
9 if (!moduleName) return
11 searchCache(moduleName, function (mod) {
12 delete require.cache[mod.id]
15 removeCachedPath(pluginPath)
18 function decacheModule (name: string) {
19 const moduleName = find(name)
21 if (!moduleName) return
23 searchCache(moduleName, function (mod) {
24 delete require.cache[mod.id]
27 removeCachedPath(moduleName)
30 // ---------------------------------------------------------------------------
37 // ---------------------------------------------------------------------------
39 function find (moduleName: string) {
41 return require.resolve(moduleName)
47 function searchCache (moduleName: string, callback: (current: NodeModule) => void) {
48 const resolvedModule = require.resolve(moduleName)
52 if (resolvedModule && ((mod = require.cache[resolvedModule]) !== undefined)) {
53 // Recursively go over the results
54 (function run (current) {
55 visited[current.id] = true
57 current.children.forEach(function (child) {
58 if (extname(child.filename) !== '.node' && !visited[child.id]) {
63 // Call the specified callback providing the
70 function removeCachedPath (pluginPath: string) {
71 const pathCache = (module.constructor as any)._pathCache
73 Object.keys(pathCache).forEach(function (cacheKey) {
74 if (cacheKey.includes(pluginPath)) {
75 delete pathCache[cacheKey]