From ae9380856f6b9357eb541b78f14d0961c06882a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Grabarz?= Date: Tue, 22 Nov 2022 16:07:58 +0100 Subject: [PATCH] Remove unnecessary watcher in dev server (#3896) --- app/ide-desktop/lib/content/esbuild-config.ts | 18 ++++--- app/ide-desktop/lib/content/src/index.html | 5 +- app/ide-desktop/lib/content/watch.ts | 8 ++- app/ide-desktop/lib/server/package.json | 7 ++- app/ide-desktop/lib/server/src/index.mjs | 49 +++++++++++-------- app/ide-desktop/lib/server/src/live-reload.js | 9 ++++ 6 files changed, 60 insertions(+), 36 deletions(-) create mode 100644 app/ide-desktop/lib/server/src/live-reload.js diff --git a/app/ide-desktop/lib/content/esbuild-config.ts b/app/ide-desktop/lib/content/esbuild-config.ts index 1499b50ff7..eea68edf47 100644 --- a/app/ide-desktop/lib/content/esbuild-config.ts +++ b/app/ide-desktop/lib/content/esbuild-config.ts @@ -110,11 +110,6 @@ const config: esbuild.BuildOptions = { publicPath: '/assets', incremental: true, color: true, - watch: { - onRebuild(error, result) { - if (error) console.error('watch build failed:', error) - }, - }, logOverride: { // Happens in ScalaJS-generated parser (scala-parser.js): // 6 │ "fileLevelThis": this @@ -134,8 +129,17 @@ const config: esbuild.BuildOptions = { /** * Spawn the esbuild watch process. It continuously runs, rebuilding the package. */ -export async function watch() { - return esbuild.build(config) +export async function watch(onRebuild?: () => void, inject?: esbuild.BuildOptions['inject']) { + return esbuild.build({ + ...config, + inject: [...(config.inject ?? []), ...(inject ?? [])], + watch: { + onRebuild(error, result) { + if (error) console.error('watch build failed:', error) + else onRebuild?.() + }, + }, + }) } /** diff --git a/app/ide-desktop/lib/content/src/index.html b/app/ide-desktop/lib/content/src/index.html index e6a2469af7..b515b9b8ed 100644 --- a/app/ide-desktop/lib/content/src/index.html +++ b/app/ide-desktop/lib/content/src/index.html @@ -6,15 +6,12 @@ - res.statusCode < 400 })) + .use(serveStatic(root)) + .use('/assets', serveStatic(assets)) + + const server = app.listen(freePort) + const wsServer = new WebSocketServer({ server, clientTracking: true, path: '/live-reload' }) + + var serverUrl = `http://localhost:${freePort}` + console.log('Serving %s', serverUrl) + + return { + port: freePort, + reload() { + wsServer.clients.forEach(sock => sock.send('reload')) + }, } - console.log(`Server configuration:`, parameters) - const server = liveServer.start(parameters) - console.log(`Server started.`) - return parameters.port } diff --git a/app/ide-desktop/lib/server/src/live-reload.js b/app/ide-desktop/lib/server/src/live-reload.js new file mode 100644 index 0000000000..ad2a84eb6d --- /dev/null +++ b/app/ide-desktop/lib/server/src/live-reload.js @@ -0,0 +1,9 @@ +// This file is injected into every entry point, but it needs to run only once. +// A global variable is used to ensure that. +if (!window.live_reload_listening) { + window.live_reload_listening = true + const protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://' + const address = protocol + window.location.host + '/live-reload' + const socket = new WebSocket(address) + socket.onmessage = msg => msg.data == 'reload' && window.location.reload() +}