-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild.mjs
More file actions
118 lines (104 loc) · 3.51 KB
/
esbuild.mjs
File metadata and controls
118 lines (104 loc) · 3.51 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
import { spawn } from "child_process";
import * as esbuild from "esbuild";
import { rm, mkdir, readFile, writeFile } from "fs/promises";
import { getLicenseFileText } from "generate-license-file";
const production = process.argv.includes('--production');
const watch = process.argv.includes('--watch');
//packages not distributed in bundle
const external = ["vscode", "vscode-languageclient", "chromium-bidi"];
async function main() {
const start = async (cmd, watchArgs, name) => {
const proc = spawn(watch ? `${cmd} ${watchArgs}` : cmd, {
shell: true,
stdio: "inherit"
});
console.log(`running ${name}...`);
if (!watch) {
await new Promise((res,rej) => proc.on("exit", (code)=>{
if (code!=0) rej(new Error(`${name} exited with nonzero exit code ${code}`));
else res();
}));
} else {
proc.on("spawn", ()=>console.log(`${name} started`));
proc.on("error", (err) => {
console.error(`failed to spawn ${name}\n`, err);
});
}
};
const outDir = production ? "dist" : "out";
if (production) {
console.log("bundling for production...");
await rm(outDir, {recursive: true, force: true});
const base = await readFile("LICENSE_BASE", "utf-8");
const thirdparty = await getLicenseFileText("./package.json");
await writeFile("LICENSE", `${base}\n${thirdparty}`);
console.log("recreating LICENSE...");
}
await mkdir(outDir, {recursive: true});
await start(`npx @tailwindcss/cli -i ./src/main.css -o ./${outDir}/output.css`, "--watch", "tailwind");
await start("npx tsc --project ./tsconfig.json", "--watch --preserveWatchOutput", "tsc");
/**
* @type {import('esbuild').Plugin}
*/
const esbuildProblemMatcherPlugin = (name) => ({
name: 'esbuild-problem-matcher',
setup(build) {
build.onStart(() => {
console.log(`[${name}] build started`);
});
build.onEnd(result => {
result.errors.forEach(({ text, location }) => {
console.error(`✘ [ERROR][${name}] ${text}`);
console.error(` ${location.file}:${location.line}:${location.column}:`);
});
console.log(`[${name}] build finished`);
});
}
});
const makeCtx = (name, entryPoints, platform, split=true) => esbuild.context({
entryPoints,
bundle: true,
outdir: outDir,
minify: production,
sourcemap: !production,
sourcesContent: !production,
platform,
format: "esm",
splitting: split,
loader: {
".ttf": "file",
".node": "file",
"": "empty" //esbuild tries to parse a README in terminal kit for some goddamned reason, idk if its bc there are glob requires there. no fucking clue.
},
external,
define: {
WATCH: watch ? "true" : "false",
PROD: production ? "true" : "false"
},
plugins: [ esbuildProblemMatcherPlugin(name) ],
banner: platform=="browser" ? undefined : {
js: `
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const __dirname = import.meta.dirname;
`
},
});
await Promise.race([
makeCtx("webviews", ["src/activitybar.tsx", "src/panel.tsx", "src/testeditor.tsx"], "browser"),
makeCtx("extension", ["src/extension.ts", "src/main.ts"], "node"),
].map(async ctx => {
const a = await ctx;
if (watch) {
await a.watch();
} else {
//OH FUCK DO NOT MOVE DISPOSE OUTSIDE OF ELSE
await a.rebuild();
await a.dispose();
}
}));
}
main().catch(e => {
console.error(e);
process.exit(1);
});