diff --git a/packages/astro/src/content/types-generator.ts b/packages/astro/src/content/types-generator.ts index 4de39ba48e4f..7d2ccc17f09b 100644 --- a/packages/astro/src/content/types-generator.ts +++ b/packages/astro/src/content/types-generator.ts @@ -3,13 +3,17 @@ import * as path from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { bold, cyan } from 'kleur/colors'; import { glob } from 'tinyglobby'; -import { normalizePath, type RunnableDevEnvironment, type ViteDevServer } from 'vite'; +import { + type DevEnvironment, + normalizePath, + type RunnableDevEnvironment, + type ViteDevServer, +} from 'vite'; import { type ZodSchema, z } from 'zod'; import { zodToJsonSchema } from 'zod-to-json-schema'; import { AstroError } from '../core/errors/errors.js'; import { AstroErrorData, AstroUserError } from '../core/errors/index.js'; import type { Logger } from '../core/logger/core.js'; -import { getRunnableEnvironment } from '../core/module-loader/index.js'; import { isRelativePath } from '../core/path.js'; import type { AstroSettings } from '../types/astro.js'; import type { ContentEntryType } from '../types/public/content.js'; @@ -152,8 +156,11 @@ export async function createContentTypesGenerator({ return { shouldGenerateTypes: false }; } if (fileType === 'config') { - const environment = getRunnableEnvironment(viteServer); - await reloadContentConfigObserver({ fs, settings, environment }); + await reloadContentConfigObserver({ + fs, + settings, + environment: viteServer.environments.content as RunnableDevEnvironment, + }); return { shouldGenerateTypes: true }; } @@ -340,8 +347,7 @@ export async function createContentTypesGenerator({ logger, settings, }); - const environment = getRunnableEnvironment(viteServer); - invalidateVirtualMod(environment); + invalidateVirtualMod(viteServer.environments.ssr); } } return { init, queueEvent }; @@ -349,7 +355,7 @@ export async function createContentTypesGenerator({ // The virtual module contains a lookup map from slugs to content imports. // Invalidate whenever content types change. -function invalidateVirtualMod(environment: RunnableDevEnvironment) { +function invalidateVirtualMod(environment: DevEnvironment) { const virtualMod = environment.moduleGraph.getModuleById('\0' + VIRTUAL_MODULE_ID); if (!virtualMod) return; diff --git a/packages/astro/src/content/vite-plugin-content-imports.ts b/packages/astro/src/content/vite-plugin-content-imports.ts index 8ed2188c2626..e734282de6b3 100644 --- a/packages/astro/src/content/vite-plugin-content-imports.ts +++ b/packages/astro/src/content/vite-plugin-content-imports.ts @@ -3,12 +3,11 @@ import { extname } from 'node:path'; import { pathToFileURL } from 'node:url'; import * as devalue from 'devalue'; import type { PluginContext } from 'rollup'; -import type { Plugin } from 'vite'; +import type { Plugin, RunnableDevEnvironment } from 'vite'; import { getProxyCode } from '../assets/utils/proxy.js'; import { AstroError } from '../core/errors/errors.js'; import { AstroErrorData } from '../core/errors/index.js'; import type { Logger } from '../core/logger/core.js'; -import { getRunnableEnvironment } from '../core/module-loader/index.js'; import type { AstroSettings } from '../types/astro.js'; import type { AstroConfig } from '../types/public/config.js'; import type { @@ -86,6 +85,14 @@ export function astroContentImportPlugin({ name: 'astro:content-imports', config(_config, env) { shouldEmitFile = env.command === 'build'; + return { + environments: { + content: { + // This is all that's needed to create a new RunnableDevEnvironment + dev: {}, + }, + }, + }; }, async buildStart() { // Get symlinks once at build start @@ -153,7 +160,7 @@ export const _internal = { configureServer(viteServer) { viteServer.watcher.on('all', async (event, entry) => { if (CHOKIDAR_MODIFIED_EVENTS.includes(event)) { - const environment = getRunnableEnvironment(viteServer); + const environment = viteServer.environments.ssr; const entryType = getEntryType(entry, contentPaths, contentEntryExts, dataEntryExts); if (!COLLECTION_TYPES_TO_INVALIDATE_ON.includes(entryType)) return; @@ -162,7 +169,11 @@ export const _internal = { // Reload the config in case of changes. // Changes to the config file itself are handled in types-generator.ts, so we skip them here if (entryType === 'content' || entryType === 'data') { - await reloadContentConfigObserver({ fs, settings, environment }); + await reloadContentConfigObserver({ + fs, + settings, + environment: viteServer.environments.content as RunnableDevEnvironment, + }); } // Invalidate all content imports and `render()` modules. diff --git a/packages/astro/src/content/vite-plugin-content-virtual-mod.ts b/packages/astro/src/content/vite-plugin-content-virtual-mod.ts index 1bfe3268f640..7de63a01cbbc 100644 --- a/packages/astro/src/content/vite-plugin-content-virtual-mod.ts +++ b/packages/astro/src/content/vite-plugin-content-virtual-mod.ts @@ -6,7 +6,6 @@ import pLimit from 'p-limit'; import { glob } from 'tinyglobby'; import { normalizePath, type Plugin, type ViteDevServer } from 'vite'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; -import { getRunnableEnvironment } from '../core/module-loader/index.js'; import { rootRelativePath } from '../core/viteUtils.js'; import type { AstroSettings } from '../types/astro.js'; import type { AstroPluginMetadata } from '../vite-plugin-astro/index.js'; @@ -48,7 +47,7 @@ interface AstroContentVirtualModPluginParams { } function invalidateDataStore(viteServer: ViteDevServer) { - const environment = getRunnableEnvironment(viteServer); + const environment = viteServer.environments.ssr; const module = environment.moduleGraph.getModuleById(RESOLVED_DATA_STORE_VIRTUAL_ID); if (module) { environment.moduleGraph.invalidateModule(module); diff --git a/packages/astro/src/vite-plugin-head/index.ts b/packages/astro/src/vite-plugin-head/index.ts index f51bc2418323..6d5686dca1fb 100644 --- a/packages/astro/src/vite-plugin-head/index.ts +++ b/packages/astro/src/vite-plugin-head/index.ts @@ -1,10 +1,9 @@ import type { ModuleInfo } from 'rollup'; import type * as vite from 'vite'; -import type { RunnableDevEnvironment } from 'vite'; +import type { DevEnvironment } from 'vite'; import { getParentModuleInfos, getTopLevelPageModuleInfos } from '../core/build/graph.js'; import type { BuildInternals } from '../core/build/internal.js'; import type { AstroBuildPlugin } from '../core/build/plugin.js'; -import { getRunnableEnvironment } from '../core/module-loader/index.js'; import type { SSRComponentMetadata, SSRResult } from '../types/public/internal.js'; import { getAstroMetadata } from '../vite-plugin-astro/index.js'; import type { PluginMetadata } from '../vite-plugin-astro/types.js'; @@ -13,7 +12,7 @@ import type { PluginMetadata } from '../vite-plugin-astro/types.js'; const injectExp = /(?:^\/\/|\/\/!)\s*astro-head-inject/; export default function configHeadVitePlugin(): vite.Plugin { - let environment: RunnableDevEnvironment; + let environment: DevEnvironment; function propagateMetadata< P extends keyof PluginMetadata['astro'], @@ -48,8 +47,8 @@ export default function configHeadVitePlugin(): vite.Plugin { name: 'astro:head-metadata', enforce: 'pre', apply: 'serve', - configureServer(_server) { - environment = getRunnableEnvironment(_server); + configureServer(server) { + environment = server.environments.ssr; }, resolveId(source, importer) { if (importer) { diff --git a/packages/astro/test/astro-assets-prefix.test.js b/packages/astro/test/astro-assets-prefix.test.js index 07f01a13ab12..46d7d3df9d45 100644 --- a/packages/astro/test/astro-assets-prefix.test.js +++ b/packages/astro/test/astro-assets-prefix.test.js @@ -18,10 +18,10 @@ describe('Assets Prefix - Static', () => { }); await fixture.build(); }); - + after(async () => { await fixture.clean(); - }) + }); after(async () => { await fixture.clean();