-
Notifications
You must be signed in to change notification settings - Fork 22
/
mod.ts
60 lines (55 loc) · 1.59 KB
/
mod.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
58
59
60
import type { Loader } from "./loader.ts";
import {
instantiate,
Parser as InternalParser,
} from "./eszip_wasm.generated.js";
import {
CacheSetting,
createCache,
} from "https://deno.land/x/deno_cache@0.7.1/mod.ts";
export type { LoadResponse } from "./loader.ts";
const encoder = new TextEncoder();
export const options: { wasmURL: URL | undefined } = { wasmURL: undefined };
export class Parser extends InternalParser {
private constructor() {
super();
}
static async createInstance() {
// insure instantiate is called
await instantiate({ url: options.wasmURL });
return new Parser();
}
}
export async function build(
roots: string[],
loader: Loader["load"] = createCache().load,
importMapUrl?: string,
): Promise<Uint8Array> {
const { build } = await instantiate({ url: options.wasmURL });
return build(
roots,
(specifier: string, options: {
isDynamic: boolean;
cacheSetting: CacheSetting;
checksum: string | undefined;
}) => {
return loader(
specifier,
options.isDynamic,
options.cacheSetting,
options.checksum,
).then((result) => {
if (result?.kind === "module") {
if (typeof result.content === "string") {
result.content = encoder.encode(result.content);
}
// need to convert to an array for serde_wasm_bindgen to work
// deno-lint-ignore no-explicit-any
(result as any).content = Array.from(result.content);
}
return result;
}).catch((err) => Promise.reject(String(err)));
},
importMapUrl,
);
}