Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 };
}

Expand Down Expand Up @@ -340,16 +347,15 @@ export async function createContentTypesGenerator({
logger,
settings,
});
const environment = getRunnableEnvironment(viteServer);
invalidateVirtualMod(environment);
invalidateVirtualMod(viteServer.environments.ssr);
}
}
return { init, queueEvent };
}

// 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;

Expand Down
19 changes: 15 additions & 4 deletions packages/astro/src/content/vite-plugin-content-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions packages/astro/src/vite-plugin-head/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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'],
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/astro-assets-prefix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ describe('Assets Prefix - Static', () => {
});
await fixture.build();
});

after(async () => {
await fixture.clean();
})
});

after(async () => {
await fixture.clean();
Expand Down
Loading