tachybase_todo/packages/core/utils/src/requireModule.ts
chenos 8108107779
fix: require module (#3277)
* fix: require module

* fix: include utils
2023-12-28 13:40:11 +08:00

30 lines
537 B
TypeScript

import path from 'path';
import { pathToFileURL } from 'url';
export function requireModule(m: any) {
if (typeof m === 'string') {
m = require(m);
}
if (typeof m !== 'object') {
return m;
}
return m.__esModule ? m.default : m;
}
export default requireModule;
export async function importModule(m: string) {
if (!process.env.VITEST) {
return requireModule(m);
}
if (path.isAbsolute(m)) {
m = pathToFileURL(m).href;
}
const r = (await import(m)).default;
return r.__esModule ? r.default : r;
}