diff --git a/.changeset/chilled-timers-shave.md b/.changeset/chilled-timers-shave.md new file mode 100644 index 000000000000..1866eab85bfb --- /dev/null +++ b/.changeset/chilled-timers-shave.md @@ -0,0 +1,26 @@ +--- +'astro': patch +--- + +**BREAKING CHANGE to experimental content layer loaders only!** + +Passes `AstroConfig` instead of `AstroSettings` object to content layer loaders. + +This will not affect you unless you have created a loader that uses the `settings` object. If you have, you will need to update your loader to use the `config` object instead. + +```diff +export default function myLoader() { + return { + name: 'my-loader' +- async load({ settings }) { +- const base = settings.config.base; ++ async load({ config }) { ++ const base = config.base; + // ... + } + } +} + +``` + +Other properties of the settings object are private internals, and should not be accessed directly. If you think you need access to other properties, please open an issue to discuss your use case. diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index d4f5d9b6beff..52a9af058a39 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -2505,6 +2505,7 @@ export interface RouteOptions { /** * Resolved Astro Config + * * Config with user settings along with all defaults filled in. */ export interface AstroConfig extends AstroConfigType { @@ -2593,7 +2594,7 @@ export interface ContentEntryType { }, ): rollup.LoadResult | Promise; contentModuleTypes?: string; - getRenderFunction?(settings: AstroSettings): Promise; + getRenderFunction?(config: AstroConfig): Promise; /** * Handle asset propagation for rendered content to avoid bleed. diff --git a/packages/astro/src/content/content-layer.ts b/packages/astro/src/content/content-layer.ts index 4861f3f61aa1..66dfdd2e75e3 100644 --- a/packages/astro/src/content/content-layer.ts +++ b/packages/astro/src/content/content-layer.ts @@ -3,7 +3,7 @@ import { isAbsolute } from 'node:path'; import { fileURLToPath } from 'node:url'; import type { FSWatcher } from 'vite'; import xxhash from 'xxhash-wasm'; -import type { AstroSettings } from '../@types/astro.js'; +import type { AstroSettings, ContentEntryType } from '../@types/astro.js'; import { AstroUserError } from '../core/errors/errors.js'; import type { Logger } from '../core/logger/core.js'; import { @@ -14,7 +14,12 @@ import { } from './consts.js'; import type { LoaderContext } from './loaders/types.js'; import type { MutableDataStore } from './mutable-data-store.js'; -import { getEntryDataAndImages, globalContentConfigObserver, posixRelative } from './utils.js'; +import { + getEntryConfigByExtMap, + getEntryDataAndImages, + globalContentConfigObserver, + posixRelative, +} from './utils.js'; export interface ContentLayerOptions { store: MutableDataStore; @@ -118,10 +123,14 @@ export class ContentLayer { store: this.#store.scopedStore(collectionName), meta: this.#store.metaStore(collectionName), logger: this.#logger.forkIntegrationLogger(loaderName), - settings: this.#settings, + config: this.#settings.config, parseData, generateDigest: await this.#getGenerateDigest(), watcher: this.#watcher, + entryTypes: getEntryConfigByExtMap([ + ...this.#settings.contentEntryTypes, + ...this.#settings.dataEntryTypes, + ] as Array), }; } diff --git a/packages/astro/src/content/loaders/file.ts b/packages/astro/src/content/loaders/file.ts index 75e5e214d9a8..22d498b12e39 100644 --- a/packages/astro/src/content/loaders/file.ts +++ b/packages/astro/src/content/loaders/file.ts @@ -14,7 +14,7 @@ export function file(fileName: string): Loader { throw new Error('Glob patterns are not supported in `file` loader. Use `glob` loader instead.'); } - async function syncData(filePath: string, { logger, parseData, store, settings }: LoaderContext) { + async function syncData(filePath: string, { logger, parseData, store, config }: LoaderContext) { let json: Array>; try { @@ -26,7 +26,7 @@ export function file(fileName: string): Loader { return; } - const normalizedFilePath = posixRelative(fileURLToPath(settings.config.root), filePath); + const normalizedFilePath = posixRelative(fileURLToPath(config.root), filePath); if (Array.isArray(json)) { if (json.length === 0) { @@ -58,22 +58,22 @@ export function file(fileName: string): Loader { return { name: 'file-loader', - load: async (options) => { - const { settings, logger, watcher } = options; + load: async (context) => { + const { config, logger, watcher } = context; logger.debug(`Loading data from ${fileName}`); - const url = new URL(fileName, settings.config.root); + const url = new URL(fileName, config.root); if (!existsSync(url)) { logger.error(`File not found: ${fileName}`); return; } const filePath = fileURLToPath(url); - await syncData(filePath, options); + await syncData(filePath, context); watcher?.on('change', async (changedPath) => { if (changedPath === filePath) { logger.info(`Reloading data from ${fileName}`); - await syncData(filePath, options); + await syncData(filePath, context); } }); }, diff --git a/packages/astro/src/content/loaders/glob.ts b/packages/astro/src/content/loaders/glob.ts index 4a4ecbcac8d3..260d32c62029 100644 --- a/packages/astro/src/content/loaders/glob.ts +++ b/packages/astro/src/content/loaders/glob.ts @@ -6,7 +6,7 @@ import micromatch from 'micromatch'; import pLimit from 'p-limit'; import type { ContentEntryRenderFuction, ContentEntryType } from '../../@types/astro.js'; import type { RenderedContent } from '../data-store.js'; -import { getContentEntryIdAndSlug, getEntryConfigByExtMap, posixRelative } from '../utils.js'; +import { getContentEntryIdAndSlug, posixRelative } from '../utils.js'; import type { Loader } from './types.js'; export interface GenerateIdOptions { @@ -66,7 +66,7 @@ export function glob(globOptions: GlobOptions): Loader { return { name: 'glob-loader', - load: async ({ settings, logger, watcher, parseData, store, generateDigest }) => { + load: async ({ config, logger, watcher, parseData, store, generateDigest, entryTypes }) => { const renderFunctionByContentType = new WeakMap< ContentEntryType, ContentEntryRenderFuction @@ -121,7 +121,7 @@ export function glob(globOptions: GlobOptions): Loader { const filePath = fileURLToPath(fileUrl); - const relativePath = posixRelative(fileURLToPath(settings.config.root), filePath); + const relativePath = posixRelative(fileURLToPath(config.root), filePath); const parsedData = await parseData({ id, @@ -131,7 +131,7 @@ export function glob(globOptions: GlobOptions): Loader { if (entryType.getRenderFunction) { let render = renderFunctionByContentType.get(entryType); if (!render) { - render = await entryType.getRenderFunction(settings); + render = await entryType.getRenderFunction(config); // Cache the render function for this content type, so it can re-use parsers and other expensive setup renderFunctionByContentType.set(entryType, render); } @@ -177,14 +177,7 @@ export function glob(globOptions: GlobOptions): Loader { fileToIdMap.set(filePath, id); } - const entryConfigByExt = getEntryConfigByExtMap([ - ...settings.contentEntryTypes, - ...settings.dataEntryTypes, - ] as Array); - - const baseDir = globOptions.base - ? new URL(globOptions.base, settings.config.root) - : settings.config.root; + const baseDir = globOptions.base ? new URL(globOptions.base, config.root) : config.root; if (!baseDir.pathname.endsWith('/')) { baseDir.pathname = `${baseDir.pathname}/`; @@ -200,13 +193,13 @@ export function glob(globOptions: GlobOptions): Loader { logger.warn(`No extension found for ${file}`); return; } - return entryConfigByExt.get(`.${ext}`); + return entryTypes.get(`.${ext}`); } const limit = pLimit(10); const skippedFiles: Array = []; - const contentDir = new URL('content/', settings.config.srcDir); + const contentDir = new URL('content/', config.srcDir); function isInContentDir(file: string) { const fileUrl = new URL(file, baseDir); diff --git a/packages/astro/src/content/loaders/types.ts b/packages/astro/src/content/loaders/types.ts index 26be0495a281..67e7e13f0825 100644 --- a/packages/astro/src/content/loaders/types.ts +++ b/packages/astro/src/content/loaders/types.ts @@ -1,6 +1,6 @@ import type { FSWatcher } from 'vite'; import type { ZodSchema } from 'zod'; -import type { AstroIntegrationLogger, AstroSettings } from '../../@types/astro.js'; +import type { AstroConfig, AstroIntegrationLogger, ContentEntryType } from '../../@types/astro.js'; import type { MetaStore, ScopedDataStore } from '../mutable-data-store.js'; export interface ParseDataOptions> { @@ -20,9 +20,8 @@ export interface LoaderContext { /** A simple KV store, designed for things like sync tokens */ meta: MetaStore; logger: AstroIntegrationLogger; - - settings: AstroSettings; - + /** Astro config, with user config and merged defaults */ + config: AstroConfig; /** Validates and parses the data according to the collection schema */ parseData>(props: ParseDataOptions): Promise; @@ -31,6 +30,8 @@ export interface LoaderContext { /** When running in dev, this is a filesystem watcher that can be used to trigger updates */ watcher?: FSWatcher; + + entryTypes: Map; } export interface Loader { diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index f94b87d9775a..7269c5151430 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -78,7 +78,8 @@ const collectionConfigParser = z.union([ store: z.any(), meta: z.any(), logger: z.any(), - settings: z.any(), + config: z.any(), + entryTypes: z.any(), parseData: z.any(), generateDigest: z.function(z.tuple([z.any()], z.string())), watcher: z.any().optional(), diff --git a/packages/astro/src/vite-plugin-markdown/content-entry-type.ts b/packages/astro/src/vite-plugin-markdown/content-entry-type.ts index 9b78d8f35149..309f624fc1a9 100644 --- a/packages/astro/src/vite-plugin-markdown/content-entry-type.ts +++ b/packages/astro/src/vite-plugin-markdown/content-entry-type.ts @@ -17,8 +17,8 @@ export const markdownContentEntryType: ContentEntryType = { // We need to handle propagation for Markdown because they support layouts which will bring in styles. handlePropagation: true, - async getRenderFunction(settings) { - const processor = await createMarkdownProcessor(settings.config.markdown); + async getRenderFunction(config) { + const processor = await createMarkdownProcessor(config.markdown); return async function renderToString(entry) { if (!entry.body) { return {