Repository navigation
fix(markdown): keep "use client" on interactive components - #435
Conversation
Vite 8 (Rolldown) printed 20 MODULE_LEVEL_DIRECTIVE warnings on every build. In markdown they were real: the library was bundled into a single dist/index.js, so the "use client" directive at the top of 12 modules (Accordion, Tabs, Tooltip, Mermaid, CopyButton, ...) was dropped. React Server Components frameworks such as the Next.js App Router then treated those components as server components. - markdown: emit one file per source module (preserveModules, rooted at src), so each client module keeps its directive. dist/index.js stays the only public entry; relative imports keep their .js extension, so Node ESM resolves the tree as before - markdown and ui-react: turn off Rolldown's moduleLevelDirective check. ui-react already emits each component as its own entry and kept the directive; Rolldown warns even when it is preserved Flame builds on Bun and Node produce the same HTML for all 30 pages as before (after normalising asset hashes and CSP nonces), and every docs page hydrates without console errors, including the 7 Mermaid diagrams. Lint, typecheck, unit tests and the browser smoke pass. Co-authored-by: Claude Opus 5.5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
I reviewed the switch of @docubook/markdown to per-module output and the warning suppression in both library builds (mode: standard). I also rebuilt both packages locally to check the output.
markdownper-module output:preserveModules+preserveModulesRoot: "src"emits 22 JS files. All 12 client modules (components/{Accordion,Accordions,Callout,Card,Image,Mermaid,Tabs,Tooltip,Tree}Mdx.jsandutils/{AccordionGroupContext,CopyButton,ExpandableCode}.js) now start with"use client". Externals are still external, and the.d.tslayout fromtscmatches the JS layout.import("./dist/index.js")works under Node ESM, andcreateMdxComponents()returns 30 entries.fileNameby entry name: required so the preserved modules don't all collide onindex.js.exports["."]still resolves todist/index.js.checks.moduleLevelDirective: false: both builds now finish without warnings. ui-react's output is unchanged:dist/index.jsre-exports from the per-entry files, and the client entries keep their directive.- Changeset:
@docubook/markdownpatch, which fits a build-output fix with an unchanged public API.
Assessment: ready to merge. Top priorities: none blocking. The note below is optional.
ℹ️ No guard on the directive now that the warning is silenced
The Rolldown warning was what exposed this bug, and both configs now turn it off. If the markdown build ever goes back to a single bundle, or a new client module ends up merged into another chunk, the directive will be dropped again with no warning. The repo convention is that a bug fix comes with a regression test, but this would only be a small post-build check. It's fine to merge without it.
Technical details
# Guard "use client" survival in markdown dist
## Affected sites
- packages/markdown/vite.config.ts:13 — `checks.moduleLevelDirective: false` removes the only build-time signal
## Required outcome
- A check fails when any `src/**/*.tsx` file that starts with `"use client"` maps to a `dist/**/*.js` file that does not start with it
## Suggested approach (optional)
- A small vitest or node script run after `vite build` that globs `src` for the directive and asserts the mirrored `dist` path keeps it (the layout is 1:1 thanks to `preserveModulesRoot: "src"`)claude-opus-5-5 | 𝕏

Summary
pnpm buildprinted 20 RolldownMODULE_LEVEL_DIRECTIVEwarnings ("the module level directive "use client" … may not be preserved when bundling"). In@docubook/markdownthey pointed at a real bug: the package was bundled into onedist/index.js, so the"use client"directive on 12 interactive modules was dropped from the published build. In@docubook/ui-reactthe warnings were noise, because the directive already survived. This PR keeps the directive in markdown and silences the warning in both packages. The build is now warning-free.Why
markdown lost its client boundaries
Rolldown can only keep a module-level directive when the module is emitted as its own chunk. markdown's lib build had a single entry and
fileName: () => "index.js", so all 23 modules were concatenated and the directive was stripped from:"use client"src/componentsAccordionMdx,AccordionsMdx,Callout,CardMdx,ImageMdx,MermaidMdx,TabsMdx,TooltipMdx,TreeMdxsrc/utilsAccordionGroupContext,CopyButton,ExpandableCodeIn a React Server Components framework such as the Next.js App Router, importing
createMdxComponentsfrom the published package therefore treated these hook-using components as server components. Flame renders without RSC, so it was not affected.ui-react was already correct
ui-react builds one lib entry per component in
src/base, and the publisheddist/drawer.js,dist/modal.jsand the others already start with"use client". Rolldown warns about every directive it sees, including directives it keeps.Changes
packages/markdown/vite.config.tsoutput.preserveModules: truewithpreserveModulesRoot: "src": one output file per source module (dist/index.js,dist/registry/index.js,dist/components/*.js,dist/utils/*.js), so each client module keeps its directivefileNamenow uses the entry name, because a fixedindex.jswould make every preserved module collide on one file namechecks.moduleLevelDirective: falsepackages/ui-react/vite.config.ts:checks.moduleLevelDirective: false. No output change..changeset/markdown-keep-use-client.md:@docubook/markdownpatchThe public surface is unchanged.
exports["."]still points atdist/index.js,styles.cssis untouched, and the.d.tslayout fromtscis the same as before. External dependencies (react,react-dom,mermaid,lucide-react,react-icons) are still not inlined. Every relative import in the emitted JS keeps its.jsextension, so Node ESM resolves the tree.Verification
pnpm build --force: 5 of 5 packages build with 0 warnings (was 20). In markdown, all 12 modules listed above start with"use client"indist/.pnpm lint,pnpm typecheckandpnpm testpass (1,124 tests across 5 packages).dist/, on Bun (bun .docu/node/build.ts) and on Node (FLAME_RUNTIME=node node bin/cli.js buildaftercompile:lib). Each build produced 27 pages. After normalizing asset hashes and CSP nonces, all 30 HTML files are identical between old and new on both runtimes. The client bundle differs only in minified module layout.pnpm test:browser-smokepasses on both the Bun and Node builds. A Playwright pass over all 29 doc pages found HTTP 200 on every page, every page hydrated, and no console or page errors. All 7 Mermaid diagrams render, matching the old build.bun bin/cli.js devserves the tabs, tooltips and mermaid pages with no errors. The workspace watcher (packageFingerprintindev-watch.ts) already hashesdist/recursively, so it picks up the per-module files.node -e 'import("@docubook/markdown")'loads the package andcreateMdxComponents()returns 30 components.Not tested: the Deno runtime (not installed in the test environment), and a real Next.js App Router consumer.
Impact on consumers
@docubook/markdown/dist/...were never part ofexports, so they remain unsupported.Out of scope
Flame's client-bundle cache key (
hashMdxSourcesin.docu/node/cache-key.ts) does not include the content or version of the@docubook/*packages it bundles. A project that upgrades@docubook/markdowncan therefore keep its previous client bundle until an MDX or CSS input changes. This was already the case before this PR and does not affect it, because the previous bundle still works. It should be fixed in a separate PR.