From 61f47a684235a049cbfc4f2cbb5edff3befeced7 Mon Sep 17 00:00:00 2001 From: Chris Swithinbank Date: Wed, 8 May 2024 12:19:54 +0200 Subject: [PATCH] Support passing inline Astro config to getViteConfig() (#10963) * Support passing inline Astro config to `getViteConfig()` * Add changeset --- .changeset/nervous-waves-shop.md | 22 ++++++++++++++++++++++ packages/astro/src/config/index.ts | 6 +++--- 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 .changeset/nervous-waves-shop.md diff --git a/.changeset/nervous-waves-shop.md b/.changeset/nervous-waves-shop.md new file mode 100644 index 0000000000..2abf43d152 --- /dev/null +++ b/.changeset/nervous-waves-shop.md @@ -0,0 +1,22 @@ +--- +"astro": minor +--- + +Adds support for passing an inline Astro configuration object to `getViteConfig()` + +If you are using `getViteConfig()` to configure the Vitest test runner, you can now pass a second argument to control how Astro is configured. This makes it possible to configure unit tests with different Astro options when using [Vitest’s workspaces](https://vitest.dev/guide/workspace.html) feature. + +```js +// vitest.config.ts +import { getViteConfig } from 'astro/config'; + +export default getViteConfig( + /* Vite configuration */ + { test: {} }, + /* Astro configuration */ + { + site: 'https://example.com', + trailingSlash: 'never', + }, +); +``` diff --git a/packages/astro/src/config/index.ts b/packages/astro/src/config/index.ts index 3f4652f204..7950990ec7 100644 --- a/packages/astro/src/config/index.ts +++ b/packages/astro/src/config/index.ts @@ -1,12 +1,12 @@ import type { UserConfig } from 'vite'; -import type { AstroUserConfig } from '../@types/astro.js'; +import type { AstroInlineConfig, AstroUserConfig } from '../@types/astro.js'; import { Logger } from '../core/logger/core.js'; export function defineConfig(config: AstroUserConfig) { return config; } -export function getViteConfig(inlineConfig: UserConfig) { +export function getViteConfig(inlineConfig: UserConfig, inlineAstroConfig: AstroInlineConfig = {}) { // Return an async Vite config getter which exposes a resolved `mode` and `command` return async ({ mode, command }: { mode: string; command: 'serve' | 'build' }) => { // Vite `command` is `serve | build`, but Astro uses `dev | build` @@ -34,7 +34,7 @@ export function getViteConfig(inlineConfig: UserConfig) { dest: nodeLogDestination, level: 'info', }); - const { astroConfig: config } = await resolveConfig({}, cmd); + const { astroConfig: config } = await resolveConfig(inlineAstroConfig, cmd); let settings = await createSettings(config, inlineConfig.root); settings = await runHookConfigSetup({ settings, command: cmd, logger }); const viteConfig = await createVite(