Skip to content

Commit 245f348

Browse files
committed
fix: return empty array instead of undefined and wrap async in array
vitePlugins() now returns [] when skipping, making it directly assignable to plugins. For async callbacks, wrap in an array since Vite's plugins field expects PluginOption[], not a bare Promise.
1 parent 4a1eebb commit 245f348

4 files changed

Lines changed: 22 additions & 18 deletions

File tree

docs/config/troubleshooting.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ For heavy plugins that should be lazily imported, combine with dynamic `import()
2222
import { defineConfig, vitePlugins } from 'vite-plus';
2323

2424
export default defineConfig({
25-
plugins: vitePlugins(async () => {
26-
const { default: heavyPlugin } = await import('vite-plugin-heavy');
27-
return [heavyPlugin()];
28-
}),
25+
plugins: [
26+
vitePlugins(async () => {
27+
const { default: heavyPlugin } = await import('vite-plugin-heavy');
28+
return [heavyPlugin()];
29+
}),
30+
],
2931
});
3032
```
3133

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { defineConfig, vitePlugins } from 'vite-plus';
22

33
export default defineConfig({
4-
plugins: vitePlugins(async () => {
5-
const { default: myLazyPlugin } = await import('./my-plugin');
6-
return [myLazyPlugin()];
7-
}),
4+
plugins: [
5+
vitePlugins(async () => {
6+
const { default: myLazyPlugin } = await import('./my-plugin');
7+
return [myLazyPlugin()];
8+
}),
9+
],
810
});

packages/cli/src/__tests__/index.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ test('should keep vitest exports stable', () => {
3636
expect(defaultBrowserPort).toBeDefined();
3737
});
3838

39-
test('vitePlugins returns undefined when VP_COMMAND is unset', () => {
39+
test('vitePlugins returns empty array when VP_COMMAND is unset', () => {
4040
delete process.env.VP_COMMAND;
4141
const result = vitePlugins(() => [{ name: 'test' }]);
42-
expect(result).toBeUndefined();
42+
expect(result).toEqual([]);
4343
});
4444

45-
test('vitePlugins returns undefined when VP_COMMAND is empty string', () => {
45+
test('vitePlugins returns empty array when VP_COMMAND is empty string', () => {
4646
process.env.VP_COMMAND = '';
4747
const result = vitePlugins(() => [{ name: 'test' }]);
48-
expect(result).toBeUndefined();
48+
expect(result).toEqual([]);
4949
});
5050

5151
test.each(['dev', 'build', 'test', 'preview'])(
@@ -58,11 +58,11 @@ test.each(['dev', 'build', 'test', 'preview'])(
5858
);
5959

6060
test.each(['lint', 'fmt', 'check', 'pack', 'install', 'run'])(
61-
'vitePlugins returns undefined when VP_COMMAND is %s',
61+
'vitePlugins returns empty array when VP_COMMAND is %s',
6262
(cmd) => {
6363
process.env.VP_COMMAND = cmd;
6464
const result = vitePlugins(() => [{ name: 'my-plugin' }]);
65-
expect(result).toBeUndefined();
65+
expect(result).toEqual([]);
6666
},
6767
);
6868

@@ -76,10 +76,10 @@ test('vitePlugins supports async callback', async () => {
7676
expect(await result).toEqual([{ name: 'async-plugin' }]);
7777
});
7878

79-
test('vitePlugins returns undefined for async callback when skipped', () => {
79+
test('vitePlugins returns empty array for async callback when skipped', () => {
8080
process.env.VP_COMMAND = 'lint';
8181
const result = vitePlugins(async () => {
8282
return [{ name: 'async-plugin' }];
8383
});
84-
expect(result).toBeUndefined();
84+
expect(result).toEqual([]);
8585
});

packages/cli/src/define-config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ export function defineConfig(config: ViteUserConfigExport): ViteUserConfigExport
4747
return viteDefineConfig(config);
4848
}
4949

50-
export function vitePlugins<T>(cb: () => T): T | undefined {
50+
export function vitePlugins<T>(cb: () => T): T | [] {
5151
const cmd = process.env.VP_COMMAND;
5252
if (cmd === 'dev' || cmd === 'build' || cmd === 'test' || cmd === 'preview') {
5353
return cb();
5454
}
55-
return undefined;
55+
return [];
5656
}

0 commit comments

Comments
 (0)