Fix Vitest with content collections (#7233)
This commit is contained in:
parent
9e7366567e
commit
96ae37eb09
3 changed files with 54 additions and 0 deletions
5
.changeset/fuzzy-papayas-shout.md
Normal file
5
.changeset/fuzzy-papayas-shout.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix `getViteConfig` and Vitest setup with content collections
|
|
@ -14,17 +14,21 @@ export function getViteConfig(inlineConfig: UserConfig) {
|
||||||
|
|
||||||
// Use dynamic import to avoid pulling in deps unless used
|
// Use dynamic import to avoid pulling in deps unless used
|
||||||
const [
|
const [
|
||||||
|
fs,
|
||||||
{ mergeConfig },
|
{ mergeConfig },
|
||||||
{ nodeLogDestination },
|
{ nodeLogDestination },
|
||||||
{ openConfig, createSettings },
|
{ openConfig, createSettings },
|
||||||
{ createVite },
|
{ createVite },
|
||||||
{ runHookConfigSetup, runHookConfigDone },
|
{ runHookConfigSetup, runHookConfigDone },
|
||||||
|
{ astroContentListenPlugin },
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
|
import('fs'),
|
||||||
import('vite'),
|
import('vite'),
|
||||||
import('../core/logger/node.js'),
|
import('../core/logger/node.js'),
|
||||||
import('../core/config/index.js'),
|
import('../core/config/index.js'),
|
||||||
import('../core/create-vite.js'),
|
import('../core/create-vite.js'),
|
||||||
import('../integrations/index.js'),
|
import('../integrations/index.js'),
|
||||||
|
import('./vite-plugin-content-listen.js'),
|
||||||
]);
|
]);
|
||||||
const logging: LogOptions = {
|
const logging: LogOptions = {
|
||||||
dest: nodeLogDestination,
|
dest: nodeLogDestination,
|
||||||
|
@ -39,6 +43,10 @@ export function getViteConfig(inlineConfig: UserConfig) {
|
||||||
const viteConfig = await createVite(
|
const viteConfig = await createVite(
|
||||||
{
|
{
|
||||||
mode,
|
mode,
|
||||||
|
plugins: [
|
||||||
|
// Initialize the content listener
|
||||||
|
astroContentListenPlugin({ settings, logging, fs }),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{ settings, logging: logging, mode }
|
{ settings, logging: logging, mode }
|
||||||
);
|
);
|
||||||
|
|
41
packages/astro/src/config/vite-plugin-content-listen.ts
Normal file
41
packages/astro/src/config/vite-plugin-content-listen.ts
Normal file
|
@ -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,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in a new issue