Repository navigation
Description
Problem
.web/utils/state.js (template: packages/reflex-base/src/reflex_base/.templates/web/utils/state.js) calls direct eval in four places:
- L469
await eval(\import(dataUri)`)` - L544
eval(event.payload.callback) - L762–763
eval(event.payload.javascript_code)/eval(event.payload.function)()
A direct eval can read and write every variable in scope, so the minifier must keep all top-level names in the chunk that contains it unmangled. With frontend_lazy_bundled_libraries=True, @radix-ui/themes and its primitives are reached both statically and through the lazy import("@radix-ui/themes") loader, so the bundler moves them into the same shared chunk as state.js (preload-helper-*.js), where none of their names can be shortened.
Measured in prod (reflex run --env prod, single port) on a test app with bundle_library(...) for lucide icons and a recharts chart, reflex 0.11.0a2, decoded bytes:
| page | flag off | flag on | delta |
|---|---|---|---|
/ (no recharts) |
2,134,012 B | 1,608,413 B | −525.6 KB (the #7276 win) |
/recharts (uses recharts directly) |
1,392,087 B | 1,463,610 B | +71.5 KB (+5.3 KB gzip) |
- The extra bytes come from minification, not from extra code. Sourcemaps show the same modules in both builds.
@radix-ui/themesis 86,953 B with the flag off and 98,717 B with it on. With the flag on, the shared chunk keeps names likeSELECT_NAMEandcreateSelectContext; with it off,root-*.jsshortens them toyh,mhand so on. - 0.10.1 behaves the same. Its shared chunk is 464,556 B and also holds the
eval. - The fix is worth about 89 KB. In a scratch build, changing
eval(to(0, eval)(shrank the shared chunk from 468,178 B to 379,101 B. With that,/rechartswith the flag on is about 17.5 KB lighter than with it off, and every page that loads the shared chunk benefits.
This is the remaining part of #7262: #7276 fixed the every-page growth, and this is the leftover.
Fix
- Make every
evalinstate.jsindirect ((0, eval)(...)orglobalThis.eval(...)), or replace it:- for
import(dataUri), useimport(/* @vite-ignore */ dataUri)directly, or keepnew Functionif the dynamic import must stay hidden from the bundler; - for the event payloads, use
new Function(...).
- for
- Indirect eval runs in global scope, so first confirm that none of the evaluated strings depend on
state.js's local scope. The callback andcall_scriptpayloads are generated byreflex_base(rx.call_script,rx.call_function, event-chain callbacks). Grep the generators for identifiers that are only instate.js's module scope (for exampleapplyEvent,refsorgetRefValue), and export toglobalThisor pass in as parameters whatever they rely on.refsis already published as a global since Publish refs as a global for 0.10 compatibility #7806. - Tests:
tests/js/vitest for the eval call sites with module-local names unavailable, and the existing integration tests forrx.call_script/rx.call_functioncallbacks. Optionally a size check: build an app with the flag on and off and assert the shared chunk shrinks.
Activity
linear-code commentedon Nov 12, 2026
ENG-14012
TRACKING: close #7476/#6122 on a2 evidence; #7262 remainder filed as #…