2023-12-21 12:39:11 +00:00
|
|
|
import path from 'path';
|
|
|
|
import { pathToFileURL } from 'url';
|
|
|
|
|
2022-07-19 14:51:27 +00:00
|
|
|
export function requireModule(m: any) {
|
|
|
|
if (typeof m === 'string') {
|
|
|
|
m = require(m);
|
2022-06-20 15:29:21 +00:00
|
|
|
}
|
2023-12-21 12:39:11 +00:00
|
|
|
|
2022-07-19 14:51:27 +00:00
|
|
|
if (typeof m !== 'object') {
|
|
|
|
return m;
|
2022-06-20 15:29:21 +00:00
|
|
|
}
|
2023-12-21 12:39:11 +00:00
|
|
|
|
2022-07-19 14:51:27 +00:00
|
|
|
return m.__esModule ? m.default : m;
|
2022-06-20 15:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default requireModule;
|
2023-12-21 12:39:11 +00:00
|
|
|
|
|
|
|
export async function importModule(m: string) {
|
2023-12-28 05:40:11 +00:00
|
|
|
if (!process.env.VITEST) {
|
|
|
|
return requireModule(m);
|
|
|
|
}
|
|
|
|
|
2023-12-21 12:39:11 +00:00
|
|
|
if (path.isAbsolute(m)) {
|
|
|
|
m = pathToFileURL(m).href;
|
|
|
|
}
|
|
|
|
|
|
|
|
const r = (await import(m)).default;
|
|
|
|
return r.__esModule ? r.default : r;
|
|
|
|
}
|