core/src/util/index.ts

29 lines
953 B
TypeScript

import path from 'path';
export { ScopedEventEmitter } from './events';
export { IProcessData, spawnProcess, execProcess } from './run';
export * from './checksum';
/**
* Load a Node.js module without caching it.
* @param file JavaScript file
* @returns Loaded module
*/
export function requireNoCache(file: string): object | undefined {
const fullPath = path.resolve(file);
// TODO: maybe use new "import" function? <- currently has no cache invalidation
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mod = require(fullPath);
if (require.cache && require.cache[fullPath]) {
delete require.cache[fullPath];
// Delete in-plugin-folder requires from cache as well
const dirname = path.dirname(fullPath);
const cacheEntries = Object.keys(require.cache).filter((entry) => entry.startsWith(dirname));
cacheEntries.forEach((entry) => {
delete require.cache[entry];
});
}
return mod;
}