index.ts
1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import path from "path";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
export default function (
moduleName: string,
dirname: string,
absoluteRuntime: string | boolean,
) {
if (absoluteRuntime === false) return moduleName;
return resolveAbsoluteRuntime(
moduleName,
path.resolve(dirname, absoluteRuntime === true ? "." : absoluteRuntime),
);
}
function resolveAbsoluteRuntime(moduleName: string, dirname: string) {
try {
return path
.dirname(
require.resolve(`${moduleName}/package.json`, { paths: [dirname] }),
)
.replace(/\\/g, "/");
} catch (err) {
if (err.code !== "MODULE_NOT_FOUND") throw err;
throw Object.assign(
new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`),
{
code: "BABEL_RUNTIME_NOT_FOUND",
runtime: moduleName,
dirname,
},
);
}
}
export function resolveFSPath(path: string) {
return require.resolve(path).replace(/\\/g, "/");
}