fix(recharts): bundle es-toolkit/compat as ESM (#6561)#6567
Conversation
…6561) recharts default-imports CommonJS `es-toolkit/compat/*` shims from its core utils (ChartUtils, DataUtils), so every chart pulls CJS into the bundle. Vite's Rolldown (prod) and Rollup (dev) emit the CJS interop helper into a chunk that can load after React Router's lazy route chunk, so any page with a chart throws a TypeError and reloads forever. Add a Vite plugin that redirects `es-toolkit/compat/<name>` default-imports to the pure-ESM `es-toolkit/compat` barrel, eliminating the CJS interop helper. Also add a prod+dev recharts integration test that renders an area chart and asserts no page error. https://claude.ai/code/session_01B53hepRQu8LCtZxaxAFPSm
Merging this PR will not alter performance
Comparing |
The Vite plugin only covered the production build. In dev, Vite 8 prebundles deps with Rolldown before the build plugins run, so recharts' CommonJS `es-toolkit/compat/*` imports were still bundled with the broken interop helper (TypeError: require_isUnsafeProperty is not a function). Register the same plugin under `optimizeDeps.rolldownOptions.plugins` so the dev dependency optimizer redirects them to the pure-ESM barrel as well. https://claude.ai/code/session_01B53hepRQu8LCtZxaxAFPSm
|
@greptile |
Greptile SummaryFixes a runtime crash affecting any page with an
Confidence Score: 5/5Safe to merge — the change is a targeted, additive Vite plugin with no modification to existing logic. The plugin is narrow in scope: it only intercepts the exact import pattern No files require special attention. Important Files Changed
Reviews (2): Last reviewed commit: "Merge branch 'main' into claude/modest-h..." | Re-trigger Greptile |
There was a problem hiding this comment.
Pull request overview
This PR fixes a runtime crash/infinite route-reload loop that could occur when rendering rx.recharts charts by ensuring es-toolkit/compat/* imports stay ESM-only in the Vite module graph, avoiding emission of problematic CJS interop helpers (per #6561).
Changes:
- Add a generated Vite plugin that rewrites
es-toolkit/compat/<name>default-imports to re-export from the ESMes-toolkit/compatbarrel (prod build + dev dependency prebundle). - Add a Playwright integration test that renders a recharts chart and asserts the SVG appears with no
pageerrors in both dev and prod harness modes. - Add a news entry documenting the regression fix.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
packages/reflex-base/src/reflex_base/compiler/templates.py |
Extends the generated vite.config.js with an ESM-redirect plugin and registers it for both build plugins and dev prebundle. |
tests/integration/tests_playwright/test_recharts.py |
Adds regression coverage ensuring recharts renders without runtime errors in dev and prod. |
packages/reflex-base/news/6561.bugfix.md |
Adds release-note entry describing the recharts runtime crash fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
this is probably the wrong layer to patch this; upstream libraries should be handling this better. a fix like this is nothing more than a compat hack |
Summary
Fixes #6561. Pages that render a
rx.rechartschart could crash at runtime with aTypeErrorand an infinite route-reload loop.recharts default-imports the CommonJS
es-toolkit/compat/*shims (get,range,sortBy,throttle, …). These are reached through recharts' shared chart utilities, so even a single chart pulls a CJS module into the bundle. Vite then emits the CJS interop helper into a chunk that isn't guaranteed to be initialized before a lazily-loaded route chunk runs, which surfaces asTypeError: … is not a functionand a reload loop.This adds a small Vite plugin to the generated
vite.config.jsthat redirectses-toolkit/compat/<name>default-imports to the pure-ESMes-toolkit/compatbarrel (it re-exports the same functions). The module graph stays ESM-only, so no CJS interop helper is emitted, and the plugin is a no-op for apps that don't importes-toolkit/compat.Thanks @hctilf for the thorough report and reproduction.
Verification
Building a minimal
import get from 'es-toolkit/compat/get'with Vite 8:__commonJSinterop helper and throwsrequire_isUnsafeProperty is not a functionat runtimeget()still worksAlso adds
tests/integration/tests_playwright/test_recharts.py, which renders an area chart and asserts the SVG appears with no page errors. It runs in both dev and prod via the existingapp_harness_envfixture.Test plan
Generated by Claude Code