summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/fuzzy-papayas-shout.md5
-rw-r--r--packages/astro/src/config/index.ts8
-rw-r--r--packages/astro/src/config/vite-plugin-content-listen.ts41
3 files changed, 54 insertions, 0 deletions
diff --git a/.changeset/fuzzy-papayas-shout.md b/.changeset/fuzzy-papayas-shout.md
new file mode 100644
index 000000000..b10a3cddb
--- /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 6601a7a5a..bdd3c492f 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 000000000..ecd9ea68b
--- /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,
+ });
+ },
+ };
+}