From 7795b5bddb75c2b74b47586daa0fec3ad7ffa311 Mon Sep 17 00:00:00 2001 From: Christian Bromann Date: Thu, 15 Feb 2024 15:16:08 -0800 Subject: [PATCH] load file from FS --- src/server.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/server.ts b/src/server.ts index e6fa376..4c244be 100644 --- a/src/server.ts +++ b/src/server.ts @@ -31,7 +31,7 @@ export class ViteServer { this.#server = await createServer({ ...this.#options, - plugins: [instrument(filename, onConnectHandler)] + plugins: [await instrument(filename, onConnectHandler)] }) await this.#server.listen() return { @@ -46,12 +46,12 @@ export class ViteServer { } } -function instrument (filename: string, onConnect: (value: ViteDevServer) => void): Plugin { - const instrumentation = path.resolve(__dirname, 'browser', 'index.js') - const sendFinishEvent = `import.meta.hot?.send('bx:event', { name: 'doneEvent' })` +async function instrument (filename: string, onConnect: (value: ViteDevServer) => void): Promise { + const instrumentation = await fs.readFile(path.join(__dirname, 'instrumentation.js'), 'utf-8') + const sendFinishEvent = `requestAnimationFrame(() => import.meta.hot?.send('bx:event', { name: 'doneEvent' }))` return { name: 'instrument', - enforce: 'post', + enforce: 'pre', transform: (code, id) => { if (id === filename) { return { @@ -75,15 +75,15 @@ function instrument (filename: string, onConnect: (value: ViteDevServer) => void const template = ` - + ${code} ${path.extname(filename) === '.html' ? `` : ''} ` res.end(await server.transformIndexHtml(`${req.originalUrl}`, template)) }) - server.ws.on('connection', onConnect) - server.ws.on('bx:event', (message: ConsoleEvent) => { + server.hot.on('connection', onConnect) + server.hot.on('bx:event', (message: ConsoleEvent) => { if (message.name === 'consoleEvent') { return handleConsole(message) }