UNPKG

745 BPlain TextView Raw
1export function requireNewCopy(moduleNameOrPath: string): any {
2 // Store a reference to whatever's in the 'require' cache,
3 // so we don't permanently destroy it, and then ensure there's
4 // no cache entry for this module
5 const resolvedModule = require.resolve(moduleNameOrPath);
6 const wasCached = resolvedModule in require.cache;
7 let cachedInstance;
8 if (wasCached) {
9 cachedInstance = require.cache[resolvedModule];
10 delete require.cache[resolvedModule];
11 }
12
13 try {
14 // Return a new copy
15 return require(resolvedModule);
16 } finally {
17 // Restore the cached entry, if any
18 if (wasCached) {
19 require.cache[resolvedModule] = cachedInstance;
20 }
21 }
22}