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
42 changes: 28 additions & 14 deletions packages/cli/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,25 @@ test('lazyPlugins executes the callback by default', () => {
expect(result).toEqual([{ name: 'test' }]);
});

test('lazyPlugins returns undefined during a config-metadata resolution', () => {
test('lazyPlugins returns an empty array during a config-metadata resolution', () => {
process.env.VP_RESOLVING_CONFIG_METADATA = '1';
const cb = vi.fn<() => Array<PluginOption>>(() => [{ name: 'my-plugin' }]);
const result = lazyPlugins(cb);
expect(result).toBeUndefined();
expect(result).toEqual([]);
expect(cb).not.toHaveBeenCalled();
});

test('withConfigMetadataResolution skips plugins during the resolution and restores after', async () => {
const cb = vi.fn<() => Array<PluginOption>>(() => [{ name: 'my-plugin' }]);
let during: ReturnType<typeof lazyPlugins>;
// Initialized so the assignment inside the async closure satisfies
// definite-assignment analysis now that the return type excludes `undefined`.
let during: ReturnType<typeof lazyPlugins> = [];
const returned = await withConfigMetadataResolution(async () => {
during = lazyPlugins(cb);
return 'result';
});
expect(returned).toBe('result');
expect(during).toBeUndefined();
expect(during).toEqual([]);
expect(cb).not.toHaveBeenCalled();
// marker cleared after → plugins load again
expect(lazyPlugins(() => [{ name: 'after' }])).toEqual([{ name: 'after' }]);
Expand Down Expand Up @@ -73,7 +75,7 @@ test('withConfigMetadataResolution keeps the marker set across overlapping resol
const second = withConfigMetadataResolution(async () => 'second');
expect(await second).toBe('second');
// first is still in flight → lazyPlugins must still skip
expect(lazyPlugins(() => [{ name: 'plugin' }])).toBeUndefined();
expect(lazyPlugins(() => [{ name: 'plugin' }])).toEqual([]);
releaseFirst();
expect(await first).toBe('first');
// both done → marker cleared, plugins load again
Expand All @@ -86,21 +88,21 @@ test('lazyPlugins supports async callback', async () => {
return [plugin];
});
// Async factory wraps the promise in an array for Vite's asyncFlatten
expect(await Promise.all(result!)).toEqual([[{ name: 'async-plugin' }]]);
expect(await Promise.all(result)).toEqual([[{ name: 'async-plugin' }]]);
});

test('lazyPlugins returns undefined for async callback during metadata resolution', () => {
test('lazyPlugins returns an empty array for async callback during metadata resolution', () => {
process.env.VP_RESOLVING_CONFIG_METADATA = '1';
const result = lazyPlugins(async () => {
return [{ name: 'async-plugin' }];
});
expect(result).toBeUndefined();
expect(result).toEqual([]);
});

test('lazyPlugins wraps sync function returning a Promise into array', async () => {
// A sync function that returns a Promise (not an async function) — same handling as async
const result = lazyPlugins(() => Promise.resolve([{ name: 'sync-promise-plugin' }]));
expect(await Promise.all(result!)).toEqual([[{ name: 'sync-promise-plugin' }]]);
expect(await Promise.all(result)).toEqual([[{ name: 'sync-promise-plugin' }]]);
});

// defineConfig auto-injects four internal plugins before user-supplied
Expand Down Expand Up @@ -128,16 +130,28 @@ const userPlugins = (plugins: unknown): Array<unknown> => {
// lazyPlugins type compatibility tests — these verify at compile time that
// lazyPlugins return types satisfy Vite's plugins?: PluginOption[] field.

// Regression test for gh-3597: under `exactOptionalPropertyTypes`, Vite's
// optional `plugins?: PluginOption[]` property rejects an explicit `undefined`,
// so the public `lazyPlugins` return type must never include `undefined`.
// When it does, this annotation resolves to `never` and fails to compile.
const lazyPluginsReturnExcludesUndefined: [undefined] extends [ReturnType<typeof lazyPlugins>]
? never
: true = true;

test('lazyPlugins return type never includes undefined (exactOptionalPropertyTypes)', () => {
expect(lazyPluginsReturnExcludesUndefined).toBe(true);
});

test('lazyPlugins sync return type satisfies plugins field', () => {
// Must compile: plugins accepts PluginOption[] | undefined
// Must compile: plugins accepts PluginOption[]
const config = defineConfig({
plugins: lazyPlugins(() => [{ name: 'sync-type-test' }]),
});
expect(userPlugins(config.plugins).length).toBe(1);
});

test('lazyPlugins async return type satisfies plugins field', async () => {
// Must compile: async overload returns PluginOption[] | undefined, not Promise
// Must compile: async overload returns PluginOption[], not Promise
const config = defineConfig({
plugins: lazyPlugins(async () => {
return [{ name: 'async-type-test' }];
Expand All @@ -146,13 +160,13 @@ test('lazyPlugins async return type satisfies plugins field', async () => {
expect(await Promise.all(userPlugins(config.plugins))).toEqual([[{ name: 'async-type-test' }]]);
});

test('lazyPlugins undefined return satisfies plugins field', () => {
test('lazyPlugins empty-array return during metadata resolution satisfies plugins field', () => {
process.env.VP_RESOLVING_CONFIG_METADATA = '1';
// Must compile: undefined is accepted by plugins?: PluginOption[]
// Must compile: the empty array is accepted by plugins?: PluginOption[]
const config = defineConfig({
plugins: lazyPlugins(() => [{ name: 'skipped' }]),
});
// lazyPlugins returns undefined, but defineConfig still injects its rewrite plugin.
// lazyPlugins returns an empty array, but defineConfig still injects its rewrite plugin.
expect(userPlugins(config.plugins).length).toBe(0);
});

Expand Down
14 changes: 8 additions & 6 deletions packages/cli/src/define-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,15 +1145,17 @@ export async function resolveConfigFromFile(
);
}

export function lazyPlugins(cb: () => Array<PluginOption>): Array<PluginOption> | undefined;
export function lazyPlugins(
cb: () => Promise<Array<PluginOption>>,
): Array<PluginOption> | undefined;
export function lazyPlugins(cb: () => Array<PluginOption>): Array<PluginOption>;
export function lazyPlugins(cb: () => Promise<Array<PluginOption>>): Array<PluginOption>;
export function lazyPlugins(
cb: () => Array<PluginOption> | Promise<Array<PluginOption>>,
): Array<PluginOption> | undefined {
): Array<PluginOption> {
if (process.env[CONFIG_METADATA_ENV] === '1') {
return undefined;
// Vite's `plugins?: PluginOption[]` is optional without `undefined`, so
// returning an empty array (instead of `undefined`) keeps
// `defineConfig({ plugins: lazyPlugins(...) })` type-checking under
// `exactOptionalPropertyTypes`.
return [];
}
const result = cb();
if (types.isPromise(result)) {
Expand Down
Loading