-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathbuild.mjs
More file actions
289 lines (255 loc) · 8.48 KB
/
build.mjs
File metadata and controls
289 lines (255 loc) · 8.48 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//@ts-check
import { copyFileSync, mkdirSync, readdirSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { build as esbuildBuild, context } from "esbuild";
const thisDir = dirname(fileURLToPath(import.meta.url));
const libsDir = join(thisDir, "..", "..", "node_modules");
// ── Shared esbuild options ──────────────────────────────────────────
/** @type {import("esbuild").BuildOptions} */
const commonBuildOptions = {
bundle: true,
external: ["vscode"],
format: "cjs",
target: ["es2022"],
sourcemap: "linked",
};
// ── Per-platform build options ──────────────────────────────────────
/** @type {Record<string, import("esbuild").BuildOptions>} */
const platformBuildOptions = {
ui: {
...commonBuildOptions,
platform: "browser",
outdir: join(thisDir, "out", "webview"),
entryPoints: [
join(thisDir, "src", "webview/webview.tsx"),
join(thisDir, "src", "webview/editor.tsx"),
],
define: {
"import.meta.url": "undefined",
__PLATFORM__: JSON.stringify("browser"),
},
// plugins added at build time (needs inlineStateComputeWorkerPlugin)
},
browser: {
...commonBuildOptions,
entryPoints: [
join(thisDir, "src", "extension.ts"),
join(thisDir, "src", "compilerWorker.ts"),
join(thisDir, "src", "debugger/debug-service-worker.ts"),
],
platform: "browser",
outdir: join(thisDir, "out", "browser"),
define: {
"import.meta.url": "undefined",
__PLATFORM__: JSON.stringify("browser"),
},
},
node: {
...commonBuildOptions,
platform: "node",
outdir: join(thisDir, "out", "node"),
entryPoints: [join(thisDir, "src", "extension.ts")],
external: ["vscode"],
banner: {
js: 'const _importMetaUrl = require("url").pathToFileURL(__filename).href;',
},
define: {
"import.meta.url": "_importMetaUrl",
__PLATFORM__: JSON.stringify("node"),
},
},
"node-worker": {
...commonBuildOptions,
platform: "node",
outdir: join(thisDir, "out", "node"),
entryPoints: [
join(thisDir, "src", "compilerWorker.ts"),
join(thisDir, "src", "debugger/debug-service-worker.ts"),
],
define: {
"import.meta.url": "undefined",
__PLATFORM__: JSON.stringify("node"),
},
},
};
// ── Inline worker plugin ────────────────────────────────────────────
/** @type {import("esbuild").Plugin} */
const inlineStateComputeWorkerPlugin = {
name: "Inline State Compute Worker",
setup(builder) {
builder.onResolve({ filter: /stateComputeWorker.inline\.ts$/ }, (args) => ({
path: join(args.resolveDir, args.path),
namespace: "inline-state-compute-worker",
}));
builder.onLoad(
{ filter: /.*/, namespace: "inline-state-compute-worker" },
async () => {
const workerEntry = join(
thisDir,
"src",
"webview",
"stateComputeWorker.ts",
);
const result = await esbuildBuild({
...commonBuildOptions,
entryPoints: [workerEntry],
bundle: true,
write: false,
platform: "browser",
format: "iife",
sourcemap: false,
logLevel: "silent",
});
const workerSource = result.outputFiles?.[0]?.text ?? "";
return {
contents: `const workerSource = ${JSON.stringify(workerSource)};\nexport default workerSource;\n`,
loader: "ts",
};
},
);
},
};
// ── Asset copy helpers ──────────────────────────────────────────────
export function copyWasmToVsCode() {
const qsharpWasm = join(
thisDir,
"..",
"npm",
"qsharp",
"lib",
"web",
"qsc_wasm_bg.wasm",
);
const qsharpDest = join(thisDir, "wasm");
console.log("Copying the wasm file to VS Code from: " + qsharpWasm);
console.log("Destination: " + qsharpDest);
mkdirSync(qsharpDest, { recursive: true });
copyFileSync(qsharpWasm, join(qsharpDest, "qsc_wasm_bg.wasm"));
}
/** @param {string} [destDir] */
export function copyKatex(destDir) {
const katexBase = join(libsDir, "katex/dist");
const katexDest = destDir ?? join(thisDir, "out/katex");
const fontsDir = join(katexBase, "fonts");
const fontsOutDir = join(katexDest, "fonts");
console.log("Copying the Katex files over from: " + katexBase);
mkdirSync(katexDest, { recursive: true });
mkdirSync(fontsOutDir, { recursive: true });
// katex
copyFileSync(
join(katexBase, "katex.min.css"),
join(katexDest, "katex.min.css"),
);
// github markdown css
copyFileSync(
join(libsDir, "github-markdown-css/github-markdown-light.css"),
join(katexDest, "github-markdown-light.css"),
);
copyFileSync(
join(libsDir, "github-markdown-css/github-markdown-dark.css"),
join(katexDest, "github-markdown-dark.css"),
);
// highlight.js css
copyFileSync(
join(libsDir, "highlight.js/styles/default.css"),
join(katexDest, "hljs-light.css"),
);
copyFileSync(
join(libsDir, "highlight.js/styles/dark.css"),
join(katexDest, "hljs-dark.css"),
);
// vscode codicons
copyFileSync(
join(libsDir, "@vscode", "codicons", "dist", "codicon.css"),
join(katexDest, "codicon.css"),
);
copyFileSync(
join(libsDir, "@vscode", "codicons", "dist", "codicon.ttf"),
join(katexDest, "codicon.ttf"),
);
// katex fonts
for (const file of readdirSync(fontsDir)) {
if (file.endsWith(".woff2")) {
copyFileSync(join(fontsDir, file), join(fontsOutDir, file));
}
}
}
// ── Build functions ─────────────────────────────────────────────────
/** @param {string} platform */
async function buildPlatform(platform) {
const options = platformBuildOptions[platform];
if (!options) throw new Error(`Invalid platform: ${platform}`);
// UI build needs the inline worker plugin
if (platform === "ui") {
options.plugins = [inlineStateComputeWorkerPlugin];
}
console.log(`Running esbuild for platform: ${platform}`);
await esbuildBuild(options);
console.log(`Built bundle to ${options.outdir}`);
}
function getTimeStr() {
const now = new Date();
const hh = now.getHours().toString().padStart(2, "0");
const mm = now.getMinutes().toString().padStart(2, "0");
const ss = now.getSeconds().toString().padStart(2, "0");
const mil = now.getMilliseconds().toString().padStart(3, "0");
return `${hh}:${mm}:${ss}.${mil}`;
}
// This only watches for platform = "browser" for the sake of simplicity,
// so make sure to run a full build first to catch any errors in the node
// build before pushing code changes.
export async function watchVsCode() {
console.log("Building vscode extension in watch mode");
/** @type {import("esbuild").Plugin} */
const buildPlugin = {
name: "Build Events",
setup(build) {
build.onStart(() =>
console.log("VS Code build started @ " + getTimeStr()),
);
build.onEnd(() =>
console.log("VS Code build complete @ " + getTimeStr()),
);
},
};
const ctx = await context({
...commonBuildOptions,
entryPoints: [
join(thisDir, "src", "extension.ts"),
join(thisDir, "src", "compilerWorker.ts"),
join(thisDir, "src", "debugger/debug-service-worker.ts"),
join(thisDir, "src", "webview/webview.tsx"),
join(thisDir, "src", "webview/editor.tsx"),
],
platform: "browser",
outdir: join(thisDir, "out", "browser"),
plugins: [inlineStateComputeWorkerPlugin, buildPlugin],
color: false,
define: {
"import.meta.url": "undefined",
__PLATFORM__: JSON.stringify("browser"),
},
});
ctx.watch();
}
(async () => {
const thisFilePath = resolve(fileURLToPath(import.meta.url));
if (thisFilePath === resolve(process.argv[1])) {
const isWatch = process.argv.includes("--watch");
if (isWatch) {
await watchVsCode();
} else {
copyKatex();
copyWasmToVsCode();
await Promise.all([
buildPlatform("ui"),
buildPlatform("browser"),
buildPlatform("node"),
buildPlatform("node-worker"),
]);
}
}
})();