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

Use unified plugin only for MDX transform #10869

Merged
merged 3 commits into from
Apr 26, 2024
Merged

Use unified plugin only for MDX transform #10869

merged 3 commits into from
Apr 26, 2024

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Apr 24, 2024

Changes

  • Port jsx/babel.ts to jsx/rehype.ts. The file logic are essentially the same thing but written as a rehype plugin.
  • The builtin vite-plugin-mdx plugin is deleted by @astrojs/mdx, the integration will takeover MDX transformation instead, using the rehype plugin.

Performance:

Rollup build time of Astro docs dropped from 108s to 96s (12s or 11% faster)

How this perf improvement works is by removing the hoops needed to compile MDX as JS:

  • Before: MDX string -> mdast -> hast -> esast -> JSX string -> esbuild -> babel ast -> JS string
  • After: MDX string -> mdast -> hast -> esast -> JS string

Testing

Existing tests should pass, we have extensive coverage for MDX usecases and edgecases.

Docs

This is a breaking change. I've added a changeset explaning the change, but the only change needed is to upgrade to Astro v4.8.0 (to be released together)

Copy link

changeset-bot bot commented Apr 24, 2024

🦋 Changeset detected

Latest commit: 9209ee0

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) semver: minor Change triggers a `minor` release labels Apr 24, 2024
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

This PR is blocked because it contains a minor changeset. A reviewer will merge this at the next release if approved.

@bluwy

This comment was marked as outdated.

@bluwy bluwy marked this pull request as draft April 24, 2024 16:05
@bluwy bluwy marked this pull request as ready for review April 25, 2024 05:03
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 file is a port of ./babel.ts so I understand if this is a bit raw to review 😄 Let me know if there's part you don't understand or grasp and I can add additional comments.

@@ -30,7 +31,6 @@ export function createMdxProcessor(mdxOptions: MdxOptions, extraOptions: MdxProc
rehypePlugins: getRehypePlugins(mdxOptions),
recmaPlugins: mdxOptions.recmaPlugins,
remarkRehypeOptions: mdxOptions.remarkRehype,
jsx: true,
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 is removed so the default of jsx is false, so MDX will compile directly to JS instead of needing a babel step.

Copy link
Member

Choose a reason for hiding this comment

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

That must be quite faster I assume!

Comment on lines +40 to +41
function injectUnderscoreFragmentImport(code: string, imports: readonly ImportSpecifier[]) {
if (!isSpecifierImported(code, imports, underscoreFragmentImportRegex, '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 changed the Fragment detection to _Fragment as it's often already generated by MDX, which should reduce the code generation.

@ematipico ematipico added this to the 4.8.0 milestone Apr 25, 2024
@bluwy
Copy link
Member Author

bluwy commented Apr 25, 2024

@ematipico This is merging into the mdx-v3 branch, so I'd like to have this reviewed and merged soon if possible, until everything's ready then I'll send the PR for mdx-v3 into main with the 4.8 milestone 😅

@ematipico
Copy link
Member

I didn't see that, sorry @bluwy

Feel free to remove the milestone and merge it whenever you want

@ematipico ematipico removed this from the 4.8.0 milestone Apr 25, 2024
@bluwy bluwy merged commit 828adc3 into mdx-v3 Apr 26, 2024
14 checks passed
@bluwy bluwy deleted the mdx-no-babel branch April 26, 2024 06:15
@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 <[email protected]>

---------

Co-authored-by: Armand Philippot <[email protected]>
Co-authored-by: Sarah Rainsberger <[email protected]>
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) semver: minor Change triggers a `minor` release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants