Extract JSON parsing helper to eliminate json5 dependency

Merged
MA
3mo ago
+317-31

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Description

Completes the removal of the json5 dependency from the frontend by extracting the non-finite float parsing logic into a dedicated helper module. This fixes a regression where the upload helper was still importing json5, causing its bundled core-js 2.6.5 runtime to be embedded in production bundles.

Changes:

  1. New helper module (packages/reflex-base/src/reflex_base/.templates/web/utils/helpers/json.js):

    • Exports parseJson() function that handles Python's bare NaN/Infinity/-Infinity tokens
    • Uses a fast path (native JSON.parse) for well-formed payloads
    • Falls back to token rewriting only when parsing fails
    • Includes comprehensive documentation
  2. Updated state.js:

    • Removed inline non-finite float parsing logic (moved to helper)
    • Imports and uses parseJson from the new helper module
    • Simplified socket decoder error handling
  3. Updated upload.js:

    • Removed json5 import
    • Uses parseJson helper instead of JSON5.parse()
  4. Comprehensive test coverage (tests/units/reflex_base/templates/test_json_helper.py):

    • Regression test ensuring no templates import json5
    • Tests for parsing Python's non-finite float tokens
    • Tests for plain JSON round-tripping
    • Tests for string literal preservation
    • Tests for error handling on unparseable payloads
  5. Integration test (tests/integration/test_prod_build_pipeline.py):

    • Verifies that built bundles do not contain __core-js_shared__ markers
    • Ensures the core-js runtime is not embedded in production assets

Closes

#7163

Testing

  • Added unit tests covering all parsing scenarios (non-finite floats, plain JSON, string preservation, error cases)
  • Added regression test to verify no templates import json5
  • Added integration test to verify production bundles don't embed core-js
  • All tests pass with Node.js available (skipped gracefully if missing)

https://claude.ai/code/session_019AVQ7zFzj4UEysGQPwi2XK