import { readFileSync } from 'node:fs'; import { createRequire } from 'node:module'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { types } from 'node:util'; import { ignoreInput, ignoreOutput } from '@voidzero-dev/vite-task-client'; import type { OxfmtConfig } from 'oxfmt'; import type { OxlintConfig } from 'oxlint'; import type { Alias, InlineConfig as ViteInlineConfig, Plugin, PluginOption, ResolvedConfig, UserConfig, } from 'vite'; import { resolveConfig, searchForWorkspaceRoot } from 'vite'; import type { ConfigEnv, TestProjectConfiguration, UserProjectConfigExport, UserProjectConfigFn, UserWorkspaceConfig, } from 'vitest/config'; import { defineConfig as viteDefineConfig, defineProject as viteDefineProject, } from 'vitest/config'; import type { InlineConfig as VitestInlineConfig } from 'vitest/node'; import type { CreateTemplateEntry } from './create/org-manifest.ts'; import type { PackUserConfig } from './pack.ts'; import type { RunConfig } from './run-config.ts'; import type { StagedConfig } from './staged-config.ts'; import { CONFIG_METADATA_ENV, VITEST_VERSION } from './utils/constants.ts'; import type { JsonObject } from './utils/json-value.ts'; import { parseJsonObject, isObjectValue } from './utils/json-value.ts'; import { VITEST_RESOLVER_PACKAGES } from './utils/vitest-ecosystem.ts'; declare module 'vite' { interface UserConfig { /** * Options for oxlint */ lint?: OxlintConfig; fmt?: OxfmtConfig; /** * Defaults for the `vp check` composite command. Each flag mirrors the * matching CLI option (`--no-fmt` / `--no-lint`) and only affects * `vp check`; standalone `vp fmt` / `vp lint` are unaffected. */ check?: { /** * Run the format step in `vp check`. * @default true */ fmt?: boolean; /** * Run the lint step in `vp check`. Type-check still runs when both * `lint.options.typeAware` and `lint.options.typeCheck` are enabled. * @default true */ lint?: boolean; }; pack?: PackUserConfig | Array; /** * Default target directory for `vp dev` / `build` / `preview` / `pack` * when invoked bare in the directory containing this config (an implicit * `vp -C `). A string targets all four commands; an object maps * commands individually, and a command absent from the object falls * through to the normal picker/listing resolution. Paths are relative to * the config file's directory. vp reads them without executing the * config, so this also works at roots with no vite-plus dependency; the * static read is why the values must stay plain string literals. */ defaultPackage?: string | { dev?: string; build?: string; preview?: string; pack?: string }; run?: RunConfig; staged?: StagedConfig; /** * Options for `vp create`. * * See `rfcs/create-org-default-templates.md` for the full specification. */ create?: { /** * When `vp create` is invoked with no template argument, use this * value as if the user had typed it — typically a scope like * `'@your-org'` paired with a `@your-org/create` package that exposes a * `createConfig.templates` manifest. Can also name a local * `create.templates` entry. */ defaultTemplate?: string; /** * Local templates available to `vp create` inside this monorepo. Each * entry is shown in the `vp create` picker by `name`/`description`; its * `template` resolves like any specifier (a workspace package name, a * relative `./path`, a `vite:*` builtin, a GitHub URL, or an npm package). */ templates?: Array; }; /** * Vitest may augment a separate Vite copy, for example an npm peer dependency. * Keep the public test field on the CLI's core types in that layout too. */ test?: VitestInlineConfig; } } type ViteUserConfigFnObject = (env: ConfigEnv) => UserConfig; type ViteUserConfigFnPromise = (env: ConfigEnv) => Promise; type ViteUserConfigFn = (env: ConfigEnv) => UserConfig | Promise; type ViteUserConfigExport = | UserConfig | Promise | ViteUserConfigFnObject | ViteUserConfigFnPromise | ViteUserConfigFn; type DefinedViteUserConfig = T extends ViteUserConfigFnPromise ? ViteUserConfigFnPromise : T extends ViteUserConfigFnObject ? ViteUserConfigFnObject : T extends ViteUserConfigFn ? ViteUserConfigFn : T extends Promise ? Promise : UserConfig; /** * `require` anchored at THIS module's location so `require.resolve` reaches * the `vitest` / `@vitest/*` family that the `vite-plus` package directly * depends on — even from a consumer project where they are only transitive. * Used to locate the bundled `vitest` package (its `package.json`), NOT to * resolve module ENTRIES: `require.resolve` applies the `require` export * condition, which selects Vitest's CJS entries — for the bare `vitest` root * a throw-stub (`index.cjs` — "Vitest cannot be imported … using require()"), * and for subpaths the CJS build (e.g. `vitest/config` → `config.cjs`) rather * than the ESM entry the test server's module graph needs. Module entries are * resolved through Vite's own resolver instead (see * [[vitePlusVitestResolverPlugin]]), which honours ESM conditions. * * `define-config.ts` is bundled by tsdown in BOTH formats: ESM (`shims: true`, * which defines a module-scoped `__dirname`) and CJS (where `__dirname` is the * Node global). The guard picks `__dirname` whenever it exists and otherwise * falls back to `import.meta.url`; tsdown rewrites the latter to * `pathToFileURL(__filename).href` in the CJS bundle, so it is safe in both. */ const vitePlusRequire = createRequire( typeof __dirname !== 'undefined' ? __dirname : import.meta.url, ); /** * Absolute path to THIS module, used as a `this.resolve` importer so Vite's * resolver roots the `vitest` / `@vitest/*` family at `vite-plus`'s own * location — reaching its direct deps (`vitest`, `vitest/*`, `@vitest/browser*`) * even from a consumer project where they are only transitive. * * `import.meta.url` is native in the ESM bundle and rewritten by tsdown to * `pathToFileURL(__filename).href` in the CJS bundle, so it is a valid file URL * in both. */ // oxlint-disable-next-line unicorn/prefer-import-meta-properties -- The CommonJS build rewrites import.meta.url for this dual-format module. const vitePlusModuleFile = fileURLToPath(import.meta.url); /** * Absolute path to the bundled `vitest` package's `package.json`, used as a * second `this.resolve` importer. Supported transitive packages can be * reachable from `vitest`'s location but not from [[vitePlusModuleFile]] * under pnpm's isolated layout. * Resolving `package.json` is condition-agnostic, so this is safe with * `require.resolve`. Cached; `null` once an attempt has failed so we never retry. */ let vitestAnchor: string | null | undefined; function getVitestAnchor(): string | null { if (vitestAnchor !== undefined) { return vitestAnchor; } try { vitestAnchor = vitePlusRequire.resolve('vitest/package.json'); } catch { vitestAnchor = null; } return vitestAnchor; } function isAliasArray( value: ReadonlyArray | Record | undefined, ): value is ReadonlyArray { return Array.isArray(value); } // The bundled manifest is fixed for this module's lifetime. Share it across // browser entry points and project configs, just like the cached package path. let vitestExports: JsonObject | undefined; function getVitestImportTarget(id: string): string { const anchor = getVitestAnchor(); if (!anchor) { throw new Error('Cannot resolve the bundled Vitest package. Reinstall vite-plus.'); } if (!vitestExports) { const { exports } = parseJsonObject(readFileSync(anchor, 'utf8')); if (!isObjectValue(exports)) { throw new Error('Expected the bundled Vitest manifest to define exports.'); } vitestExports = exports; } let target = vitestExports[id === 'vitest' ? '.' : `.${id.slice('vitest'.length)}`]; while (isObjectValue(target)) { target = target.import ?? target.default; } if (typeof target !== 'string' || !target.startsWith('./')) { throw new Error(`The bundled Vitest has no ESM export for ${id}.`); } return resolve(anchor, '..', target); } /** * Match the `vitest` / `@vitest/*` family of bare specifiers — the imports a * browser-mode Vite dev server must resolve. Any query string is stripped * first; relative (`./`), absolute (`/`), and virtual (`\0`) ids never match. * * Exported for unit testing. */ export function isVitestFamilySpecifier(id: string): boolean { const bare = id.split('?')[0]; if (bare.startsWith('.') || bare.startsWith('/') || bare.startsWith('\0')) { return false; } const segments = bare.split('/'); const packageName = bare.startsWith('@') ? segments.slice(0, 2).join('/') : segments[0]; return VITEST_RESOLVER_PACKAGES.has(packageName); } /** * Rescue `vitest` / `@vitest/*` resolution for browser-mode tests. * * In an established project that depends only on `vite-plus`, both `vitest` * and `@vitest/browser` are transitive deps. pnpm's isolated layout only * exposes a package's *direct* deps, so the browser-mode Vite dev server * (rooted at the consumer project) cannot resolve `vitest/internal/browser`, * `@vitest/mocker`, etc. Non-browser tests are unaffected — vitest's own * module runner handles resolution there. * * This plugin re-resolves the explicit Vitest runtime allowlist through Vite's OWN * resolver, but ROOTED at `vite-plus`'s location ([[vitePlusModuleFile]]) and * then the bundled `vitest`'s location ([[getVitestAnchor]]) BEFORE the * project. So every such import binds to the same physical (pinned) Vitest that * `vp test` spawns as the runner (see `resolveBundled` in `resolve-test.ts`) * and that the `vite-plus/test*` shims re-export. Were a project-local Vitest * preferred instead, a project that keeps its own `vitest` dependency would * split the run across two physical Vitest module instances — the runner * (bundled) vs. the test files' `vi`/`expect`/runner internals (project) — a * classic source of internal-state and mock-hoisting mismatches. For the common * migrated layout (a project depending only on `vite-plus`) nothing in this * family is resolvable from the project root under pnpm's isolated layout * anyway, so default resolution would return `null` there regardless; * bundle-first only changes the project-keeps-its-own-`vitest` case, which is * exactly the case we want pinned. * * Resolution goes through `this.resolve` (NOT [[vitePlusRequire]].resolve) so * Vite's ESM export conditions are honoured: a raw `require.resolve` would pick * Vitest's CJS `require`-condition entry — a throw-stub for the bare `vitest` * root (`index.cjs`), and the CJS build for subpaths (e.g. `vitest/config` → * `config.cjs`) instead of the ESM entry. Two bundled anchors are tried because `@vitest/browser*` are * direct deps of `vite-plus` (reachable from [[vitePlusModuleFile]]) while the * nested `@vitest/*` family are deps of `vitest` (reachable only from the * `vitest` anchor). The project root remains the last resort for any family id * the bundled tree cannot resolve, so this is never worse than deferring first. * * Two intentional limits of routing through `this.resolve`: * - An EXPLICIT project `resolve.alias` / `resolve.dedupe` on the vitest * family takes precedence (Vite's pipeline applies it even from a bundled * anchor). Neither is set by default in Vitest 4.x, so this only affects * projects that deliberately re-point the family — treated as an opt-out of * pinning, not defeated silently. * - Coverage providers (`@vitest/coverage-v8` / `-istanbul`) are NOT shipped * with `vite-plus`, so they hit the project fallback below. Under * `--coverage`, a project-installed provider of a different version pairs * with the bundled runner; Vitest only WARNS on the version skew and then * runs mixed versions (its provider `_initialize` logs and continues, it * does not throw), which silently yields unreliable coverage — so * [[vitePlusCoverageVersionGuardPlugin]] fails fast on a mismatch instead. */ function vitePlusVitestResolverPlugin(): PluginOption { let isVitestServer = false; return { name: 'vite-plus:vitest-resolver', enforce: 'pre', config: { order: 'post', handler(config, { command }) { // Vitest adds this environment before post config hooks, including for // programmatic runners and child projects. A browser test config alone // must not redirect application dependencies during `vp dev` or `vp build`. isVitestServer = command === 'serve' && !!config.environments?.['__vitest__']; if (!isVitestServer || !config.test?.browser?.enabled) { return; } // The browser optimizer resolves forced includes with an alias-only // resolver, without user resolveId hooks. Keep its runtime entry points // on the same graph as the browser runner in isolated installs. const aliases = config.resolve?.alias; const entries: Array = isAliasArray(aliases) ? [...aliases] : Object.entries(aliases ?? {}).map(([find, replacement]) => ({ find, replacement })); const anchor = getVitestAnchor(); if (!anchor) { throw new Error('Cannot resolve the bundled Vitest package. Reinstall vite-plus.'); } const runtimeRequire = createRequire(anchor); const targets = new Map( [ 'vitest', 'vitest/internal/browser', 'vitest/internal/traces', 'expect-type', 'magic-string', 'chai', 'vite/module-runner', ].map((id) => [ id, id.startsWith('vitest') ? getVitestImportTarget(id) : runtimeRequire.resolve(id), ]), ); if (config.test.browser.provider?.name === 'preview') { const previewRequire = createRequire(vitePlusRequire.resolve('@vitest/browser-preview')); // Preview's forced includes need the same isolated-layout fallback. // These entries may be CommonJS; the browser optimizer converts them. for (const id of ['@testing-library/user-event', '@testing-library/dom']) { targets.set(id, previewRequire.resolve(id)); } } for (const [id, replacement] of targets) { if ( entries.some(({ find }) => typeof find === 'string' ? id === find || id.startsWith(`${find}/`) : new RegExp(find).test(id), ) ) { continue; } entries.push({ find: new RegExp(`^${id}$`), replacement, }); } config.resolve ??= {}; config.resolve.alias = entries; }, }, configResolved(config) { // Vite merges plugin arrays from inherited and referenced configs. Remove // duplicate Vite+ hooks before it creates the server's plugin container. const seen = new Set(); // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Vite exposes resolved plugins as readonly; deduplication must preserve the array identity. const plugins = config.plugins as Array; const unique = plugins.filter((plugin) => { if (!VITE_PLUS_TEST_PLUGIN_NAMES.has(plugin.name)) { return true; } if (seen.has(plugin.name)) { return false; } seen.add(plugin.name); return true; }); plugins.splice(0, plugins.length, ...unique); }, resolveId: { // Keep the allowlist in the handler; skip unrelated ids before the handler runs. filter: { id: /^(?:vitest(?:\/|$)|@vitest\/|vite-plus\/test$)/ }, async handler(id, importer, options) { if (isVitestServer && id === 'vite-plus/test') { // pnpm can install separate vite-plus/Vitest peer instances for a // workspace's root and children. Externalizing the child wrapper lets // Node load its uninitialized runner state, bypassing Vite's hooks. // Resolve the API before externalization through Vitest's own resolver: // it selects the active runner (or the browser's optimized instance), // not the Vitest copy next to this config's vite-plus installation. return this.resolve('vitest', importer, { ...options, skipSelf: true }); } if (!isVitestFamilySpecifier(id)) { return null; } // The redirected imports are all clean bare specifiers; a queried id is // outside the scope of this resolver — let the default resolver handle it. if (id.includes('?')) { return null; } // Re-resolve from vite-plus's own (pinned) tree via Vite's resolver so the // runner and every imported Vitest module are a single physical instance. // `skipSelf` avoids infinite recursion back into this plugin. const vitestAnchorPath = getVitestAnchor(); const bundledAnchors = vitestAnchorPath ? [vitePlusModuleFile, vitestAnchorPath] : [vitePlusModuleFile]; for (const anchor of bundledAnchors) { const resolved = await this.resolve(id, anchor, { ...options, skipSelf: true }); if (resolved) { return resolved; } } // Last resort: default project-rooted resolution for any family id the // bundled tree cannot resolve (e.g. coverage providers not shipped with // vite-plus). return this.resolve(id, importer, { ...options, skipSelf: true }); }, }, }; } /** * Packages that register Vitest `expect` matchers via `expect.extend()` from * a side-effect import. When Vite serves these from a separate module graph * than the test runtime, the matchers register on a different `expect` * instance and `expect(...).` is undefined at call time (vitest * issue #897). Inlining them into the test server's module graph forces * registration on the same instance. * * Only packages that are **installed** in the consumer project are inlined. * Absent packages are silently skipped so the server-deps optimizer never * tries to resolve a name that does not exist in the project's node_modules. * * The environment hook adds installed packages to the module-runner options * inherited by shared-server projects. The resolved-config hook also handles * independent project servers, using each project's resolved root. * * Exported for unit testing. */ export const AUTO_INLINE_DEPS: ReadonlyArray = [ '@testing-library/jest-dom', '@storybook/test', 'jest-extended', ]; /** * Compute the merged `test.server.deps.inline` list for a given project root, * appending only those entries from [[AUTO_INLINE_DEPS]] that are actually * installed in the project. * * Returns `null` when nothing needs to change (either `inline: true` or an * empty result), so the caller can skip the mutation step. * * Exported for unit testing. The `_createRequire` parameter lets tests inject * a controlled resolver without needing to spy on Node's ESM module namespace. * Callers may share installation results for the same root within one config load. */ export function computeAutoInlineList( existingInline: Array | true | undefined, projectRoot: string, _createRequire: (from: string) => { resolve: (id: string) => string } = createRequire, installedPackages = new Map(), ): Array | null { // User opted into "inline everything" — don't touch. if (existingInline === true) { return null; } // Build a require resolver anchored at the project root so we only // inline packages that are actually installed there. let projectRequire: ReturnType | undefined; // Start from a copy of the user-supplied array (or a fresh array when // none was provided) so the originating user-config object is not mutated. const merged: Array = Array.isArray(existingInline) ? [...existingInline] : []; for (const pkg of AUTO_INLINE_DEPS) { // Skip if already covered by a string or regexp entry. if (merged.some((entry) => entry === pkg || (types.isRegExp(entry) && entry.test(pkg)))) { continue; } let installed = installedPackages.get(pkg); if (installed === undefined) { projectRequire ??= _createRequire(`${projectRoot}/package.json`); try { projectRequire.resolve(pkg); installed = true; } catch { installed = false; } installedPackages.set(pkg, installed); } if (!installed) { continue; } merged.push(pkg); } // Return null when there's nothing new to inline so callers can bail early. const hadEntries = Array.isArray(existingInline) ? existingInline.length : 0; if (merged.length === hadEntries) { return null; } return merged; } function vitePlusAutoInlineMatcherPlugin(): PluginOption { let projectRoot = ''; let isVitestServer = false; const installedByRoot = new Map>(); type TestConfig = Pick; function inlineList(existing: Array | true | undefined, root: string) { root = resolve(root); let installed = installedByRoot.get(root); if (!installed) { installed = new Map(); installedByRoot.set(root, installed); } return computeAutoInlineList(existing, root, createRequire, installed); } return { name: 'vite-plus:auto-inline-matcher-deps', enforce: 'pre', config: { order: 'post', handler(config, { command }) { isVitestServer = command === 'serve' && !!config.environments?.['__vitest__']; projectRoot = resolve(config.root ?? '.'); // Reuse positive and negative lookups across environments and the // resolved-config hook, but refresh them when Vite reloads the config. installedByRoot.clear(); }, }, configEnvironment(_name, config) { if (!isVitestServer) { return; } const existing = config.resolve?.noExternal; const merged = inlineList( typeof existing === 'string' || types.isRegExp(existing) ? [existing] : existing, projectRoot, ); if (merged !== null) { config.resolve ??= {}; config.resolve.noExternal = merged; } }, configResolved(resolvedConfig) { if (!isVitestServer) { return; } const config = resolvedConfig as { root: string; test?: TestConfig }; const merged = inlineList(config.test?.server?.deps?.inline, config.root); if (merged !== null) { config.test ??= {}; config.test.server ??= {}; config.test.server.deps ??= {}; config.test.server.deps.inline = merged; } }, }; } /** Coverage providers vite-plus can version-check against the bundled runner. */ const KNOWN_COVERAGE_PROVIDERS: ReadonlySet = new Set(['v8', 'istanbul']); /** * Resolve the coverage provider package name that should be version-checked, or * `null` when no check applies (coverage off, or a `custom`/unknown provider * vite-plus does not bundle a runner for). * * Takes Vitest's OWN resolved coverage options (`enabled`/`provider`), which the * `configureVitest` hook exposes AFTER Vitest's CLI parser has run — so the * `--coverage` family of flags is already folded into `enabled`/`provider` and * we never re-parse `process.argv` ourselves. Unset `provider` defaults to `v8` * (Vitest's default). * * Exported for unit testing. */ export function resolveCoverageProviderToCheck( coverage: { enabled?: boolean; provider?: string } | undefined, ): string | null { if (!coverage?.enabled) { return null; } const name = coverage.provider ?? 'v8'; return KNOWN_COVERAGE_PROVIDERS.has(name) ? `@vitest/coverage-${name}` : null; } /** * vite-plus bundles `vitest@VITEST_VERSION` as the test runner, but coverage * providers (`@vitest/coverage-v8` / `-istanbul`) are project-installed peer * deps it does not ship. Vitest only PRINTS A WARNING on a provider/runner * version skew and then runs mixed versions (verified in 4.1.9: the provider's * `_initialize` calls `logger.warn`, it never throws), silently producing * unreliable coverage. Fail fast instead. * * Exported for unit testing. */ export function assertCoverageProviderVersionMatch( providerPackageName: string, installedVersion: string | null | undefined, expectedVersion: string = VITEST_VERSION, ): void { if (installedVersion && installedVersion !== expectedVersion) { throw new Error( `vite-plus bundles vitest@${expectedVersion}, but ${providerPackageName}@${installedVersion} ` + `is installed. A coverage provider must match the test runner version: Vitest only prints a ` + `warning on a mismatch and then runs mixed versions, which produces unreliable coverage. ` + `Pin ${providerPackageName} to ${expectedVersion} in your dependencies.`, ); } } /** * The bundled vitest's `package.json` path — the SAME anchor Vitest's own * `import('@vitest/coverage-*')` resolves against (its dist walks up from here). * Used as a fallback resolution anchor for the coverage guard. Lazily computed * and cached; `null` when the bundled vitest is somehow unreachable, in which * case the guard simply relies on the project-root anchor. */ let bundledVitestAnchorCache: string | null | undefined; function bundledVitestAnchor(): string | null { if (bundledVitestAnchorCache === undefined) { try { bundledVitestAnchorCache = createRequire(import.meta.url).resolve('vitest/package.json'); } catch { bundledVitestAnchorCache = null; } } return bundledVitestAnchorCache; } /** * Read a project-installed coverage provider's version, mirroring how Vitest * itself resolves the provider. Vitest install-checks it from BOTH the runner * root AND its own bundled dir (`isPackageExists(dep, {paths:[root, vitestDir]})`) * and then loads it via a bare `import('@vitest/coverage-*')` anchored at that * bundled dir. So the guard tries the project root FIRST — the supported layout * where a directly-declared provider is symlinked at the root, the same copy the * bundled vitest walks up to — then falls back to the bundled-vitest anchor, * which catches hoisted / pnpm peer-set layouts where the provider lives next to * vitest but is not resolvable from the project root (a silent skip otherwise). * `@vitest/coverage-*`'s exports map has a `"./*": "./*"` catch-all, so * `./package.json` is resolvable. Returns `null` when no anchor can resolve it — * Vitest then emits its own (already clear) missing-provider error. * * The `_createRequire` / `_readFile` parameters let tests inject controlled * resolvers without spying on Node's module/fs namespaces. */ function readInstalledCoverageProviderVersion( providerPackageName: string, projectRoot: string, _createRequire: (from: string) => { resolve: (id: string) => string } = createRequire, _readFile: (path: string) => string = (path) => readFileSync(path, 'utf8'), ): string | null { const anchors = [`${projectRoot}/package.json`, bundledVitestAnchor()]; for (const anchor of anchors) { if (!anchor) { continue; } try { const req = _createRequire(anchor); const pkgJsonPath = req.resolve(`${providerPackageName}/package.json`); const parsed = parseJsonObject(_readFile(pkgJsonPath)); if (typeof parsed.version === 'string' && parsed.version) { return parsed.version; } } catch { // This anchor can't resolve the provider — try the next one. } } return null; } /** * Orchestrates the coverage version guard: detect the active provider from * Vitest's resolved coverage options, read its installed version from the * project root, and throw on a mismatch. A no-op when coverage is off or the * provider is not installed. * * Exported (with injectable `deps`) for unit testing. */ export function checkCoverageProviderVersion( coverage: { enabled?: boolean; provider?: string } | undefined, projectRoot: string, deps: { createRequire?: (from: string) => { resolve: (id: string) => string }; readFile?: (path: string) => string; } = {}, ): void { const providerPackageName = resolveCoverageProviderToCheck(coverage); if (!providerPackageName) { return; } const installedVersion = readInstalledCoverageProviderVersion( providerPackageName, projectRoot, deps.createRequire, deps.readFile, ); assertCoverageProviderVersionMatch(providerPackageName, installedVersion); } interface ResolvedCoverageView { root: string; coverage?: { enabled?: boolean; provider?: string }; } /** * The shared Vitest instance, narrowed to what the late-path guard touches. Its * `enableCoverage()` is the programmatic/watch/UI path that turns coverage on * AFTER `configureVitest` and creates the provider lazily — wrapping it lets the * same skew check guard that path. */ interface VitestInstanceView { config: ResolvedCoverageView; enableCoverage: () => Promise; } /** * The `configureVitest` plugin hook context, narrowed to the fields the guard * reads. Vitest passes more (incl. a per-project `project`), but coverage is a * single, GLOBAL runner concern: Vitest creates ONE provider from the shared * runner config and imports it from the runner context — never per project. So * the guard reads the shared `vitest.config` (its `coverage.{enabled,provider}` * already reflects the `--coverage` CLI flags, unlike a `configResolved` Vite * hook) and resolves the provider from `vitest.config.root` (the workspace/ * runner root), which resolves to the exact provider copy Vitest loads. Reading * the per-project `project.config.root` would miss a root-level skew or * false-positive on a leaf-local provider the runner never loads. */ interface VitestConfigureContext { vitest: VitestInstanceView; } /** * Vitest instances the guard has already handled. The `configureVitest` hook * fires once PER PROJECT but `vitest` is shared and coverage is global, so this * runs the whole guard exactly once per instance (and never leaks, since it is * keyed weakly on identity). */ const coverageGuardedVitestInstances = new WeakSet(); function vitePlusCoverageVersionGuardPlugin(): PluginOption { return { name: 'vite-plus:coverage-version-guard', // Vitest invokes this hook via `getSortedPluginHooks('configureVitest')`. configureVitest(context: VitestConfigureContext) { const { vitest } = context; // Coverage is global and the provider is imported once from the runner, so // run the guard once per shared instance even though this hook fires per // project. if (coverageGuardedVitestInstances.has(vitest)) { return; } coverageGuardedVitestInstances.add(vitest); // STARTUP path: `vitest run --coverage` / config `coverage.enabled` — the // only path `vp test`/CI take. `vitest.config.coverage` is the resolved // runner config; `vitest.config.root` anchors the provider resolution. checkCoverageProviderVersion(vitest.config.coverage, vitest.config.root); // LATE path: Vitest's programmatic `enableCoverage()` (node API / watch / // UI toggle) flips coverage on after this hook and creates the provider // lazily, so the startup check above already returned. Wrap it so the same // skew check runs before the provider loads. The wrapper always delegates. const enableCoverage = vitest.enableCoverage.bind(vitest); vitest.enableCoverage = async () => { // `enableCoverage()` sets enabled:true then creates the provider; force // enabled so the check does not early-return, and read the provider from // the live runner config (the late-path source of truth). checkCoverageProviderVersion( { enabled: true, provider: vitest.config.coverage?.provider }, vitest.config.root, ); return enableCoverage(); }; }, } as PluginOption; } /** * The `configureVitest` plugin hook context, narrowed to the fields the cache * path plugin reads: the shared runner's resolved Vite config (`cacheDir`, * `root`), the resolved `fsModuleCachePath` overrides, and the attached * projects (each carrying its own resolved `fsModuleCachePath`). */ interface VitestCachePathContext { vitest: { viteConfig: { cacheDir: string; root: string }; config: { fsModuleCachePath?: string }; projects: Array<{ config: { fsModuleCachePath?: string } }>; }; } /** * Vitest 5.0.4 enables its on-disk caches by default: the results cache * (`/results.json`, written atomically through `.tmp-*` siblings) * and the fs module cache (`/node_modules/.vitest-cache`, or * `fsModuleCachePath` when overridden). Under `vp run`, file-system tracking * would record those cache reads/writes as task inputs the run itself * modifies, so every `vp test` task would stay permanently uncacheable * ("not cached because it modified its input"). Tell the runner these * tool-managed paths are neither inputs nor outputs, mirroring what Vite * does for `.vite-temp` and the optimizer cache dir. * * Runtime project injection appends to `vitest.projects` without calling * `configureVitest` again. Observe those attachments as well as the initial * list. Both ignore calls are no-ops outside `vp run`. */ function vitePlusVitestCachePathsPlugin(): PluginOption { const observedProjectLists = new WeakSet(); return { name: 'vite-plus:vitest-cache-paths', configureVitest(context: VitestCachePathContext) { const { vitest } = context; const { projects } = vitest; if (observedProjectLists.has(projects)) { return; } observedProjectLists.add(projects); const ignoredPaths = new Set(); const ignoreCachePath = (path: string) => { if (!path || ignoredPaths.has(path)) { return; } ignoredPaths.add(path); ignoreInput(path); ignoreOutput(path); }; // Results and dependency-record caches live under the resolved Vite // cacheDir, which Vitest points at its own subdirectory. ignoreCachePath(vitest.viteConfig.cacheDir); // Mirror Vitest's fs module cache layout: the shared root cache plus // any per-project override. const rootCache = vitest.config.fsModuleCachePath || join(searchForWorkspaceRoot(vitest.viteConfig.root), 'node_modules', '.vitest-cache'); ignoreCachePath(rootCache); for (const project of projects) { ignoreCachePath(project.config.fsModuleCachePath || rootCache); } // Vitest's injectTestProjects resolves and attaches projects before // pushing them here, even when an async `order: 'pre'` hook started // injection before this hook. A restart replaces the array, so each // new list gets its own observer and resolved root cache. const push = projects.push; projects.push = function (...addedProjects) { const length = push.apply(this, addedProjects); for (const project of addedProjects) { ignoreCachePath(project.config.fsModuleCachePath || rootCache); } return length; }; }, } as PluginOption; } /** * Inject the vitest resolver plugin, the auto-inline matcher plugin, the * coverage version guard, and the cache-paths plugin into a single inline * project config. Used both for * root configs and for object-shaped entries inside `test.projects`. * * The shapes overlap (both have an optional top-level `plugins` array and * an optional `test.server.deps.inline`), so a shared helper keeps the * wiring consistent. */ const VITE_PLUS_TEST_PLUGIN_NAMES: ReadonlySet = new Set([ 'vite-plus:vitest-resolver', 'vite-plus:auto-inline-matcher-deps', 'vite-plus:coverage-version-guard', 'vite-plus:vitest-cache-paths', ]); function withoutVitePlusTestPlugins(plugin: PluginOption): PluginOption { if (Array.isArray(plugin)) { return plugin.filter(keepUserPlugin).map(withoutVitePlusTestPlugins); } // Injected plugins are never async, and re-wrapping with `.then` drops own props that tools like Vike look up. if (types.isPromise(plugin)) { return plugin; } return plugin && VITE_PLUS_TEST_PLUGIN_NAMES.has(plugin.name) ? false : plugin; } function keepUserPlugin(plugin: PluginOption): boolean { return !plugin || !('name' in plugin) || !VITE_PLUS_TEST_PLUGIN_NAMES.has(plugin.name); } function injectPluginIntoInlineConfig< T extends { plugins?: UserConfig['plugins']; test?: { server?: { deps?: { inline?: unknown } } }; }, >(config: T): T { return { ...config, plugins: [ vitePlusVitestResolverPlugin(), vitePlusAutoInlineMatcherPlugin(), vitePlusCoverageVersionGuardPlugin(), vitePlusVitestCachePathsPlugin(), ...(config.plugins ?? []).filter(keepUserPlugin).map(withoutVitePlusTestPlugins), ], }; } /** * Inline projects inherit root plugins by default in Vitest v5. Only projects * with inheritance disabled or an external base need their own injection. * * Entry shapes (from `TestProjectConfiguration`): * - string (glob path like `'./packages/*'`) → passed through unchanged. * - object (inline config with `test: {...}`) → clone and prepend plugin. * - function (sync or async) → wrap so its result is injected. * - Promise (resolves to inline config) → chain `.then(injectPlugin)`. * * String/glob entries cannot be cloned, so they carry no injected plugin. This * only weakens the COVERAGE guard, and only narrowly: coverage is global, and * the migration rewrites every nested config file to vite-plus * `defineConfig`/`defineProject` (which re-inject the guard), so a migrated * workspace still fires it from its resolved projects. The residual gap is a * hand-authored workspace whose string globs resolve to raw `vitest/config` * sub-configs or bare directory projects — there a provider/runner skew falls * back to Vitest's own (softer) warning instead of the guard's hard error. */ function injectPluginIntoProject(project: TestProjectConfiguration): TestProjectConfiguration { if (typeof project === 'string') { return project; } if (typeof project === 'function') { const wrapped: UserProjectConfigFn = (env: ConfigEnv) => { const result = project(env); if (types.isPromise(result)) { return result.then(injectIndependentProject); } return injectIndependentProject(result); }; return wrapped; } if (types.isPromise(project)) { return project.then(injectIndependentProject); } if (typeof project === 'object' && project !== null) { return injectIndependentProject(project); } return project; } function injectIndependentProject( config: UserWorkspaceConfig & { extends?: string | boolean }, ): UserWorkspaceConfig { return config.extends === false || typeof config.extends === 'string' ? injectPlugin(config) : config; } function injectPlugin(config: T): T { const injected = injectPluginIntoInlineConfig(config); const projects = injected.test?.projects; if (!projects || projects.length === 0) { return injected; } return { ...injected, test: { ...injected.test, projects: projects.map(injectPluginIntoProject), }, }; } function injectPluginIntoConfig(config: ViteUserConfigExport): ViteUserConfigExport { if (typeof config === 'function') { return (env: ConfigEnv) => { const result = config(env); if (types.isPromise(result)) { return result.then(injectPlugin); } return injectPlugin(result); }; } if (types.isPromise(config)) { return config.then(injectPlugin); } return injectPlugin(config); } /** * Keep a single generic signature so callback returns receive one contextual * config type. Separate sync and async overloads make TypeScript widen nested * literals in async callbacks before overload resolution (for example, an * Oxlint rule severity of `'warn'` becomes `string`). */ export function defineConfig(config: T): DefinedViteUserConfig { // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Injection preserves the caller’s config overload while Vite returns the broader union. return viteDefineConfig(injectPluginIntoConfig(config)) as DefinedViteUserConfig; } /** * Inject the vite-plus plugins into a `defineProject` export. A project config * (`UserWorkspaceConfig`) can itself declare nested projects in v5. Referenced * configs own a Vite server, so inject at their root and apply inheritance rules * to their inline children. */ function injectPluginIntoProjectExport(config: UserProjectConfigExport): UserProjectConfigExport { if (typeof config === 'function') { return (env: ConfigEnv) => { const result = config(env); return types.isPromise(result) ? result.then(injectPlugin) : injectPlugin(result); }; } if (types.isPromise(config)) { return config.then(injectPlugin); } return injectPlugin(config); } /** * `defineProject` counterpart of [[defineConfig]]. A migrated project config * that uses `defineProject({ test: { browser: ... } })` — e.g. a file named by * a string `test.projects` entry — must still receive the vite-plus resolver / * auto-inline plugins, or a browser project can fail to resolve `vitest` / * `@vitest/*` from its own root under pnpm strict / Yarn PnP. The raw * `vitest/config` helper does not add them. */ export function defineProject(config: UserWorkspaceConfig): UserWorkspaceConfig; export function defineProject(config: Promise): Promise; export function defineProject(config: UserProjectConfigFn): UserProjectConfigFn; export function defineProject(config: UserProjectConfigExport): UserProjectConfigExport; export function defineProject(config: UserProjectConfigExport): UserProjectConfigExport { // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Vitest has separate overloads for the members of this validated config union. return viteDefineProject(injectPluginIntoProjectExport(config) as never); } // Number of in-flight `withConfigMetadataResolution` calls, and the marker // value captured before the first one set it. The marker stays set until the // LAST overlapping resolution finishes (e.g. several package configs resolved // concurrently in a monorepo), so an async config that reaches `lazyPlugins` // while any metadata resolution is still pending is not mistaken for a build. let configMetadataDepth = 0; let configMetadataSavedValue: string | undefined; /** * Run a config-metadata resolution (a `resolveConfig` call that loads the * user's config purely to read a non-plugin block) with the metadata marker * set, so any `lazyPlugins` evaluated during it skips the plugin factory. * * The marker is scoped in time, not by command name: it is set only while at * least one resolution is in flight (ref-counted so overlapping/nested * resolutions compose) and the prior value is restored afterwards. By the time * the task runner spawns a child (a verbatim `vp run` build, a `vp exec` * child), the marker is already gone, so those builds correctly load plugins. * Keying on the resolution itself — rather than guessing from the command * name — also means command aliases (`vp format`) and not-yet-known commands * all load plugins. */ export async function withConfigMetadataResolution(fn: () => Promise): Promise { if (configMetadataDepth === 0) { configMetadataSavedValue = process.env[CONFIG_METADATA_ENV]; process.env[CONFIG_METADATA_ENV] = '1'; } configMetadataDepth++; try { return await fn(); } finally { configMetadataDepth--; if (configMetadataDepth === 0) { if (configMetadataSavedValue === undefined) { delete process.env[CONFIG_METADATA_ENV]; } else { process.env[CONFIG_METADATA_ENV] = configMetadataSavedValue; } configMetadataSavedValue = undefined; } } } // The bundled core's internal parameters are not part of its public type // contract. Keep this bridge private to the helper. type ResolveConfigWithInternalOptions = ( inlineConfig: ViteInlineConfig, command: 'build' | 'serve', defaultMode?: string, defaultNodeEnv?: string, isPreview?: boolean, patchConfig?: (config: ResolvedConfig) => void, patchPlugins?: (plugins: Array) => void, internalOptions?: { rootBase?: string }, ) => Promise; const resolveConfigWithInternalOptions = resolveConfig as ResolveConfigWithInternalOptions; export interface ResolveConfigFromFileOptions { /** * Base directory for an unset or relative `root` in the config file. * Defaults to the config file's parent directory. * Relative paths resolve from `process.cwd()`. Does not change the process cwd. */ rootBase?: string; } /** * Resolve a Vite config file to read a non-plugin block (`lint`, `fmt`, `pack`, …) from it, * for tools such as oxlint, oxfmt, and Vite+ itself. * * With an absolute `configFile`, default resolution of the initial `root` from the * config file is independent of `process.cwd()`: an unset or relative `root` * resolves from the config file's parent directory. * Callers can supply `rootBase` to use a different project directory, * such as when the config is in a subdirectory. Absolute roots are preserved. * Plugin hooks can later change `root` using Vite's normal semantics. For example, * a relative `root` returned by a `config` hook resolves from `process.cwd()`. * * Relative `configFile` and explicit `rootBase` paths still resolve from `process.cwd()`. * The helper does not change the process cwd; config code and plugins can still read it. * The resolver loads the config once, as a config-metadata resolution, * so `lazyPlugins` skips its factory. * Plugins outside `lazyPlugins` still run their hooks and side effects, * e.g. TanStack Start's router generator scans `src/routes`. */ export async function resolveConfigFromFile( configFile: string, options: ResolveConfigFromFileOptions = {}, ): Promise { if (configFile.length === 0) { throw new Error('Config file path must not be empty.'); } const rootBase = resolve(options.rootBase ?? dirname(resolve(configFile))); return withConfigMetadataResolution(() => resolveConfigWithInternalOptions( { configFile }, 'build', undefined, undefined, undefined, undefined, undefined, { rootBase }, ), ); } export function lazyPlugins(cb: () => Array): Array | undefined; export function lazyPlugins( cb: () => Promise>, ): Array | undefined; export function lazyPlugins( cb: () => Array | Promise>, ): Array | undefined { if (process.env[CONFIG_METADATA_ENV] === '1') { return undefined; } const result = cb(); if (types.isPromise(result)) { return [result]; } return result; }