Skip to content

Commit 96ae37e

Browse files
authored
Fix Vitest with content collections (#7233)
1 parent 9e73665 commit 96ae37e

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

.changeset/fuzzy-papayas-shout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fix `getViteConfig` and Vitest setup with content collections

packages/astro/src/config/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ export function getViteConfig(inlineConfig: UserConfig) {
1414

1515
// Use dynamic import to avoid pulling in deps unless used
1616
const [
17+
fs,
1718
{ mergeConfig },
1819
{ nodeLogDestination },
1920
{ openConfig, createSettings },
2021
{ createVite },
2122
{ runHookConfigSetup, runHookConfigDone },
23+
{ astroContentListenPlugin },
2224
] = await Promise.all([
25+
import('fs'),
2326
import('vite'),
2427
import('../core/logger/node.js'),
2528
import('../core/config/index.js'),
2629
import('../core/create-vite.js'),
2730
import('../integrations/index.js'),
31+
import('./vite-plugin-content-listen.js'),
2832
]);
2933
const logging: LogOptions = {
3034
dest: nodeLogDestination,
@@ -39,6 +43,10 @@ export function getViteConfig(inlineConfig: UserConfig) {
3943
const viteConfig = await createVite(
4044
{
4145
mode,
46+
plugins: [
47+
// Initialize the content listener
48+
astroContentListenPlugin({ settings, logging, fs }),
49+
],
4250
},
4351
{ settings, logging: logging, mode }
4452
);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type fsMod from 'node:fs';
2+
import type { Plugin, ViteDevServer } from 'vite';
3+
import type { AstroSettings } from '../@types/astro';
4+
import { attachContentServerListeners } from '../content/server-listeners.js';
5+
import type { LogOptions } from '../core/logger/core.js';
6+
7+
/**
8+
* Listen for Astro content directory changes and generate types.
9+
*
10+
* This is a separate plugin for `getViteConfig` as the `attachContentServerListeners` API
11+
* needs to be called at different times in `astro dev` and `getViteConfig`. For `astro dev`,
12+
* it needs to be called after the Astro server is started (packages/astro/src/core/dev/dev.ts).
13+
* For `getViteConfig`, it needs to be called after the Vite server is started.
14+
*/
15+
export function astroContentListenPlugin({
16+
settings,
17+
logging,
18+
fs,
19+
}: {
20+
settings: AstroSettings;
21+
logging: LogOptions;
22+
fs: typeof fsMod;
23+
}): Plugin {
24+
let server: ViteDevServer;
25+
26+
return {
27+
name: 'astro:content-listen',
28+
apply: 'serve',
29+
configureServer(_server) {
30+
server = _server;
31+
},
32+
async buildStart() {
33+
await attachContentServerListeners({
34+
fs: fs,
35+
settings,
36+
logging,
37+
viteServer: server,
38+
});
39+
},
40+
};
41+
}

0 commit comments

Comments
 (0)