Skip to content

Commit b182686

Browse files
committed
refactor: remove fallback loader
1 parent 0540d70 commit b182686

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

packages/astro/src/content/vite-plugin-content-imports.ts

+36-35
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as devalue from 'devalue';
22
import type fsMod from 'node:fs';
3+
import { extname } from 'node:path';
34
import { pathToFileURL } from 'url';
45
import type { Plugin } from 'vite';
5-
import { AstroSettings } from '../@types/astro.js';
6+
import { AstroSettings, ContentEntryType } from '../@types/astro.js';
67
import { AstroErrorData } from '../core/errors/errors-data.js';
78
import { AstroError } from '../core/errors/errors.js';
89
import { escapeViteEnvReferences, getFileInfo } from '../vite-plugin-utils/index.js';
@@ -34,6 +35,13 @@ export function astroContentImportPlugin({
3435
const contentPaths = getContentPaths(settings.config, fs);
3536
const contentEntryExts = getContentEntryExts(settings);
3637

38+
const contentEntryExtToParser: Map<string, ContentEntryType> = new Map();
39+
for (const entryType of settings.contentEntryTypes) {
40+
for (const ext of entryType.extensions) {
41+
contentEntryExtToParser.set(ext, entryType);
42+
}
43+
}
44+
3745
return {
3846
name: 'astro:content-imports',
3947
async load(id) {
@@ -71,55 +79,48 @@ export function astroContentImportPlugin({
7179
});
7280
}
7381
const rawContents = await fs.promises.readFile(fileId, 'utf-8');
74-
const contentEntryType = settings.contentEntryTypes.find((entryType) =>
75-
entryType.extensions.some((ext) => fileId.endsWith(ext))
76-
);
77-
let body: string,
78-
unvalidatedData: Record<string, unknown>,
79-
unvalidatedSlug: string,
80-
rawData: string;
81-
if (contentEntryType) {
82-
const info = await contentEntryType.getEntryInfo({
83-
fileUrl: pathToFileURL(fileId),
84-
contents: rawContents,
82+
const fileExt = extname(fileId);
83+
if (!contentEntryExtToParser.has(fileExt)) {
84+
throw new AstroError({
85+
...AstroErrorData.UnknownContentCollectionError,
86+
message: `No parser found for content entry ${JSON.stringify(
87+
fileId
88+
)}. Did you apply an integration for this file type?`,
8589
});
86-
body = info.body;
87-
unvalidatedData = info.data;
88-
unvalidatedSlug = info.slug;
89-
rawData = info.rawData;
90-
} else {
91-
const parsed = parseFrontmatter(rawContents, fileId);
92-
body = parsed.content;
93-
unvalidatedData = parsed.data;
94-
unvalidatedSlug = parsed.data.slug;
95-
rawData = parsed.matter;
9690
}
97-
98-
const entryInfo = getEntryInfo({
91+
const contentEntryParser = contentEntryExtToParser.get(fileExt)!;
92+
const info = await contentEntryParser.getEntryInfo({
93+
fileUrl: pathToFileURL(fileId),
94+
contents: rawContents,
95+
});
96+
const generatedInfo = getEntryInfo({
9997
entry: pathToFileURL(fileId),
10098
contentDir: contentPaths.contentDir,
10199
});
102-
if (entryInfo instanceof Error) return;
100+
if (generatedInfo instanceof Error) return;
103101

104-
const _internal = { filePath: fileId, rawData };
102+
const _internal = { filePath: fileId, rawData: info.rawData };
105103
// TODO: move slug calculation to the start of the build
106104
// to generate a performant lookup map for `getEntryBySlug`
107-
const slug = getEntrySlug({ ...entryInfo, unvalidatedSlug });
105+
const slug = getEntrySlug({ ...generatedInfo, unvalidatedSlug: info.slug });
108106

109-
const collectionConfig = contentConfig?.collections[entryInfo.collection];
107+
const collectionConfig = contentConfig?.collections[generatedInfo.collection];
110108
const data = collectionConfig
111-
? await getEntryData({ ...entryInfo, _internal, unvalidatedData }, collectionConfig)
112-
: unvalidatedData;
109+
? await getEntryData(
110+
{ ...generatedInfo, _internal, unvalidatedData: info.data },
111+
collectionConfig
112+
)
113+
: info.data;
113114

114115
const code = escapeViteEnvReferences(`
115-
export const id = ${JSON.stringify(entryInfo.id)};
116-
export const collection = ${JSON.stringify(entryInfo.collection)};
116+
export const id = ${JSON.stringify(generatedInfo.id)};
117+
export const collection = ${JSON.stringify(generatedInfo.collection)};
117118
export const slug = ${JSON.stringify(slug)};
118-
export const body = ${JSON.stringify(body)};
119+
export const body = ${JSON.stringify(info.body)};
119120
export const data = ${devalue.uneval(data) /* TODO: reuse astro props serializer */};
120121
export const _internal = {
121-
filePath: ${JSON.stringify(fileId)},
122-
rawData: ${JSON.stringify(unvalidatedData)},
122+
filePath: ${JSON.stringify(_internal.filePath)},
123+
rawData: ${JSON.stringify(_internal.rawData)},
123124
};
124125
`);
125126
return { code };

0 commit comments

Comments
 (0)