-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathesbuild-service.js
More file actions
59 lines (53 loc) · 1.04 KB
/
esbuild-service.js
File metadata and controls
59 lines (53 loc) · 1.04 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
import * as esbuild from 'esbuild';
/** @type {esbuild.Service} */
let inst,
svc,
timer,
usage = 0;
function free() {
if (--usage) return;
clearTimeout(timer);
timer = setTimeout(shutdown, 10);
}
function use() {
clearTimeout(timer);
usage++;
}
export function keepalive() {
use();
free();
}
export function shutdown() {
if (inst) inst.stop();
else if (svc) svc.then(inst => usage || inst.stop());
inst = svc = null;
}
process.on('beforeExit', shutdown);
/**
* This function is the same shape as Terser.minify, except that it is async.
* @param {string} code
* @param {esbuild.TransformOptions} opts
*/
export async function transform(code, opts) {
if (!inst) {
if (!svc) {
svc = ((esbuild && esbuild.default) || esbuild).startService();
}
inst = await svc;
}
use();
try {
const result = await inst.transform(code, opts);
return {
code: result.js,
map: result.jsSourceMap || null,
warnings: result.warnings.map(warning => warning.text)
};
} catch (err) {
return {
error: err
};
} finally {
free();
}
}