-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
esbuild.mjs
119 lines (114 loc) · 3.02 KB
/
esbuild.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
import { build, context } from 'esbuild';
/** @type {import('esbuild').BuildOptions} */
const server_opt = {
define: {
'process.env.BROWSER': 'false',
'process.env.DEBUG': 'false',
},
entryPoints: ['server/src/server.ts'],
format: 'cjs',
logLevel: 'error',
outbase: 'server/src',
outdir: 'server/dist',
platform: 'node',
sourcemap: true,
};
const client_opt = {
...server_opt,
entryPoints: ['client/src/extension.ts', 'client/src/test/*.ts'],
outbase: 'client/src',
outdir: 'client/dist',
};
switch (process.argv[2]) {
case '--cli': build_cli(); break;
case '--dev': build_watch(); break;
case '--web': build_watch(true); break;
default: build_prod(); break;
}
async function build_cli() {
server_opt.bundle = true;
server_opt.minify = true;
server_opt.entryPoints = ['server/cli/*.ts'];
server_opt.outbase = 'server/cli';
server_opt.outdir = 'server/cli';
server_opt.sourcemap = false;
const start = new Date;
await build(server_opt);
console.log(`build finished in ${new Date - start} ms`);
}
async function build_prod() {
const opts = [
client_opt, server_opt,
{ ...client_opt, entryPoints: [client_opt.entryPoints.pop()] }
];
client_opt.external = ['vscode'];
client_opt.bundle = server_opt.bundle = true;
client_opt.minify = server_opt.minify = true;
opts.push(...browser_opts(true));
const start = new Date;
await Promise.all(opts.map(o => build(o)));
console.log(`build finished in ${new Date - start} ms`);
}
function build_watch(web = false) {
let start, timer, opts;
/** @type {import('esbuild').Plugin} */
const plugin = {
name: 'esbuild-problem-matcher',
setup(build) {
build.onStart(() => {
start ??= (console.log('\x1bc[watch] build started'), new Date);
});
build.onEnd(result => {
const end = new Date;
result.errors.forEach(({ text, location }) => {
console.error(`✘ [ERROR] ${text}`);
console.error(` ${location.file}:${location.line}:${location.column}:`);
});
timer && clearTimeout(timer);
timer = setTimeout(() => {
console.log(`[watch] build finished in ${end - start} ms`);
start = timer = undefined;
}, 500);
});
}
};
client_opt.logLevel = server_opt.logLevel = 'silent';
server_opt.define['process.env.DEBUG'] = 'true';
if (web)
opts = browser_opts(false);
else {
server_opt.entryPoints = ['server/src/*.ts'];
server_opt.outdir = 'server/out';
opts = [client_opt, server_opt];
}
for (const opt of opts) {
opt.plugins = [plugin];
context(opt).then(ctx => ctx.watch());
}
}
/** @returns {Array<import('esbuild').BuildOptions>} */
function browser_opts(minify) {
const browser_opt = {
bundle: true,
define: {
...server_opt.define,
'process.cwd': 'process_cwd',
'process.env.BROWSER': 'true',
},
minify,
platform: 'browser',
};
return [{
...client_opt,
...browser_opt,
entryPoints: ['client/src/browserClientMain.ts'],
external: ['vscode'],
}, {
...server_opt,
...browser_opt,
entryPoints: ['server/src/browserServerMain.ts'],
footer: {
js: 'function process_cwd(){return""}'
},
}];
}