Skip to content

fix(mermaid): import the bundled ESM build so diagrams render in dev - #74

Merged
Infi-Knight merged 1 commit into
mainfrom
fix/docsdev-75-mermaid-dev-import
Sep 7, 2026
Merged

fix(mermaid): import the bundled ESM build so diagrams render in dev#74
Infi-Knight merged 1 commit into
mainfrom
fix/docsdev-75-mermaid-dev-import

Conversation

@Infi-Knight

@Infi-Knight Infi-Knight commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Mermaid diagrams do not render on the Vite dev server. They render correctly in a production build, so every deploy preview looks right.

Reported by Anca on docs-styleguide#119.

Cause

0.14.0 changed the import:

- import mermaid from '/node_modules/mermaid/dist/mermaid.esm.min.mjs'
+ import mermaid from 'mermaid'

The bare specifier resolves to dist/mermaid.core.mjs. That build treats dayjs as an external CommonJS package. A bundler fixes the interop. The dev server does not, so the module throws:

The requested module '/node_modules/dayjs/dayjs.min.js' does not provide
an export named 'default'

Mermaid never loads, initialize never runs, and the raw diagram source shows on the page.

Fix

import mermaid from 'mermaid/dist/mermaid.esm.min.mjs'

This build inlines its dependencies, so it needs no interop. It is a package subpath, not an absolute path, so it resolves in dev and in a build.

Scope

This is not specific to Astro 7. The same failure occurs on Astro 6.3.8 with Vite 7, on a clean install. The mermaid version does not matter either, because 11.15.0 and 11.17.2 both map mermaid to the same file.

Four repos use this component, with 63 diagrams between them: rafiki 35, open-payments 18, docs-styleguide 8, rafiki-v2 2. All fail on the dev server. No deployed site is affected.

Verified

In docs-styleguide, on a cold cache:

Check Before After
Dev, /shared/mermaid/ 0 of 1 1 of 1
Dev, /shared/mermaidwrapper/ 0 of 3 3 of 3, buttons work
Build and preview 1 of 1 1 of 1

Console is clean. pnpm lint passes.

After merge

Publish 0.14.1, then refresh the lockfile on the four consumer PRs. Their ranges read ^0.14.0, which accepts 0.14.1, but a committed lockfile holds the old version.

Diagrams did not render on the Vite dev server. They rendered correctly in
a production build, so the problem was invisible to every check in this
cycle, which used `astro preview` against built output.

Cause:

Version 0.14.0 changed the import from an absolute path to the bare
`mermaid` specifier. The bare specifier resolves through the package
exports map to `dist/mermaid.core.mjs`. That build treats dayjs and other
CommonJS packages as externals. A bundler fixes the interop at build time.
The dev server does not, so the module throws:

  The requested module '/node_modules/dayjs/dayjs.min.js' does not
  provide an export named 'default'

The whole mermaid module then fails to load, `mermaid.initialize` never
runs, and the raw diagram source shows on the page.

Fix:

Import `mermaid/dist/mermaid.esm.min.mjs`. That build has its
dependencies inlined, so it needs no CommonJS interop. It is a package
subpath, not an absolute path, so it resolves in dev and in a build.

Verified in docs-styleguide on both paths:

- Dev server: `/shared/mermaid/` renders 1 of 1, and
  `/shared/mermaidwrapper/` renders 3 of 3 with all 3 buttons
- Production build and preview: renders 1 of 1
- The view and download buttons still find the SVG
- No console errors

Four repos consume this component: docs-styleguide, open-payments, rafiki
and rafiki-v2, with 63 diagrams between them. All were affected in dev.

Reported by Anca on the docs-styleguide PR.

@Anca2022 Anca2022 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not convinced this causes the bug, but let's test it out

@Infi-Knight
Infi-Knight merged commit 0c2e22a into main Sep 7, 2026
1 check passed
@Infi-Knight
Infi-Knight deleted the fix/docsdev-75-mermaid-dev-import branch September 7, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants