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
41 changes: 41 additions & 0 deletions src/Elastic.Documentation.Site/vite.config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @jest-environment node
*/
import type { UserConfig } from 'vite'

jest.mock('vite', () => ({ defineConfig: (config: unknown) => config }))
jest.mock('@tailwindcss/vite', () => () => ({ name: 'tailwindcss' }))
jest.mock('@vitejs/plugin-react', () => () => ({ name: 'react' }))
jest.mock('vite-plugin-svgr', () => () => ({ name: 'svgr' }))

const loadConfig = async (argv: string[]): Promise<UserConfig> => {
const originalArgv = process.argv
process.argv = ['node', 'vite', ...argv]
try {
let config: UserConfig = {}
await jest.isolateModulesAsync(async () => {
config = (await import('./vite.config')).default as UserConfig
})
return config
} finally {
process.argv = originalArgv
}
}

describe('vite.config build.watch', () => {
it('leaves watch unset for a one-shot build so `vite build` exits', async () => {
const config = await loadConfig(['build'])

expect(config.build?.watch).toBeNull()
})

it('excludes the build output under --watch so writes do not trigger a rebuild', async () => {
const config = await loadConfig(['build', '--watch'])

expect(config.build?.watch).toEqual(
expect.objectContaining({
exclude: expect.arrayContaining(['**/_static/**']),
})
)
})
})
6 changes: 6 additions & 0 deletions src/Elastic.Documentation.Site/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export default defineConfig({
sourcemap: true,
// Razor resolves `main.js` to the hashed entry through `.vite/manifest.json`.
manifest: true,
// Tailwind's content globs match the built `_static/*.js`, and the plugin watches every
// scanned file, so without this each `vite build --watch` output triggers another build.
// Only set under --watch: any non-null value turns a plain `vite build` into a watcher.
watch: process.argv.includes('--watch')
? { exclude: ['**/_static/**', '**/node_modules/**'] }
Comment on lines +35 to +37

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: P3 (Low)

Add automated regression coverage for the build/watch lifecycle. This fix has no accompanying test that verifies a normal build exits, watch mode settles after writing _static, and a source edit triggers another build. Those are the behaviors this change must preserve, and without coverage the rebuild loop or a hanging frontend build can return unnoticed.

Generated by Libra. React with 👍 or 👎 to give feedback on this comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added vite.config.test.ts in c90ecf1. It covers both regressions at the config level: a one-shot vite build keeps build.watch unset (otherwise the MSBuild step hangs), and --watch excludes _static/** (otherwise every build triggers the next). I checked that it fails when watch is forced on. A real vite build --watch lifecycle test, which edits a file and counts rebuilds, would need several seconds of real Vite and Tailwind builds and would be flaky, so I left it out of the Jest suite.

: null,
rollupOptions: {
input: {
main: path.resolve(__dirname, 'Assets/main.ts'),
Expand Down
Loading