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

Tag MDX component for faster checks when rendering #10864

Merged
merged 1 commit into from
Apr 24, 2024

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Apr 24, 2024

Changes

Merges into the mdx-v3 branch. Technically this is non-breaking, but it's easier to merge here to prevent new releases for @astrojs/mdx.

This PR does two things:

  1. Inject __astro_tag_component__(Content, 'astro:jsx') so that we can render MDX components faster. No need to call check() to test if it's astro:jsx as we already tagged it.
  2. Add enhanced error handling for astro:jsx renderToStaticMarkup(). Because of the above, check() wasn't enhancing the error anymore, which failed a test.

Performance:

I tested this in the docs repo but there isn't any significant improvements nor regressions, so this would be harmless. Part of the reason I added this is to port a faster version of https://github.com/withastro/astro/blob/main/packages/astro/src/vite-plugin-mdx/tag.ts.

Testing

Existing tests should pass.

Docs

Added changesets about the improved error message, but otherwise this shouldn't have a significant impact on normal usage.

Copy link

changeset-bot bot commented Apr 24, 2024

🦋 Changeset detected

Latest commit: f513ba6

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added pkg: integration Related to any renderer integration (scope) pkg: astro Related to the core `astro` package (scope) labels Apr 24, 2024
Comment on lines -43 to +42
const importsFromJSXRuntime = imports
.filter(({ n }) => n === 'astro/jsx-runtime')
.map(({ ss, se }) => code.substring(ss, se));
const hasFragmentImport = importsFromJSXRuntime.some((statement) =>
fragmentImportRegex.test(statement)
);
if (!hasFragmentImport) {
code = `import { Fragment } from "astro/jsx-runtime"\n` + code;
if (!isSpecifierImported(code, imports, fragmentImportRegex, 'astro/jsx-runtime')) {
code += `\nimport { Fragment } from 'astro/jsx-runtime';`;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the code = to code +=. This causes the import statement to append the code, but ESM hoists the import specifiers, so you can actually use Fragment beforehand.

The reason I append is to avoid generating a sourcemap as we don't alter existing code positions.

Comment on lines +111 to +123
// Tag the `Content` export as "astro:jsx" so it's quicker to identify how to render this component
if (ssr) {
if (
!isSpecifierImported(
code,
imports,
astroTagComponentImportRegex,
'astro/runtime/server/index.js'
)
) {
code += `\nimport { __astro_tag_component__ } from 'astro/runtime/server/index.js';`;
}
code += `\n__astro_tag_component__(Content, 'astro:jsx');`;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mimics tag.ts but specific to the MDX component so it's much simpler. We only need to tag the default exported Content component. I don't think there will be other exports that are MDX components.

@bluwy bluwy merged commit 5aa5433 into mdx-v3 Apr 24, 2024
13 checks passed
@bluwy bluwy deleted the mdx-cleanup-postprocess branch April 24, 2024 14:37
@bluwy bluwy mentioned this pull request May 2, 2024
ematipico pushed a commit that referenced this pull request May 8, 2024
* fix(mdx): convert remark-images-to-component plugin to a rehype plugin (#10697)

* Remove fs read for MDX transform (#10866)

* Tag MDX component for faster checks when rendering (#10864)

* Use unified plugin only for MDX transform (#10869)

* Only traverse children and handle mdxJsxTextElement when optimizing (#10885)

* Rename to `optimize.ignoreComponentNames` in MDX (#10884)

* Allow remark/rehype plugins added after mdx to work (#10877)

* Improve MDX optimize with sibling nodes (#10887)

* Improve types in rehype-optimize-static.ts

* Rename `ignoreComponentNames` to `ignoreElementNames`

I think this better reflects what it's actually doing

* Simplify plain MDX nodes in optimize option (#10934)

* Format code

* Minimize diff changes

* Update .changeset/slimy-cobras-end.md

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

---------

Co-authored-by: Armand Philippot <59021693+ArmandPhilippot@users.noreply.github.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants