425ed98c42f68ebb48e2b9d2622109c9f360a861
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / import-fresh / index.js
1 'use strict';
2 const path = require('path');
3 const resolveFrom = require('resolve-from');
4 const parentModule = require('parent-module');
5
6 module.exports = moduleId => {
7         if (typeof moduleId !== 'string') {
8                 throw new TypeError('Expected a string');
9         }
10
11         const parentPath = parentModule(__filename);
12
13         const filePath = resolveFrom(path.dirname(parentPath), moduleId);
14
15         const oldModule = require.cache[filePath];
16         // Delete itself from module parent
17         if (oldModule && oldModule.parent) {
18                 let i = oldModule.parent.children.length;
19
20                 while (i--) {
21                         if (oldModule.parent.children[i].id === filePath) {
22                                 oldModule.parent.children.splice(i, 1);
23                         }
24                 }
25         }
26
27         delete require.cache[filePath]; // Delete module from cache
28
29         const parent = require.cache[parentPath]; // If `filePath` and `parentPath` are the same, cache will already be deleted so we won't get a memory leak in next step
30
31         return parent === undefined ? require(filePath) : parent.require(filePath); // In case cache doesn't have parent, fall back to normal require
32 };