Skip to content

Commit

Permalink
Fix Vitest with content collections (#7233)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jun 2, 2023
1 parent 9e73665 commit 96ae37e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-papayas-shout.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix `getViteConfig` and Vitest setup with content collections
8 changes: 8 additions & 0 deletions packages/astro/src/config/index.ts
Expand Up @@ -14,17 +14,21 @@ export function getViteConfig(inlineConfig: UserConfig) {

// Use dynamic import to avoid pulling in deps unless used
const [
fs,
{ mergeConfig },
{ nodeLogDestination },
{ openConfig, createSettings },
{ createVite },
{ runHookConfigSetup, runHookConfigDone },
{ astroContentListenPlugin },
] = await Promise.all([
import('fs'),
import('vite'),
import('../core/logger/node.js'),
import('../core/config/index.js'),
import('../core/create-vite.js'),
import('../integrations/index.js'),
import('./vite-plugin-content-listen.js'),
]);
const logging: LogOptions = {
dest: nodeLogDestination,
Expand All @@ -39,6 +43,10 @@ export function getViteConfig(inlineConfig: UserConfig) {
const viteConfig = await createVite(
{
mode,
plugins: [
// Initialize the content listener
astroContentListenPlugin({ settings, logging, fs }),
],
},
{ settings, logging: logging, mode }
);
Expand Down
41 changes: 41 additions & 0 deletions packages/astro/src/config/vite-plugin-content-listen.ts
@@ -0,0 +1,41 @@
import type fsMod from 'node:fs';
import type { Plugin, ViteDevServer } from 'vite';
import type { AstroSettings } from '../@types/astro';
import { attachContentServerListeners } from '../content/server-listeners.js';
import type { LogOptions } from '../core/logger/core.js';

/**
* Listen for Astro content directory changes and generate types.
*
* This is a separate plugin for `getViteConfig` as the `attachContentServerListeners` API
* needs to be called at different times in `astro dev` and `getViteConfig`. For `astro dev`,
* it needs to be called after the Astro server is started (packages/astro/src/core/dev/dev.ts).
* For `getViteConfig`, it needs to be called after the Vite server is started.
*/
export function astroContentListenPlugin({
settings,
logging,
fs,
}: {
settings: AstroSettings;
logging: LogOptions;
fs: typeof fsMod;
}): Plugin {
let server: ViteDevServer;

return {
name: 'astro:content-listen',
apply: 'serve',
configureServer(_server) {
server = _server;
},
async buildStart() {
await attachContentServerListeners({
fs: fs,
settings,
logging,
viteServer: server,
});
},
};
}

0 comments on commit 96ae37e

Please sign in to comment.