Skip to content

feat: propagate VarData.module_code into page module code#6456

Open
FarhanAliRaza wants to merge 9 commits into
reflex-dev:mainfrom
FarhanAliRaza:app-module-code-vardata
Open

feat: propagate VarData.module_code into page module code#6456
FarhanAliRaza wants to merge 9 commits into
reflex-dev:mainfrom
FarhanAliRaza:app-module-code-vardata

Conversation

@FarhanAliRaza
Copy link
Copy Markdown
Contributor

@FarhanAliRaza FarhanAliRaza commented May 5, 2026

Adds module_code on VarData so Vars can contribute top-of-file JS helpers/constants. The default collector and the legacy _get_all_custom_code path both pick it up, ensuring snippets carried by Vars on memoized stateful components aren't dropped from the memo file.

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

New Feature Submission:

  • Does your submission pass the tests?
  • Have you linted your code locally prior to submission?

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

closes #6219
fixes ENG-9151

Adds module_code on VarData so Vars can contribute top-of-file JS
helpers/constants. The default collector and the legacy
_get_all_custom_code path both pick it up, ensuring snippets carried
by Vars on memoized stateful components aren't dropped from the memo
file.
@FarhanAliRaza FarhanAliRaza requested a review from a team as a code owner May 5, 2026 06:53
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 5, 2026

Merging this PR will degrade performance by 17.06%

❌ 3 regressed benchmarks
✅ 21 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
test_compile_page[_stateful_page] 1.7 ms 2.2 ms -22.37%
test_compile_single_pass_all_artifacts[_complicated_page] 80.9 ms 83.5 ms -3.09%
test_compile_page[_complicated_page] 8.5 ms 11.2 ms -24.16%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing FarhanAliRaza:app-module-code-vardata (bc7ff54) with main (e88acf7)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (7281317) during the generation of this report, so e88acf7 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 5, 2026

Greptile Summary

This PR adds a module_code: tuple[str, ...] field to VarData, allowing Vars to contribute top-of-file JS helpers/constants to the page module. Collection is wired into both the DefaultCollectorPlugin tree-walker path and the legacy _get_all_custom_code recursion used by the memo-compile pipeline, with deduplication via dict.fromkeys / setdefault throughout.

  • Two integration-test routes (/multi, /hook) are navigated via frontend_url + \"multi\" / frontend_url + \"hook\" without a leading slash or .removesuffix(\"/\"), producing malformed URLs when frontend_url has no trailing slash — the same pattern every other integration test in this repo avoids with .removesuffix(\"/\").

Confidence Score: 4/5

Core implementation is correct and well-tested; the only defect is in the new integration test's URL construction, which would cause two of the three new playwright tests to navigate to wrong addresses.

The module_code field, its merge logic, and all three collection paths are implemented correctly. The integration test for /multi and /hook routes concatenates frontend_url directly with the path segment without a leading slash or .removesuffix('/'), so those tests would navigate to malformed URLs and fail to validate the behavior they were written to cover.

tests/integration/tests_playwright/test_var_module_code.py — both routed test functions need the URL construction fixed before the integration tests can be relied on.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/vars/base.py Adds module_code: tuple[str, ...] field to VarData with proper default, hook-merge propagation, deduplication in merge(), and __bool__ support. Implementation is clean and consistent with existing field patterns.
packages/reflex-base/src/reflex_base/components/component.py Adds _iter_var_module_code() that yields module_code from component Vars, internal hooks, and added hooks; integrates it into _get_all_custom_code() with deduplication via setdefault. Architecture is correct and mirrors existing custom-code patterns.
reflex/compiler/plugins/builtin.py Adds _collect_var_module_code() static method and calls it in both the regular and the fast-path compiled leave hook, matching the placement of _collect_component_custom_code. Correct and symmetric.
reflex/compiler/utils.py Adds _iter_var_module_code() call to _root_only_custom_code() so the memo-compile path picks up Var-level module snippets. Change is minimal and correctly placed.
tests/integration/tests_playwright/test_var_module_code.py New integration test covering the three cases. URL concatenation for the /multi and /hook routes is missing a leading slash separator, which will cause tests to navigate to malformed URLs if frontend_url has no trailing slash.
tests/units/compiler/test_plugins.py Adds unit test for the DefaultCollectorPlugin collecting var module_code. Straightforward and covers the new code path.
tests/units/components/test_component.py Adds unit test confirming that var module_code reaches the legacy _get_all_custom_code() path used by the memo compile pipeline.
tests/units/test_var.py Adds three focused unit tests for module_code default/truthiness, merge deduplication with order preservation, and hook-VarData propagation. Coverage is thorough for the new field.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["VarData\nmodule_code field"] -->|merge / dedup| B["VarData.merge"]
    B --> C["Var._get_all_var_data"]
    C --> D["Component._iter_var_module_code"]
    D --> E["Legacy path\n_get_all_custom_code"]
    D --> F["Plugin path\n_collect_var_module_code"]
    D --> G["Memo path\n_root_only_custom_code"]
    E --> H["Page module_code dict"]
    F --> H
    G --> H
Loading

Reviews (1): Last reviewed commit: "feat: propagate VarData.module_code into..." | Re-trigger Greptile

Comment thread tests/integration/tests_playwright/test_var_module_code.py
Comment thread tests/integration/tests_playwright/test_var_module_code.py
FarhanAliRaza and others added 8 commits May 5, 2026 11:59
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Vars (and hook VarData) can now carry top-of-file JS helpers/constants via a
module_code field, deduplicated on merge and surfaced through
Component._iter_var_module_code; the page compiler emits these alongside
component custom code.
Page compilation walked each component's Vars twice (once for hooks,
once for module_code) and called the uncached _get_added_hooks more
than once per component.

Gather Var module_code during the existing _get_vars_hooks walk and
cache it on _var_module_code_cache, exposed via _iter_var_only_module_code.
In the builtin collector, fetch _get_hooks_internal/_get_added_hooks
once per component and thread them into both the module_code scan and
page-hook aggregation.
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.

Compiler: Add module_code field to VarData

2 participants