From 51aef9fd2246d9888f17d366fed314a944bfcc6a Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Thu, 25 Jul 2024 23:02:29 +0200 Subject: [PATCH] enable minified state names by default in prod --- reflex/constants/compiler.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/reflex/constants/compiler.py b/reflex/constants/compiler.py index 5a6240b94..ed175fda6 100644 --- a/reflex/constants/compiler.py +++ b/reflex/constants/compiler.py @@ -6,7 +6,7 @@ from enum import Enum from types import SimpleNamespace from reflex.base import Base -from reflex.constants import Dirs +from reflex.constants import ENV_MODE_ENV_VAR, Dirs, Env from reflex.utils.imports import ImportVar # The prefix used to create setters for state vars. @@ -15,6 +15,9 @@ SETTER_PREFIX = "set_" # The file used to specify no compilation. NOCOMPILE_FILE = "nocompile" +# The env var to toggle minification of states. +ENV_MINIFY_STATES = "REFLEX_MINIFY_STATES" + class Ext(SimpleNamespace): """Extension used in Reflex.""" @@ -31,6 +34,20 @@ class Ext(SimpleNamespace): EXE = ".exe" +def minify_states() -> bool: + """Whether to minify states. + + Returns: + True if states should be minified. + """ + env = os.environ.get(ENV_MINIFY_STATES, None) + if env is not None: + return env.lower() == "true" + + # minify states in prod by default + return os.environ.get(ENV_MODE_ENV_VAR, "") == Env.PROD.value + + class CompileVars(SimpleNamespace): """The variables used during compilation.""" @@ -62,10 +79,10 @@ class CompileVars(SimpleNamespace): CONNECT_ERROR = "connectErrors" # The name of the function for converting a dict to an event. TO_EVENT = "Event" - # The env var to toggle minification of states. - ENV_MINIFY_STATES = "REFLEX_MINIFY_STATES" + # Whether to minify states. - MINIFY_STATES = os.environ.get(ENV_MINIFY_STATES, False) + MINIFY_STATES = minify_states() + # The name of the OnLoadInternal state. ON_LOAD_INTERNAL_STATE = ( "l" if MINIFY_STATES else "reflex___state____on_load_internal_state"