deno.land / x / esm@v135_2 / build.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
export type BuildInput = { source: string; loader?: "js" | "jsx" | "ts" | "tsx"; dependencies?: Record<string, string>; types?: string;};
export type TransformOptions = { target?: | "deno" | "denonext" | "node" | "esnext" | `es201${5 | 6 | 7 | 8 | 9}` | `es202${0 | 1 | 2}`; imports?: Record<string, string>;};
export type BuildOutput = { id: string; url: string; bundleUrl: string;};
const MiB = 10 << 20;
async function fetchApi( endpoint: string, options: Record<string, any>,): Promise<any> { const apiName = endpoint.slice(1); if (options.source.length > MiB) { throw new Error(`esm.sh [${apiName}] <400> source exceeded limit.`); } const body = JSON.stringify(options); if (body.length > 2 * MiB) { throw new Error(`esm.sh [${apiName}] <400> body exceeded limit.`); } const res = await fetch(new URL(endpoint, import.meta.url), { method: "POST", headers: { "Content-Type": "application/json" }, body, }); if (!res.ok) { throw new Error( `esm.sh [${apiName}] <${res.status}> ${res.statusText}`, ); } const ret = await res.json(); if (ret.error) { throw new Error( `esm.sh [${apiName}] ${ret.error.message}`, ); } return ret;}
export function build(input: string | BuildInput): Promise<BuildOutput> { const options = typeof input === "string" ? { source: input } : input; if (!options.source) { throw new Error("esm.sh [build] <400> missing source"); } return fetchApi("/build", options);}
export function transform( input: string | (BuildInput & TransformOptions),): Promise<{ code: string }> { const options = typeof input === "string" ? { source: input } : input; if (!options.source) { throw new Error("esm.sh [transform] <400> missing source"); } Reflect.set(options, "imports", JSON.stringify(options.imports || {})); return fetchApi("/transform", options);}
export async function esm<T extends object = Record<string, any>>( strings: TemplateStringsArray, ...values: any[]): Promise<T & { _build: BuildOutput }> { const source = String.raw({ raw: strings }, ...values); const ret = await buildWithCache(source); const mod: T = await import(ret.url); return { ...mod, _build: ret, };}
async function buildWithCache( input: string | BuildInput,): Promise<BuildOutput> { const key = await computeHash( typeof input === "string" ? input : JSON.stringify(input), ); if (globalThis.localStorage) { const cached = localStorage.getItem(key); if (cached) { return JSON.parse(cached); } } const ret = await build(input); if (globalThis.localStorage) { localStorage.setItem(key, JSON.stringify(ret)); } return ret;}
async function computeHash(input: string): Promise<string> { const buffer = new Uint8Array( await crypto.subtle.digest( "SHA-1", new TextEncoder().encode(input), ), ); return [...buffer].map((b) => b.toString(16).padStart(2, "0")).join("");}
export default build;
esm

Version Info

Tagged at
2 months ago