Web Dev Solutions

Catalin Mititiuc

From 10b27a788d66d226b334c13e5892ce1c9b7b8ffa Mon Sep 17 00:00:00 2001 From: Catalin Mititiuc Date: Fri, 31 May 2024 13:41:06 -0700 Subject: WIP: implement build status plugin --- esbuild-server.mjs | 64 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 8 deletions(-) (limited to 'esbuild-server.mjs') diff --git a/esbuild-server.mjs b/esbuild-server.mjs index 15ee06d..323a97e 100644 --- a/esbuild-server.mjs +++ b/esbuild-server.mjs @@ -24,6 +24,55 @@ const mime = { '.html': 'text/html; charset=utf-8', }; +function createServerSentEventHandler() { + const listeners = new Set(); + const setupConnection = (req, res) => { + // res.writeHead(200, { + // 'Content-Type': 'text/event-stream', + // 'Cache-Control': 'no-cache', + // Connection: 'keep-alive', + // }); + listeners.add(res); + // req.on('close', () => { + // listeners.delete(res); + // }); + }; + + const sendMessage = (data) => { + listeners.forEach((res) => { + res.write(`data: ${JSON.stringify(data)}\n\n`); + }); + }; + + return { setupConnection, sendMessage }; +} + +const buildListeners = createServerSentEventHandler(); + +const buildStatusPlugin = { + name: 'build-status', + setup(build) { + let buildStart = Date.now(); + let buildResolver = () => {}; + let buildPromise = Promise.resolve(); + + build.onStart(() => { + buildStart = Date.now(); + buildPromise = new Promise((resolve) => { + buildResolver = resolve; + }); + buildListeners.sendMessage({ type: 'build-start' }); + }); + build.onEnd((result) => { + const duration = Date.now() - buildStart; + buildStart = -1; + buildResolver(); + const success = result.errors.length === 0; + buildListeners.sendMessage({ type: 'build-end', duration, success }); + }); + }, +}; + const importSvg = { name: 'importSvg', setup(build) { @@ -80,13 +129,6 @@ const importSvg = { resolveDir: path.dirname(args.path) //'./public/assets/images' } }); - - // build.onLoad({ filter: /.*/, namespace: 'svg-stub' }, async (args) => ({ - // contents: `import svg from ${JSON.stringify(args.path)} - // export default (imports) => - // WebAssembly.instantiate(wasm, imports).then( - // result => result.instance.exports)`, - // })); } }; @@ -174,11 +216,16 @@ const ctx = await esbuild.context({ entryPoints: ['src/index.js', 'src/soldier_record_block.js', 'src/map.js'], bundle: true, outdir: 'build', - plugins: [resolveSvgImports, externalSvgToInternal], + plugins: [ + resolveSvgImports, + externalSvgToInternal, + // buildStatusPlugin + ], loader: { '.svg': 'file' }, assetNames: 'assets/images/[name]-[hash]', + metafile: true }); await ctx.watch(); @@ -231,6 +278,7 @@ http.createServer((req, res) => { const type = proxyRes.headers['content-type']; console.log(`${req.method} ${req.url} => ${proxyRes.statusCode} ${type} via esbuild`); res.writeHead(proxyRes.statusCode, { 'content-type': type }); + // if (req.url === '/esbuild') buildListeners.setupConnection(req, res); proxyRes.pipe(res); }); -- cgit v1.2.3