2022-06-15 20:23:26 +02:00
|
|
|
import type esbuild from 'esbuild'
|
|
|
|
import type {IncomingMessage, ServerResponse} from 'http'
|
|
|
|
|
|
|
|
export function createEsbuildLiveReloadTools(): {
|
|
|
|
handleRequest(req: IncomingMessage, res: ServerResponse): boolean
|
|
|
|
hasOpenConnections(): boolean
|
|
|
|
esbuildBanner: esbuild.BuildOptions['banner']
|
|
|
|
} {
|
|
|
|
const openResponses = new Set<ServerResponse>()
|
|
|
|
return {
|
|
|
|
handleRequest(req, res) {
|
|
|
|
// If special /esbuild url requested, subscribe clients to changes
|
2023-02-07 18:50:57 +01:00
|
|
|
// if (req.url === '/esbuild') {
|
|
|
|
// res.writeHead(200, {
|
|
|
|
// 'Content-Type': 'text/event-stream',
|
|
|
|
// 'Cache-Control': 'no-cache',
|
|
|
|
// Connection: 'keep-alive',
|
|
|
|
// })
|
|
|
|
// res.write('data: open\n\n')
|
|
|
|
// openResponses.add(res)
|
|
|
|
// res.on('close', () => openResponses.delete(res))
|
|
|
|
// return true // handled
|
|
|
|
// }
|
2022-06-15 20:23:26 +02:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
hasOpenConnections() {
|
|
|
|
return openResponses.size > 0
|
|
|
|
},
|
|
|
|
esbuildBanner: {
|
|
|
|
// Below uses function toString to insert raw source code of the function into the JS source.
|
|
|
|
// This is being used so we can at least get a few type completions, but please understand that
|
|
|
|
// you cannot reference any non-global browser values from within the function.
|
|
|
|
js: `;(${function liveReloadClientSetup() {
|
2023-02-07 18:50:57 +01:00
|
|
|
console.log('%cLive reload enabled', 'color: gray')
|
2022-06-15 20:23:26 +02:00
|
|
|
// from packages/playground/devEnv/createEsbuildLiveReloadTools.ts
|
|
|
|
function connect() {
|
2022-06-22 20:36:33 +02:00
|
|
|
if (window.parent !== window) {
|
|
|
|
console.log(
|
|
|
|
'%cLive reload disabled for iframed content',
|
|
|
|
'color: gray',
|
|
|
|
)
|
|
|
|
}
|
2022-06-15 20:23:26 +02:00
|
|
|
try {
|
|
|
|
const es = new EventSource('/esbuild')
|
2023-02-07 18:50:57 +01:00
|
|
|
es.addEventListener('change', () => {
|
|
|
|
console.log('%cLive reload triggered', 'color: gray')
|
|
|
|
window.location.reload()
|
|
|
|
})
|
2022-06-15 20:23:26 +02:00
|
|
|
} catch (err) {
|
|
|
|
attemptConnect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function attemptConnect() {
|
|
|
|
setTimeout(() => connect(), 1000)
|
|
|
|
}
|
|
|
|
attemptConnect()
|
|
|
|
}.toString()})();`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|