Skip to content

fix(CodeBlock): import the ESM highlighter languages in the ESM build - #1329

Open
marsian83 wants to merge 1 commit into
ClickHouse:mainfrom
marsian83:fix/codeblock-ssr-language-imports
Open

marsian83 wants to merge 1 commit into
ClickHouse:mainfrom
marsian83:fix/codeblock-ssr-language-imports

Conversation

@marsian83

@marsian83 marsian83 commented Nov 25, 2026 •

Copy link
Copy Markdown

Description

In a Vite dev server that renders on the server through Vite's module runner, such as Waku, every page that imports from @clickhouse/click-ui fails to server render (#1020):

Error during rendering: TypeError: _interopRequireDefault is not a function
    at eval (node_modules/react-syntax-highlighter/dist/cjs/languages/hljs/sql.js:8:12)
    at async ESModulesEvaluator.runInlinedModule (node_modules/vite/dist/node/module-runner.js:807:3)
    ...
    at async eval (node_modules/@clickhouse/click-ui/dist/esm/components/CodeBlock/index.js:12:31)
    ...
    at async eval (node_modules/@clickhouse/click-ui/dist/esm/index.js:33:1)

The page responds with a 500 and an empty <body>, and the browser renders it from the RSC payload. Production builds (waku build) are not affected.

In the ESM build, CodeBlock imports Light from react-syntax-highlighter, which bundlers resolve to its ESM build, but its six languages from react-syntax-highlighter/dist/cjs/.... When Vite's module runner evaluates one of those CJS files, its require('@babel/runtime/helpers/interopRequireDefault') returns an ES module instead of the function.

  • CodeBlock imports the languages from react-syntax-highlighter/dist/esm/....
  • The CJS output maps those paths back to dist/cjs/... with Rollup's output.paths, so the CJS build keeps requiring CJS files. Pointing it at the ESM files would break Jest setups that don't transform node_modules.
  • Removed the .default || x fallbacks from the registerLanguage calls.

Links and tickets

Closes #1020

Good to know

Tested with packed builds (yarn pack) of main and this branch:

Consumer main This branch
Waku 1.0.0-rc.5, waku dev, page importing only Button 500, error above 200
Waku waku dev, SQL CodeBlock 500 200, highlighted
Waku waku build + waku start 200, highlighted 200, highlighted
Next 16.4.6, next build with webpack and with Turbopack builds builds; webpack build served: 200, highlighted
CJS build rendered with react-dom/server, CSS stubbed as Jest would highlighted identical output
  • Client JS in the Next webpack build is 809 B smaller (194 B gzip), since the ESM language files are bare re-exports.
  • No new unit test: Vitest runs CodeBlock from source through Vite, which loads either format

Follow-ups I noticed and kept out of this PR:

  • CodeBlock registers its languages at module top level, so importing anything from the package root runs it. Registering on first render would keep imports stabl.
  • language is typed as string, so an unsupported language silently renders as plain text. tightening the type, or letting consumers register more languages, is a public API change, so Ig i should propose that in an issue first?

Checklist

  • Breaking changes? (add migration notes in changesets)
  • Visual changes? (specify in changesets)
  • Design review needed?

Contribution

  • Sufficient research before PR
  • Self-reviewed the PR
  • Manually tested the changes (when applies - visual confirmation in Storybook)
  • build and build-storybook work locally
  • Tests and Stories are aligned with the changes: no behavior change in Storybook, see "Good to know".

Accessibility

No markup, behavior or style changes.

Screenshots

Network tab, waku dev, page importing only Button:

Before After
Network tab: GET / returns 500 Network tab: GET / returns 200

Note

Medium Risk
Touches dual-format packaging and dependency resolution for CodeBlock, but scope is narrow and backed by new build guards plus manual Waku/Next/Jest verification.

Overview
Fixes server-side failures (_interopRequireDefault is not a function) in Vite SSR consumers (e.g. Waku) when anything from the package root loads CodeBlock, which previously pulled react-syntax-highlighter language modules from dist/cjs in the ESM output.

CodeBlock now imports languages from dist/esm/... and registers them without CJS interop fallbacks. The CJS bundle rewrites those paths back to dist/cjs via Rollup output.paths in vite.config.ts, so Jest-style CJS consumers stay on CommonJS deps.

The build health check script now fails the build if ESM artifacts import another package’s CJS build, or CJS artifacts require ESM builds — guarding against this class of SSR/Jest breakage going forward. Highlighting behavior is unchanged; patch release noted in the changeset.

Reviewed by Cursor Bugbot for commit 308b44a. Bugbot is set up for automated code reviews on this repo. Configure here.

@changeset-bot

changeset-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 308b44a

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

This PR includes changesets to release 1 package
Name Type
@clickhouse/click-ui Patch

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

@marsian83
marsian83 marked this pull request as ready for review November 27, 2026 15:51

This branch has not been deployed

No deployments
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.

[Bug]: CodeBlock breaks SSR/client evaluation in Waku/Vite due to CJS react-syntax-highlighter language imports

1 participant