Skip to content
Open
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
14 changes: 14 additions & 0 deletions packages/cli/src/__tests__/resolve-vite-config-startup.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
47 changes: 47 additions & 0 deletions packages/cli/src/config-metadata.ts
Original file line number Diff line number Diff line change
@@ -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<T>(fn: () => Promise<T>): Promise<T> {
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;
}
}
}
44 changes: 2 additions & 42 deletions packages/cli/src/define-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<T>(fn: () => Promise<T>): Promise<T> {
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.
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/resolve-vite-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down
Loading