0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-17 22:44:24 -05:00

Lazily initialize the md and mdx processor (#12026)

This commit is contained in:
Bjorn Lu 2024-09-19 20:40:49 +08:00 committed by GitHub
parent dd3b753aba
commit 40e7a1b05d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 18 additions and 22 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Initializes the Markdown processor only when there's `.md` files

View file

@ -0,0 +1,5 @@
---
'@astrojs/mdx': patch
---
Initializes the MDX processor only when there's `.mdx` files

View file

@ -31,14 +31,11 @@ const astroErrorModulePath = normalizePath(
);
export default function markdown({ settings, logger }: AstroPluginOptions): Plugin {
let processor: MarkdownProcessor | undefined;
let processor: Promise<MarkdownProcessor> | undefined;
return {
enforce: 'pre',
name: 'astro:markdown',
async buildStart() {
processor = await createMarkdownProcessor(settings.config.markdown);
},
buildEnd() {
processor = undefined;
},
@ -61,15 +58,12 @@ export default function markdown({ settings, logger }: AstroPluginOptions): Plug
const fileURL = pathToFileURL(fileId);
// `processor` is initialized in `buildStart`, and removed in `buildEnd`. `load`
// should be called in between those two lifecycles, so this error should never happen
// Lazily initialize the Markdown processor
if (!processor) {
return this.error(
'MDX processor is not initialized. This is an internal error. Please file an issue.',
);
processor = createMarkdownProcessor(settings.config.markdown);
}
const renderResult = await processor
const renderResult = await (await processor)
.render(raw.content, {
// @ts-expect-error passing internal prop
fileURL,

View file

@ -9,6 +9,7 @@ import { parseFrontmatter } from './utils.js';
export function vitePluginMdx(mdxOptions: MdxOptions): Plugin {
let processor: ReturnType<typeof createMdxProcessor> | undefined;
let sourcemapEnabled: boolean;
return {
name: '@mdx-js/rollup',
@ -17,13 +18,7 @@ export function vitePluginMdx(mdxOptions: MdxOptions): Plugin {
processor = undefined;
},
configResolved(resolved) {
// `mdxOptions` should be populated at this point, but `astro sync` doesn't call `astro:config:done` :(
// Workaround this for now by skipping here. `astro sync` shouldn't call the `transform()` hook here anyways.
if (Object.keys(mdxOptions).length === 0) return;
processor = createMdxProcessor(mdxOptions, {
sourcemap: !!resolved.build.sourcemap,
});
sourcemapEnabled = !!resolved.build.sourcemap;
// HACK: Remove the `astro:jsx` plugin if defined as we handle the JSX transformation ourselves
const jsxPluginIndex = resolved.plugins.findIndex((p) => p.name === 'astro:jsx');
@ -51,12 +46,9 @@ export function vitePluginMdx(mdxOptions: MdxOptions): Plugin {
// Ensure `data.astro` is available to all remark plugins
setVfileFrontmatter(vfile, frontmatter);
// `processor` is initialized in `configResolved`, and removed in `buildEnd`. `transform`
// should be called in between those two lifecycle, so this error should never happen
// Lazily initialize the MDX processor
if (!processor) {
return this.error(
'MDX processor is not initialized. This is an internal error. Please file an issue.',
);
processor = createMdxProcessor(mdxOptions, { sourcemap: sourcemapEnabled });
}
try {