Skip to content

Commit

Permalink
Lazily initialize the md and mdx processor (#12026)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Sep 19, 2024
1 parent dd3b753 commit 40e7a1b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-pugs-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Initializes the Markdown processor only when there's `.md` files
5 changes: 5 additions & 0 deletions .changeset/moody-cougars-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/mdx': patch
---

Initializes the MDX processor only when there's `.mdx` files
14 changes: 4 additions & 10 deletions packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand All @@ -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,
Expand Down
16 changes: 4 additions & 12 deletions packages/integrations/mdx/src/vite-plugin-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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');
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 40e7a1b

Please sign in to comment.