Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing inline Astro config to getViteConfig() #10963

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions .changeset/nervous-waves-shop.md
Original file line number Diff line number Diff line change
@@ -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',
},
);
```
6 changes: 3 additions & 3 deletions packages/astro/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -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(
Expand Down
Loading