Skip to content

Commit

Permalink
load file from FS
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Feb 15, 2024
1 parent ad354b3 commit 7795b5b
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Plugin> {
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 {
Expand All @@ -75,15 +75,15 @@ function instrument (filename: string, onConnect: (value: ViteDevServer) => void
const template = `
<!DOCTYPE html>
<html>
<script type="module" src="/@fs${instrumentation}"></script>
<script type="module">${instrumentation}</script>
${code}
${path.extname(filename) === '.html' ? `<script type="module">${sendFinishEvent}</script>` : ''}
`
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)
}
Expand Down

0 comments on commit 7795b5b

Please sign in to comment.