deno.land / x / esm@v135_2 / packages / esm-vscode / src / util.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { IText, parse, SyntaxKind, walk } from "html5parser";import type { ImportMap } from "./typescript-esmsh-plugin.ts";
export const regexpNpmNaming = /^[a-zA-Z0-9][\w\.\-]*$/;export const regexpBuildVersion = /^(v\d+|stable)$/;export const regexpSemVersion = /^v?\d+(\.\d+)*(-[\w\.]+)*$/;
export function isNEString(s: any): s is string { return typeof s === "string" && s.length > 0;}
export function isValidEsmshUrl( v: string,): { url: URL; name: string; version: string } | null { if (!v.startsWith("https://esm.sh/")) { return null; } let scope = ""; let name = ""; let version = ""; const url = new URL(v); const parts = url.pathname.slice(1).split("/"); if (regexpBuildVersion.test(parts[0])) { parts.shift(); } name = parts.shift()!; console.log(name); if (name?.startsWith("@")) { scope = name; if (!regexpNpmNaming.test(scope.slice(1))) { return null; } name = parts.shift()!; } const idx = name.lastIndexOf("@"); if (idx > 0) { version = name.slice(idx + 1); if (!regexpSemVersion.test(version)) { return null; } name = name.slice(0, idx); } if (!name || !regexpNpmNaming.test(name)) { return null; } return { url, name: scope ? scope + "/" + name : name, version };}
export function getImportMapFromHtml(html: string) { let importMap: ImportMap = {}; walk(parse(html), { enter: (node) => { if ( node.type === SyntaxKind.Tag && node.name === "script" && node.body && node.attributes.some((a) => a.name.value === "type" && a.value?.value === "importmap" ) ) { importMap = JSON.parse( node.body.map((a) => (a as IText).value).join(""), ); } }, }); return importMap;}
export function insertImportMap(html: string, importMap: ImportMap): string { let start = -1; let end = -1; let firstScriptTagStart = -1; let headTagEnd = -1;
walk(parse(html), { enter: (node) => { if ( firstScriptTagStart === -1 && node.type === SyntaxKind.Tag && node.name === "script" ) { firstScriptTagStart = node.start; } if ( headTagEnd === -1 && node.type === SyntaxKind.Tag && node.name === "head" ) { headTagEnd = node.end; } if ( node.type === SyntaxKind.Tag && node.name === "script" && node.body && node.attributes.some((a) => a.name.value === "type" && a.value?.value === "importmap" ) ) { start = node.start; end = node.end; } }, });
const EOL = "\n"; const ident = " "; const im = JSON.stringify(importMap, undefined, 2).split("\n") .map((l) => ident + ident + l).join(EOL); const imScript = `<script type="importmap">${EOL}${im}${EOL}${ident}</script>`;
if (start > 0 && end > 0) { return html.slice(0, start) + imScript + html.slice(end); }
if (firstScriptTagStart > 0) { return html.slice(0, firstScriptTagStart) + imScript + EOL + ident + html.slice(firstScriptTagStart); }
if (headTagEnd > 0) { let offset = 0; let i = headTagEnd; while (html[i] && html[i] !== "<") { offset++; i--; } return html.slice(0, headTagEnd - offset) + ident + imScript + EOL + html.slice(headTagEnd - offset); }
return html;}
export function sortByVersion(a: string, b: string) { const [aMain, aPR] = a.split("-"); const [bMain, bPR] = b.split("-"); const aParts = aMain.split("."); const bParts = bMain.split("."); for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) { const aPart = parseInt(aParts[i]) || 0; const bPart = parseInt(bParts[i]) || 0; if (aPart !== bPart) { return bPart - aPart; } } if (aPR && bPR) { return bPR.localeCompare(aPR); } return 0;}
export function debunce<T extends (...args: any[]) => any>( fn: T, timeout: number,): T { let timer: number | undefined; return ((...args: any[]) => { if (timer) { clearTimeout(timer); } timer = setTimeout(() => { fn(...args); }, timeout); }) as any;}
esm

Version Info

Tagged at
a year ago