Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/utils/vite.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { join } from 'node:path'
import { isVoidProject, loadVoidApp, startVite } from './vite'
import { isViteNoise, isVoidProject, loadVoidApp, startVite } from './vite'

const FIXTURE = join(import.meta.dirname, 'fixtures/vite')
const VOID_FIXTURE = join(import.meta.dirname, 'fixtures/void')
Expand Down Expand Up @@ -62,3 +62,17 @@ describe('startVite', () => {
expect(isVoidProject()).toBe(false)
})
})

describe('isViteNoise', () => {
it("drops Vite's own notes and keeps the app logs", () => {
expect(isViteNoise('Re-optimizing dependencies because vite config has changed')).toBe(true)
expect(isViteNoise('Default inspector port 9229 not available, using 9232 instead')).toBe(true)
expect(
isViteNoise(
'Sourcemap for "/a/node_modules/x/index.mjs" points to a source file outside its package: "/a/src/x.ts"'
)
).toBe(true)
expect(isViteNoise('GET /users 200')).toBe(false)
expect(isViteNoise('Error: something broke')).toBe(false)
})
})
15 changes: 14 additions & 1 deletion src/utils/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,25 @@ const loadVite = async (): Promise<ViteModule> => {
* that a Vite plugin builds (with virtual modules) can take requests.
* The server listens on a random port on 127.0.0.1 only.
*/
const VITE_NOISE = [
/^Sourcemap for .* points to (a source file outside its package|missing source files)/,
/^Re-optimizing dependencies because/,
/^Default inspector port \d+ not available/,
]

/** Vite's own notes that say nothing about the app */
export const isViteNoise = (msg: string): boolean => VITE_NOISE.some((noise) => noise.test(msg))

export const startVite = async (): Promise<ViteTarget> => {
const { createLogger, createServer: createViteServer } = await loadVite()
// Keep the app logs off stdout, which is for the JSON output. An app
// in workerd logs through this logger; an app in this process (e.g.
// @hono/vite-dev-server) calls console.log, so `request` wraps it.
const toStderr = (msg: string) => console.error(msg)
const toStderr = (msg: string) => {
if (!isViteNoise(msg)) {
console.error(msg)
}
}
// Plugins may log with console.log while the server starts (Void does)
const vite = await logsToStderr(() =>
createViteServer({
Expand Down
Loading