deno.land / x / esm@v135_2 / packages / esm-vscode / src / extension.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import * as vscode from "vscode";import type { ImportMap } from "./typescript-esmsh-plugin.ts";import { debunce, getImportMapFromHtml, insertImportMap, regexpNpmNaming, sortByVersion,} from "./util.ts";
interface ProjectConfig { importMap?: ImportMap;}
interface TSApi { updateConfig: (config: ProjectConfig) => void;}
const jsxRuntimes = [ "react", "preact", "solid-js",];
export async function activate(context: vscode.ExtensionContext) { const { commands, workspace, window } = vscode;
let tsApi: TSApi; try { tsApi = await ensureTsApi(); } catch (e) { console.error(e); }
const onIndexHtmlChange = debunce((html: string) => { const importMap = getImportMapFromHtml(html); tsApi.updateConfig({ importMap }); }, 500);
context.subscriptions.push( workspace.onDidSaveTextDocument((document) => { const name = workspace.asRelativePath(document.uri); if (name === "index.html") { onIndexHtmlChange(document.getText()); } }), );
context.subscriptions.push( commands.registerCommand( "esmsh.addModule", async () => { const name = await window.showInputBox({ placeHolder: "Enter module name, e.g. lodash", validateInput: (name: string) => { if (!name.trim()) { return null; } let scope = ""; let pkgName = name; if (name.startsWith("@")) { const parts = name.split("/"); if (parts.length < 2) { return "Invalid Module Name"; } scope = parts[0] + "/"; pkgName = parts[1]; } pkgName = pkgName.split("@")[0]; if ( (scope && !regexpNpmNaming.test(scope)) || !regexpNpmNaming.test(pkgName) ) { return "Invalid Module Name"; } return null; }, }); if (!name) { return; }
// TODO: support multiple packages let pkgName = name.trim().split(" ")[0]; let scope = ""; let version = ""; if (pkgName.startsWith("@")) { const parts = pkgName.split("/"); scope = parts[0]; pkgName = parts[1]; } if (pkgName.includes("@")) { const parts = pkgName.split("@"); pkgName = parts[0]; version = parts[1]; } if (scope) { pkgName = scope + "/" + pkgName; } window.withProgress({ location: vscode.ProgressLocation.Window, cancellable: true, title: `Searching '${pkgName}'...`, }, async () => { try { const res = await fetch(`https://registry.npmjs.org/${pkgName}`); if (!res.ok) { window.showErrorMessage(`Could not find '${pkgName}'`); return; }
const pkgInfo = await res.json(); const distTags = Object.keys(pkgInfo["dist-tags"]).map((dist) => ({ label: pkgName, description: `<${dist}> ${pkgInfo["dist-tags"][dist]}`, })); const allVersions = Object.keys(pkgInfo.versions).sort( sortByVersion, ).map((version) => ({ label: pkgName, description: version })); const versions = version ? allVersions.filter((v) => v.description.startsWith(version)) : distTags.concat(allVersions); if (versions.length === 0) { window.showErrorMessage(`Could not find '${pkgName}@${version}'`); return; } window.showQuickPick( versions, { placeHolder: `Select a version of '${pkgName}':`, matchOnDescription: true, }, ).then(async (item) => { if (!item) { return; }
const uris = await workspace.findFiles("index.html"); if (uris.length === 0) { window.showErrorMessage("No index.html found"); return; }
const uri = uris[0]; const document = await workspace.openTextDocument(uri); const importMap = getImportMapFromHtml(document.getText()); const pkgName = item.label; const version = item.description.split(" ")[1] ?? item.description; const imports = importMap.imports || (importMap.imports = {}); if (jsxRuntimes.includes(pkgName)) { imports["@jsxImportSource"] = `https://esm.sh/${pkgName}@${version}`; } imports[pkgName] = `https://esm.sh/${pkgName}@${version}`; imports[pkgName + "/"] = `https://esm.sh/${pkgName}@${version}/`; const newHtml = new TextEncoder().encode( insertImportMap(document.getText(), importMap), ); workspace.fs.writeFile(uri, newHtml); tsApi.updateConfig({ importMap }); }); } catch (error) { window.showErrorMessage(`Could not find '${name}'`); } }); }, ), );}
async function ensureTsApi() { const tsExtension = vscode.extensions.getExtension( "vscode.typescript-language-features", ); if (!tsExtension) { throw new Error("vscode.typescript-language-features not found"); } await tsExtension.activate(); const api = tsExtension.exports.getAPI(0); if (!api) { throw new Error("vscode.typescript-language-features api not found"); } const config: ProjectConfig = {}; return { updateConfig: (c: ProjectConfig) => { const old = JSON.stringify(config); Object.assign(config, c); if (old !== JSON.stringify(config)) { api.configurePlugin("typescript-esmsh-plugin", config); } }, };}
export function deactivate() {}
esm

Version Info

Tagged at
a year ago