From 96ae37eb09f7406f40fba93e14b2a26ccd46640c Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 2 Jun 2023 21:05:57 +0800 Subject: [PATCH] Fix Vitest with content collections (#7233) --- .changeset/fuzzy-papayas-shout.md | 5 +++ packages/astro/src/config/index.ts | 8 ++++ .../src/config/vite-plugin-content-listen.ts | 41 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 .changeset/fuzzy-papayas-shout.md create mode 100644 packages/astro/src/config/vite-plugin-content-listen.ts diff --git a/.changeset/fuzzy-papayas-shout.md b/.changeset/fuzzy-papayas-shout.md new file mode 100644 index 000000000000..b10a3cddb6d0 --- /dev/null +++ b/.changeset/fuzzy-papayas-shout.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix `getViteConfig` and Vitest setup with content collections diff --git a/packages/astro/src/config/index.ts b/packages/astro/src/config/index.ts index 6601a7a5acc8..bdd3c492f88d 100644 --- a/packages/astro/src/config/index.ts +++ b/packages/astro/src/config/index.ts @@ -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, @@ -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 } ); diff --git a/packages/astro/src/config/vite-plugin-content-listen.ts b/packages/astro/src/config/vite-plugin-content-listen.ts new file mode 100644 index 000000000000..ecd9ea68b768 --- /dev/null +++ b/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, + }); + }, + }; +}