diff --git a/packages/cli/src/__tests__/resolve-vite-config-startup.spec.ts b/packages/cli/src/__tests__/resolve-vite-config-startup.spec.ts new file mode 100644 index 0000000000..2ec200da85 --- /dev/null +++ b/packages/cli/src/__tests__/resolve-vite-config-startup.spec.ts @@ -0,0 +1,14 @@ +import { expect, test, vi } from 'vitest'; + +// `vp create` imports resolve-vite-config.ts at startup but often only looks up +// config files. Vite must load only when `resolveViteConfig` runs, through its +// dynamic import, so those paths do not pay for loading Vite. +vi.mock('vite', () => { + throw new Error('resolve-vite-config.ts loaded vite on import'); +}); + +test('importing resolve-vite-config does not load vite', async () => { + const { findViteConfigUp, hasViteConfig } = await import('../resolve-vite-config.ts'); + expect(findViteConfigUp).toBeTypeOf('function'); + expect(hasViteConfig).toBeTypeOf('function'); +}); diff --git a/packages/cli/src/config-metadata.ts b/packages/cli/src/config-metadata.ts new file mode 100644 index 0000000000..e3cd37a95b --- /dev/null +++ b/packages/cli/src/config-metadata.ts @@ -0,0 +1,47 @@ +// Kept apart from define-config.ts, which imports Vite at the top level, so a +// command that imports resolve-vite-config.ts but only looks up config files +// (for example `vp create`) does not load Vite at startup. +import { CONFIG_METADATA_ENV } from './utils/constants.ts'; + +// 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; + } + } +} diff --git a/packages/cli/src/define-config.ts b/packages/cli/src/define-config.ts index 504453d6da..01cce3e88e 100644 --- a/packages/cli/src/define-config.ts +++ b/packages/cli/src/define-config.ts @@ -29,6 +29,7 @@ import { } from 'vitest/config'; import type { InlineConfig as VitestInlineConfig } from 'vitest/node'; +import { withConfigMetadataResolution } from './config-metadata.ts'; import type { CreateTemplateEntry } from './create/org-manifest.ts'; import type { PackUserConfig } from './pack.ts'; import type { RunConfig } from './run-config.ts'; @@ -1037,48 +1038,7 @@ export function defineProject(config: UserProjectConfigExport): UserProjectConfi 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; - } - } -} +export { withConfigMetadataResolution }; // The bundled core's internal parameters are not part of its public type // contract. Keep this bridge private to the helper. diff --git a/packages/cli/src/resolve-vite-config.ts b/packages/cli/src/resolve-vite-config.ts index c10186907f..3ff34e9d62 100644 --- a/packages/cli/src/resolve-vite-config.ts +++ b/packages/cli/src/resolve-vite-config.ts @@ -2,7 +2,7 @@ import fs from 'node:fs'; import path from 'node:path'; import { findMonorepoRoot } from '../binding/index.js'; -import { withConfigMetadataResolution } from './define-config.ts'; +import { withConfigMetadataResolution } from './config-metadata.ts'; import { VITE_CONFIG_FILES } from './utils/constants.ts'; /**