Skip to content

Commit ae477ae

Browse files
allow to enable/disable state and event ID minification independently via env vars
1 parent 5b6e124 commit ae477ae

6 files changed

Lines changed: 281 additions & 24 deletions

File tree

reflex/environment.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,13 @@ class PathExistsFlag:
478478
ExistingPath = Annotated[Path, PathExistsFlag]
479479

480480

481+
class MinifyMode(enum.Enum):
482+
"""Mode for minification of state/event IDs."""
483+
484+
ENABLED = "enabled"
485+
DISABLED = "disabled"
486+
487+
481488
class PerformanceMode(enum.Enum):
482489
"""Performance mode for the app."""
483490

@@ -762,6 +769,12 @@ class EnvironmentVariables:
762769
# How long to opportunistically hold the redis lock in milliseconds (must be less than the token expiration).
763770
REFLEX_OPLOCK_HOLD_TIME_MS: EnvVar[int] = env_var(0)
764771

772+
# Whether to enable state ID minification (requires minify.json).
773+
REFLEX_MINIFY_STATE: EnvVar[MinifyMode] = env_var(MinifyMode.DISABLED)
774+
775+
# Whether to enable event ID minification (requires minify.json).
776+
REFLEX_MINIFY_EVENTS: EnvVar[MinifyMode] = env_var(MinifyMode.DISABLED)
777+
765778

766779
environment = EnvironmentVariables()
767780

reflex/minify.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,46 @@ def get_minify_config() -> MinifyConfig | None:
115115

116116

117117
def is_minify_enabled() -> bool:
118-
"""Check if minification is enabled.
118+
"""Check if any minification is enabled (state or event).
119119
120120
Returns:
121-
True if minify.json exists and is valid.
121+
True if either state or event minification is enabled.
122122
"""
123-
return get_minify_config() is not None
123+
return is_state_minify_enabled() or is_event_minify_enabled()
124+
125+
126+
@functools.cache
127+
def is_state_minify_enabled() -> bool:
128+
"""Check if state ID minification is enabled.
129+
130+
Requires both REFLEX_MINIFY_STATE=enabled and minify.json to exist.
131+
132+
Returns:
133+
True if state minification is enabled.
134+
"""
135+
from reflex.environment import MinifyMode, environment
136+
137+
return (
138+
environment.REFLEX_MINIFY_STATE.get() == MinifyMode.ENABLED
139+
and get_minify_config() is not None
140+
)
141+
142+
143+
@functools.cache
144+
def is_event_minify_enabled() -> bool:
145+
"""Check if event ID minification is enabled.
146+
147+
Requires both REFLEX_MINIFY_EVENTS=enabled and minify.json to exist.
148+
149+
Returns:
150+
True if event minification is enabled.
151+
"""
152+
from reflex.environment import MinifyMode, environment
153+
154+
return (
155+
environment.REFLEX_MINIFY_EVENTS.get() == MinifyMode.ENABLED
156+
and get_minify_config() is not None
157+
)
124158

125159

126160
def get_state_id(state_full_path: str) -> str | None:
@@ -175,6 +209,8 @@ def clear_config_cache() -> None:
175209
This should be called after modifying minify.json programmatically.
176210
"""
177211
get_minify_config.cache_clear()
212+
is_state_minify_enabled.cache_clear()
213+
is_event_minify_enabled.cache_clear()
178214

179215

180216
# Base-54 encoding for minified names

reflex/reflex.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,9 +1017,9 @@ def minify_list(output_json: bool):
10171017

10181018
from reflex.minify import (
10191019
get_event_id,
1020+
get_minify_config,
10201021
get_state_full_path,
10211022
get_state_id,
1022-
is_minify_enabled,
10231023
)
10241024
from reflex.state import BaseState, State
10251025
from reflex.utils import prerequisites
@@ -1042,7 +1042,8 @@ class StateTreeData(TypedDict):
10421042
# Load the user's app to register all state classes
10431043
prerequisites.get_app()
10441044

1045-
minify_enabled = is_minify_enabled()
1045+
# CLI inspection always shows config contents regardless of env var settings
1046+
minify_enabled = get_minify_config() is not None
10461047

10471048
def build_state_tree(state_cls: type[BaseState]) -> StateTreeData:
10481049
"""Recursively build state tree data.

reflex/state.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,13 @@ def _register_event_handler_for_minify(cls, handler_name: str) -> None:
694694
Args:
695695
handler_name: The original name of the event handler.
696696
"""
697-
from reflex.minify import get_event_id, get_minify_config, get_state_full_path
697+
from reflex.minify import (
698+
get_event_id,
699+
get_state_full_path,
700+
is_event_minify_enabled,
701+
)
698702

699-
if get_minify_config() is None:
703+
if not is_event_minify_enabled():
700704
return
701705

702706
state_path = get_state_full_path(cls)
@@ -1030,13 +1034,17 @@ def get_name(cls) -> str:
10301034
Returns:
10311035
The name of the state (minified if configured in minify.json).
10321036
"""
1033-
from reflex.minify import get_state_full_path, get_state_id, is_minify_enabled
1037+
from reflex.minify import (
1038+
get_state_full_path,
1039+
get_state_id,
1040+
is_state_minify_enabled,
1041+
)
10341042

10351043
module = cls.__module__.replace(".", "___")
10361044
full_name = format.to_snake_case(f"{module}___{cls.__name__}")
10371045

1038-
# If minification is enabled, look up the state ID from minify.json
1039-
if is_minify_enabled():
1046+
# If state minification is enabled, look up the state ID from minify.json
1047+
if is_state_minify_enabled():
10401048
state_path = get_state_full_path(cls)
10411049
state_id = get_state_id(state_path)
10421050
if state_id is not None:

reflex/utils/format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def get_event_handler_parts(
452452
The state and function name (possibly minified based on minify.json).
453453
"""
454454
from reflex.event import EventHandler
455-
from reflex.minify import is_minify_enabled
455+
from reflex.minify import is_event_minify_enabled
456456
from reflex.state import State
457457

458458
# Cast for type checker - at runtime this is always an EventHandler
@@ -477,7 +477,7 @@ def get_event_handler_parts(
477477
# Check for event_id minification from minify.json
478478
# The state class stores its event ID mapping in _event_id_to_name
479479
# where key is minified_name and value is original_handler_name
480-
if is_minify_enabled():
480+
if is_event_minify_enabled():
481481
try:
482482
# Get the state class using the path
483483
state_cls = State.get_class_substate(state_full_name)

0 commit comments

Comments
 (0)