diff --git a/packages/bundler-vite/src/resolveViteConfig.ts b/packages/bundler-vite/src/resolveViteConfig.ts index 63f979519a..9058a57b8c 100644 --- a/packages/bundler-vite/src/resolveViteConfig.ts +++ b/packages/bundler-vite/src/resolveViteConfig.ts @@ -22,8 +22,9 @@ export const resolveViteConfig = ({ options: ViteBundlerOptions isBuild: boolean isServer: boolean -}): InlineConfig => - mergeConfig( +}): InlineConfig => { + // generate base vite config + let viteConfig = mergeConfig( { clearScreen: false, configFile: false, @@ -43,3 +44,18 @@ export const resolveViteConfig = ({ // some vite options would not take effect inside a plugin, so we still need to merge them here in addition to userConfigPlugin options.viteOptions ?? {}, ) + + // allow modifying vite config via `configureVite` + const configureViteResult = options.configureVite?.( + viteConfig, + isServer, + isBuild, + ) + + // if `configureVite` returns a configuration object, use vite's mergeConfig to merge it + if (configureViteResult) { + viteConfig = mergeConfig(viteConfig, configureViteResult) + } + + return viteConfig +} diff --git a/packages/bundler-vite/src/types.ts b/packages/bundler-vite/src/types.ts index dde557c177..1afe2d7857 100644 --- a/packages/bundler-vite/src/types.ts +++ b/packages/bundler-vite/src/types.ts @@ -6,6 +6,25 @@ import type { InlineConfig } from 'vite' * Options for bundler-vite */ export interface ViteBundlerOptions extends BundlerOptions { + /** + * Vite options + */ viteOptions?: InlineConfig + /** + * Options for @vitejs/plugin-vue + */ vuePluginOptions?: VuePluginOptions + /** + * Modify Vite config + * + * @param config - Vite config + * @param isServer - Whether it is server bundle + * @param isBuild - Whether in build mode + * @returns Vite config to be merged with the default config + */ + configureVite?: ( + config: InlineConfig, + isServer: boolean, + isBuild: boolean, + ) => InlineConfig | void }