Skip to content

Commit

Permalink
Support (async) function in vite plugin (resolves #303)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 18, 2023
1 parent e2aebc3 commit 7b1686b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/plugins/vite/index.ts
@@ -1,7 +1,7 @@
import { timerify } from '../../util/Performance.js';
import { hasDependency, load } from '../../util/plugin.js';
import { findVitestDeps } from '../vitest/index.js';
import type { ViteConfig } from './types.js';
import type { ViteConfig, MODE, COMMAND } from './types.js';
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';

// https://vitejs.dev/config/
Expand All @@ -20,6 +20,17 @@ const findViteDependencies: GenericPluginCallback = async (configFilePath, optio

if (!localConfig) return [];

if (typeof localConfig === 'function') {
const dependencies = new Set<string>();
for (const command of ['dev', 'serve', 'build'] as COMMAND[]) {
for (const mode of ['development', 'production'] as MODE[]) {
const config = await localConfig({ command, mode, ssrBuild: undefined });
findVitestDeps(config, options).forEach(dependency => dependencies.add(dependency));
}
}
return Array.from(dependencies);
}

return findVitestDeps(localConfig, options);
};

Expand Down
13 changes: 12 additions & 1 deletion src/plugins/vite/types.ts
@@ -1,5 +1,16 @@
import type { VitestConfig } from '../vitest/types.js';

export interface ViteConfig extends VitestConfig {
interface Config extends VitestConfig {
plugins: unknown[];
}

export type COMMAND = 'dev' | 'serve' | 'build';
export type MODE = 'development' | 'production';

interface Options {
command: COMMAND;
mode: MODE;
ssrBuild?: boolean | undefined;
}

export type ViteConfig = Config | ((options: Options) => Config) | ((options: Options) => Promise<Config>);

0 comments on commit 7b1686b

Please sign in to comment.