Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove MDX processor on buildEnd #10770

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/orange-ladybugs-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/mdx": patch
---

Removes internal MDX processor on `buildEnd` to free up memory
23 changes: 23 additions & 0 deletions benchmark/make-project/memory-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ ${loremIpsum}
);
}

for (let i = 0; i < 100; i++) {
const content = `\
# Post ${i}

${loremIpsum}
`;
promises.push(
fs.writeFile(new URL(`./src/content/blog/post-${i}.mdx`, projectDir), content, 'utf-8')
);
}

await fs.writeFile(
new URL(`./src/pages/blog/[...slug].astro`, projectDir),
`\
Expand All @@ -56,4 +67,16 @@ const { Content } = await entry.render();
);

await Promise.all(promises);

await fs.writeFile(
new URL('./astro.config.js', projectDir),
`\
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';

export default defineConfig({
integrations: [mdx()],
});`,
'utf-8'
);
}
10 changes: 9 additions & 1 deletion packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ export default function markdown({ settings, logger }: AstroPluginOptions): Plug

const fileURL = pathToFileURL(fileId);

const renderResult = await processor!
// `processor` is initialized in `buildStart`, and removed in `buildEnd`. `load`
// should be called in between those two lifecycles, so this error should never happen
if (!processor) {
return this.error(
'MDX processor is not initialized. This is an internal error. Please file an issue.'
);
}

const renderResult = await processor
.render(raw.content, {
// @ts-expect-error passing internal prop
fileURL,
Expand Down
13 changes: 12 additions & 1 deletion packages/integrations/mdx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
),
});

let processor: ReturnType<typeof createMdxProcessor>;
let processor: ReturnType<typeof createMdxProcessor> | undefined;

updateConfig({
vite: {
plugins: [
{
name: '@mdx-js/rollup',
enforce: 'pre',
buildEnd() {
processor = undefined;
},
configResolved(resolved) {
processor = createMdxProcessor(mdxOptions, {
sourcemap: !!resolved.build.sourcemap,
Expand Down Expand Up @@ -118,6 +121,14 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
// 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
if (!processor) {
return this.error(
'MDX processor is not initialized. This is an internal error. Please file an issue.'
);
}

try {
const compiled = await processor.process(vfile);

Expand Down
1 change: 1 addition & 0 deletions packages/markdown/remark/src/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export async function highlightCodeBlocks(tree: Root, highlighter: Highlighter)
for (const { node, language, grandParent, parent } of nodes) {
const meta = (node.data as any)?.meta ?? node.properties.metastring ?? undefined;
const code = toText(node, { whitespace: 'pre' });
// TODO: In Astro 5, have `highlighter()` return hast directly to skip expensive HTML parsing and serialization.
const html = await highlighter(code, language, { meta });
// The replacement returns a root node with 1 child, the `<pr>` element replacement.
const replacement = fromHtml(html, { fragment: true }).children[0] as Element;
Expand Down
Loading