forked from thqby/vscode-autohotkey2-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.mjs
168 lines (159 loc) · 4.21 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
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
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,
};
/** @type {import('esbuild').BuildOptions} */
const client_opt = {
...server_opt,
entryPoints: ['client/src/extension.ts', 'client/src/test/*.ts'],
outbase: 'client/src',
outdir: 'client/dist',
};
/** @type {import('esbuild').BuildOptions} */
const util_opt = {
...server_opt,
entryPoints: ['util/src/*.ts'],
outbase: 'util/src',
// build in-place as files are only imported incrementally
outdir: 'util/src',
};
switch (process.argv[2]) {
case '--cli':
build_cli();
break;
case '--dev':
build_watch();
break;
case '--e2e':
build_e2e();
break;
case '--web':
build_watch(true);
break;
default:
build_prod();
break;
}
/** Build the server to be installed via CLI */
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`);
}
/** Build the client and server for e2e testing (include tests) */
async function build_e2e() {
const opts = [
client_opt,
server_opt,
{ ...client_opt, entryPoints: [client_opt.entryPoints.pop()] },
util_opt,
];
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`);
}
/** Build the client and server for production (excludes tests) */
async function build_prod() {
client_opt.entryPoints.pop(); // remove the test endpoints for prod
const opts = [client_opt, server_opt];
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`);
}
/** Build incrementally, non-bundling, for local development */
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.entryPoints.push('client/src/**/*.ts');
client_opt.logLevel = server_opt.logLevel = util_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, util_opt];
}
for (const opt of opts) {
opt.plugins = [plugin];
context(opt).then((ctx) => ctx.watch());
}
}
/**
* Returns the options for building the browser extension
* @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""}',
},
},
];
}