0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/astro/content-module.template.mjs
Ben Holmes 7877a06d82
Fix up pnpm dev (#7424)
* feat: use persistent per turbo recs

* feat: preserve types yay

* fix: *actually* solve problem

* fix: type failures from content types template

* fix: bad JS doc comments

* fix: bad src/content/template path

* chore: changeset

---------

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
2023-06-20 14:06:36 -04:00

83 lines
2.2 KiB
JavaScript

// astro-head-inject
import {
createCollectionToGlobResultMap,
createGetCollection,
createGetDataEntryById,
createGetEntries,
createGetEntry,
createGetEntryBySlug,
createReference,
} from 'astro/content/runtime';
export { z } from 'astro/zod';
const contentDir = '@@CONTENT_DIR@@';
const contentEntryGlob = import.meta.glob('@@CONTENT_ENTRY_GLOB_PATH@@', {
query: { astroContentCollectionEntry: true },
});
const contentCollectionToEntryMap = createCollectionToGlobResultMap({
globResult: contentEntryGlob,
contentDir,
});
const dataEntryGlob = import.meta.glob('@@DATA_ENTRY_GLOB_PATH@@', {
query: { astroDataCollectionEntry: true },
});
const dataCollectionToEntryMap = createCollectionToGlobResultMap({
globResult: dataEntryGlob,
contentDir,
});
const collectionToEntryMap = createCollectionToGlobResultMap({
globResult: { ...contentEntryGlob, ...dataEntryGlob },
contentDir,
});
let lookupMap = {};
/* @@LOOKUP_MAP_ASSIGNMENT@@ */
function createGlobLookup(glob) {
return async (collection, lookupId) => {
const filePath = lookupMap[collection]?.entries[lookupId];
if (!filePath) return undefined;
return glob[collection][filePath];
};
}
const renderEntryGlob = import.meta.glob('@@RENDER_ENTRY_GLOB_PATH@@', {
query: { astroRenderContent: true },
});
const collectionToRenderEntryMap = createCollectionToGlobResultMap({
globResult: renderEntryGlob,
contentDir,
});
export function defineCollection(config) {
if (!config.type) config.type = 'content';
return config;
}
export const getCollection = createGetCollection({
contentCollectionToEntryMap,
dataCollectionToEntryMap,
getRenderEntryImport: createGlobLookup(collectionToRenderEntryMap),
});
export const getEntryBySlug = createGetEntryBySlug({
getEntryImport: createGlobLookup(contentCollectionToEntryMap),
getRenderEntryImport: createGlobLookup(collectionToRenderEntryMap),
});
export const getDataEntryById = createGetDataEntryById({
dataCollectionToEntryMap,
});
export const getEntry = createGetEntry({
getEntryImport: createGlobLookup(collectionToEntryMap),
getRenderEntryImport: createGlobLookup(collectionToRenderEntryMap),
});
export const getEntries = createGetEntries(getEntry);
export const reference = createReference({ lookupMap });