Last change
on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
883 bytes
|
Line | |
---|
1 | import path from "path";
|
---|
2 |
|
---|
3 | import { createRequire } from "module";
|
---|
4 | const require = createRequire(import.meta.url);
|
---|
5 |
|
---|
6 | export default function (moduleName, dirname, absoluteRuntime) {
|
---|
7 | if (absoluteRuntime === false) return moduleName;
|
---|
8 |
|
---|
9 | return resolveAbsoluteRuntime(
|
---|
10 | moduleName,
|
---|
11 | path.resolve(dirname, absoluteRuntime === true ? "." : absoluteRuntime),
|
---|
12 | );
|
---|
13 | }
|
---|
14 |
|
---|
15 | function resolveAbsoluteRuntime(moduleName: string, dirname: string) {
|
---|
16 | try {
|
---|
17 | return path
|
---|
18 | .dirname(
|
---|
19 | require.resolve(`${moduleName}/package.json`, { paths: [dirname] }),
|
---|
20 | )
|
---|
21 | .replace(/\\/g, "/");
|
---|
22 | } catch (err) {
|
---|
23 | if (err.code !== "MODULE_NOT_FOUND") throw err;
|
---|
24 |
|
---|
25 | throw Object.assign(
|
---|
26 | new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`),
|
---|
27 | {
|
---|
28 | code: "BABEL_RUNTIME_NOT_FOUND",
|
---|
29 | runtime: moduleName,
|
---|
30 | dirname,
|
---|
31 | },
|
---|
32 | );
|
---|
33 | }
|
---|
34 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.