forked from planetarium/Corvette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moduleUtils.ts
57 lines (51 loc) · 1.6 KB
/
moduleUtils.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as path from "std/path/mod.ts";
// TODO: this const should be changed to point to the root source code directory whenever this
// file is moved.
const relativeToBase = ".";
export const baseDir = path.join(
path.dirname(path.fromFileUrl(import.meta.url)),
relativeToBase,
);
const baseDirInternal = baseDir;
export function normalizeImports(
url: string,
baseDir: string = baseDirInternal,
): string {
if (!path.isAbsolute(baseDir)) throw new Error("`baseDir` must be absolute.");
try {
const parsed = new URL(url);
if (parsed.protocol !== "file:" || url.startsWith("file:/")) {
return parsed.toString();
}
parsed.pathname = path.join(baseDir, parsed.pathname.slice(1));
return parsed.toString();
} catch (e) {
if (!(e instanceof TypeError)) throw e;
return "file:" + url.startsWith("/") ? url : path.join(baseDir, url);
}
}
export async function importESOrJson(
url: string,
options?: ImportCallOptions & { baseDir?: string },
) {
url = normalizeImports(url, options?.baseDir);
let imported;
let isJsonModule = true;
try {
imported = await import(url, { assert: { type: "json" }, ...options });
} catch (e) {
if (!(e instanceof TypeError)) throw e;
isJsonModule = false;
imported = await import(url, options);
}
const hash = new URL(url).hash.slice(1);
return isJsonModule
? hash ? imported["default"][hash] : imported["default"]
: imported[hash || "default"];
}
export function getRelativeScriptPath(importMetaUrl: string) {
return path.relative(
Deno.cwd(),
path.dirname(path.fromFileUrl(importMetaUrl)),
);
}