fes.js/packages/fes-utils/src/cleanRequireCache.js

22 lines
808 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export const isWindows = typeof process !== 'undefined' && process.platform === 'win32';
// 暂时无法使用 jest 进行单元测试,原因可参见
// https://github.com/facebook/jest/issues/5741
export default function (cacheKey) {
// windows 下 require.cache 中路径 key 为类似 c:\demo\.umirc.ts
const cachePath = isWindows ? cacheKey.replace(/\//g, '\\') : cacheKey;
if (require.cache[cachePath]) {
const cacheParent = (require.cache[cachePath]).parent;
let i = cacheParent?.children.length || 0;
// 清理 require cache 中 parents 的引用
while (i--) {
if (cacheParent.children[i].id === cachePath) {
cacheParent.children.splice(i, 1);
}
}
delete require.cache[cachePath];
}
}