Fixing an MDX Component Resolution Bug in Astro

I was adding a docs page to an Astro project and kept the imports at the bottom of the MDX file because the prose came first and the components were grouped after it. One of those components, a Callout, resolved in a smaller test file but failed on the docs page. Moving the import above the first usage fixed it, which was suspicious: ESM imports are hoisted, so authoring order should not matter.

Astro's MDX compiler builds a HAST tree and runs metadata plugins over it. The old createAstroMetadataPlugin(filePath) did two jobs in one traversal: it collected ESM imports into a map and rewrote JSX component references using that map. A HAST walk is document-ordered, not module-ordered. If a component usage preceded its import, the resolver ran against an incomplete map and the component looked unresolved, even though the module was valid. The plugin had no way to tell "this component was never imported" from "this import has not been visited yet."

const astroMeta = createAstroMetadataPlugin(filePath);
// [astro-metadata-imports, astro-metadata]

hastPlugins.push(
  imageToComponent,
  headingIds,
  ...astroMeta,
  literalizeScriptStyleExpression,
  collapseScriptStyleText,
);

The fix splits the plugin into astro-metadata-imports and astro-metadata with a shared import map. The first pass walks the entire tree and collects imports; the second walks it again and resolves JSX against the complete map. createAstroMetadataPlugin() now returns a HastPluginDefinition[], and create-processor.ts spreads ...astroMeta into the HAST plugin list in the same position as before. The extra traversal is the cost; the benefit is that tree order no longer leaks into module resolution.

I reduced the reproduction to two MDX files with the same component and the only difference being the position of the import. That made the ordering dependency obvious and ruled out the component itself. The regression coverage is now in mdx-client-components.test.ts, with the same component reference and the import placed both before and after it. The previous suite only exercised the common top-of-file import shape, so the single-pass assumption was never challenged. With both orders covered, the suite passed with 30 tests, and any future return to a single collection/resolution pass should fail one of them. View the pull request on GitHub →

               . .
    . .       . . .
 . . . .       . .     .
. . . .       . . .   . . .               ,
 . . . .     . . .     . .       _       d#D
. . . . .   . . . . ._. . .    _(%)_      `
   .     . . . . . _(#)_ .    (%%O%%)     "Bzzzz"
        . . . . _ (##O##) .   _ (%)   _
       . . .  _(#)_ (#)  . _ (%)_:  _(%)_
      . . . .(##O##)._\  _(#)%O%%):(%%O%%)
         . .   (#)  _  \(##O##))  |,'(%)
              `. ._(#)_ \,(#)  `. ;  _
                 (##O##) `.      Y _(#)_
                  .(#)\ . .).   / (##O##)
                 . . . `-.(. . ;  ,'(#)
                  . . . . .\. .| (
                 . . . d888888888888b
                  . . ,88888888888888b
                   . .d888888888888888
                    . d888888888888888
                     .`88888888888888P
                       Y8888888888888
                        Y8888888888P
_________________________`Y8888888P____________