|
1 | 1 | #!/usr/bin/env node |
2 | | -const path = require('path'); |
3 | | -const { spawn } = require('child_process'); |
4 | | -const { context } = require('esbuild'); |
5 | | - |
6 | | -const outfile = path.join(__dirname, 'sub-store.min.js'); |
7 | | - |
8 | | -let serverProcess = null; |
9 | | -let buildContext = null; |
10 | | -let shuttingDown = false; |
11 | | -let restartQueue = Promise.resolve(); |
12 | | - |
13 | | -function log(message) { |
14 | | - console.log(`[dev:esbuild] ${message}`); |
15 | | -} |
16 | | - |
17 | | -function startServer() { |
18 | | - const child = spawn(process.execPath, [outfile], { |
19 | | - cwd: __dirname, |
20 | | - stdio: 'inherit', |
21 | | - }); |
22 | | - |
23 | | - serverProcess = child; |
24 | | - log(`server started (pid ${child.pid})`); |
25 | | - |
26 | | - child.on('exit', (code, signal) => { |
27 | | - if (serverProcess === child) { |
28 | | - serverProcess = null; |
29 | | - } |
30 | | - |
31 | | - if (!shuttingDown && signal !== 'SIGTERM') { |
32 | | - log( |
33 | | - `server exited${code === null ? '' : ` with code ${code}`}${ |
34 | | - signal ? ` (${signal})` : '' |
35 | | - }`, |
36 | | - ); |
37 | | - } |
38 | | - }); |
39 | | -} |
40 | | - |
41 | | -async function stopServer() { |
42 | | - const child = serverProcess; |
43 | | - if (!child) { |
44 | | - return; |
45 | | - } |
46 | | - |
47 | | - serverProcess = null; |
48 | | - |
49 | | - if (child.exitCode !== null || child.signalCode !== null) { |
50 | | - return; |
51 | | - } |
52 | | - |
53 | | - await new Promise((resolve) => { |
54 | | - const timeout = setTimeout(() => { |
55 | | - child.kill('SIGKILL'); |
56 | | - }, 3000); |
57 | | - |
58 | | - child.once('exit', () => { |
59 | | - clearTimeout(timeout); |
60 | | - resolve(); |
61 | | - }); |
62 | | - |
63 | | - child.kill('SIGTERM'); |
64 | | - }); |
65 | | -} |
66 | | - |
67 | | -async function restartServer() { |
68 | | - if (serverProcess) { |
69 | | - log('restarting server'); |
70 | | - await stopServer(); |
71 | | - } |
72 | | - |
73 | | - if (!shuttingDown) { |
74 | | - startServer(); |
75 | | - } |
76 | | -} |
77 | | - |
78 | | -async function shutdown(signal) { |
79 | | - if (shuttingDown) { |
80 | | - return; |
81 | | - } |
82 | | - |
83 | | - shuttingDown = true; |
84 | | - log(`received ${signal}, shutting down`); |
85 | | - |
86 | | - if (buildContext) { |
87 | | - await buildContext.dispose(); |
88 | | - } |
89 | | - |
90 | | - await restartQueue.catch((error) => { |
91 | | - console.error(error); |
92 | | - }); |
93 | | - await stopServer(); |
94 | | -} |
| 2 | +const { build } = require('esbuild'); |
95 | 3 |
|
96 | 4 | !(async () => { |
97 | | - buildContext = await context({ |
98 | | - entryPoints: ['src/main.js'], |
99 | | - bundle: true, |
100 | | - minify: false, |
101 | | - sourcemap: false, |
102 | | - platform: 'node', |
103 | | - format: 'cjs', |
104 | | - outfile, |
105 | | - logOverride: { |
106 | | - 'direct-eval': 'silent', |
107 | | - }, |
108 | | - plugins: [ |
109 | | - { |
110 | | - name: 'restart-server-on-build', |
111 | | - setup(build) { |
112 | | - build.onStart(() => { |
113 | | - log('building'); |
114 | | - }); |
115 | | - |
116 | | - build.onEnd((result) => { |
117 | | - if (result.errors.length > 0) { |
118 | | - log(`build failed with ${result.errors.length} error(s)`); |
119 | | - return; |
120 | | - } |
121 | | - |
122 | | - log('build succeeded'); |
123 | | - restartQueue = restartQueue |
124 | | - .catch((error) => { |
125 | | - console.error(error); |
126 | | - }) |
127 | | - .then(() => restartServer()); |
128 | | - return restartQueue; |
129 | | - }); |
130 | | - }, |
| 5 | + const artifacts = [{ src: 'src/main.js', dest: 'sub-store.min.js' }]; |
| 6 | + |
| 7 | + for await (const artifact of artifacts) { |
| 8 | + await build({ |
| 9 | + entryPoints: [artifact.src], |
| 10 | + bundle: true, |
| 11 | + minify: false, |
| 12 | + sourcemap: false, |
| 13 | + platform: 'node', |
| 14 | + format: 'cjs', |
| 15 | + outfile: artifact.dest, |
| 16 | + logOverride: { |
| 17 | + 'direct-eval': 'silent', |
131 | 18 | }, |
132 | | - ], |
133 | | - }); |
134 | | - |
135 | | - process.on('SIGINT', () => { |
136 | | - shutdown('SIGINT').finally(() => process.exit(0)); |
137 | | - }); |
138 | | - process.on('SIGTERM', () => { |
139 | | - shutdown('SIGTERM').finally(() => process.exit(0)); |
140 | | - }); |
141 | | - |
142 | | - await buildContext.watch(); |
143 | | - log('watching for changes'); |
| 19 | + }); |
| 20 | + } |
144 | 21 | })() |
145 | | - .catch((error) => { |
146 | | - console.error(error); |
147 | | - process.exitCode = 1; |
| 22 | + .catch((e) => { |
| 23 | + console.log(e); |
| 24 | + }) |
| 25 | + .finally(() => { |
| 26 | + console.log('done'); |
148 | 27 | }); |
0 commit comments