// vite.test.config.ts — Plan 01-11 two-bundle separation. // // Extends the production `./vite.config.ts` with two delta knobs: // 1. `mode: 'test'` — Vite statically replaces `import.meta.env.MODE` // everywhere in the input source with the string literal `'test'`. // The gated dynamic imports in src/background/index.ts + // src/offscreen/recorder.ts (Plan 01-11 Task 2) take the form // `if (import.meta.env.MODE === 'test') { await import('../test-hooks/...'); }`. // With mode='test' the comparison resolves to a live branch and // Rollup KEEPS the dynamic import; with the default mode='production' // the comparison is a static dead branch and Rollup tree-shakes the // `await import` away entirely (verified by the Tier-1 grep gate // `tests/background/no-test-hooks-in-prod-bundle.test.ts`). // 2. `build.outDir: 'dist-test'` + `emptyOutDir: true` — emit to a // SEPARATE directory so a `npm run build` immediately after this // build does not clobber. Puppeteer harness consumes this path via // `puppeteer.launch({ enableExtensions: [] })`. // // References: // - Vite mergeConfig: https://vite.dev/guide/api-javascript.html#mergeconfig // - Vite environment variables: https://vite.dev/guide/env-and-mode.html // - Rollup tree-shaking literal-comparison dead branches: // https://rollupjs.org/plugin-development/#how-rollup-handles-dynamic-imports import { defineConfig, mergeConfig, type UserConfigExport } from 'vite'; import baseConfig from './vite.config'; const baseAsExport: UserConfigExport = baseConfig; export default defineConfig(({ command, mode }) => mergeConfig( typeof baseAsExport === 'function' ? baseAsExport({ command, mode, isPreview: false, isSsrBuild: false }) : baseAsExport, { mode: 'test', // `define` performs a static text replacement at build time. We use a // dedicated `__MOKOSH_UAT__` token (NOT `import.meta.env.MODE`) for the // hook-import gate because vitest defaults its mode to 'test' too — // gating on MODE would activate the hooks during unit tests and // overwrite their vi.fn() mocks for chrome.notifications.create etc. // The dedicated token is `false` everywhere except in this test bundle // (Plan 01-11 RESEARCH §6 augmented — keeps Vite tree-shake semantics // intact while sidestepping the vitest cross-contamination). // Reference: https://vite.dev/config/shared-options.html#define define: { __MOKOSH_UAT__: 'true', }, build: { outDir: 'dist-test', emptyOutDir: true, }, }, ), );