-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathbuild.mjs
145 lines (124 loc) · 3.79 KB
/
build.mjs
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
// 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, context } from "esbuild";
const thisDir = dirname(fileURLToPath(import.meta.url));
const libsDir = join(thisDir, "..", "node_modules");
/** @type {import("esbuild").BuildOptions} */
const buildOptions = {
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"),
],
outdir: join(thisDir, "out"),
bundle: true,
// minify: true,
mainFields: ["browser", "module", "main"],
external: ["vscode"],
format: "cjs",
platform: "browser",
target: ["es2020"],
sourcemap: "linked",
//logLevel: "debug",
define: { "import.meta.url": "undefined" },
};
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}`;
}
export function copyWasmToVsCode() {
// Copy the wasm module into the extension directory
let qsharpWasm = join(
thisDir,
"..",
"npm",
"qsharp",
"lib",
"web",
"qsc_wasm_bg.wasm",
);
let qsharpDest = join(thisDir, `wasm`);
console.log("Copying the wasm file to VS Code from: " + qsharpWasm);
mkdirSync(qsharpDest, { recursive: true });
copyFileSync(qsharpWasm, join(qsharpDest, "qsc_wasm_bg.wasm"));
}
/**
*
* @param {string} [destDir]
*/
export function copyKatex(destDir) {
let katexBase = join(libsDir, `katex/dist`);
let katexDest = destDir ?? join(thisDir, `out/katex`);
console.log("Copying the Katex files over from: " + katexBase);
mkdirSync(katexDest, { recursive: true });
copyFileSync(
join(katexBase, "katex.min.css"),
join(katexDest, "katex.min.css"),
);
// Also copy the 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"),
);
const fontsDir = join(katexBase, "fonts");
const fontsOutDir = join(katexDest, "fonts");
mkdirSync(fontsOutDir, { recursive: true });
for (const file of readdirSync(fontsDir)) {
if (file.endsWith(".woff2")) {
copyFileSync(join(fontsDir, file), join(fontsOutDir, file));
}
}
}
function buildBundle() {
console.log("Running esbuild");
build(buildOptions).then(() =>
console.log(`Built bundle to ${join(thisDir, "out")}`),
);
}
export async function watchVsCode() {
console.log("Building vscode extension in watch mode");
// Plugin to log start/end of build events (mostly to help VS Code problem matcher)
/** @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()),
);
},
};
let ctx = await context({
...buildOptions,
plugins: [buildPlugin],
color: false,
});
ctx.watch();
}
const thisFilePath = resolve(fileURLToPath(import.meta.url));
if (thisFilePath === resolve(process.argv[1])) {
// This script being run directly (not imported)
const isWatch = process.argv.includes("--watch");
if (isWatch) {
watchVsCode();
} else {
copyWasmToVsCode();
copyKatex();
buildBundle();
}
}