Skip to content

Commit 6b043d6

Browse files
adhami3310masenf
andauthored
expose exp hmr and sourcemap (#5906)
* enable exp hmr and expose sourcemap * disable by default --------- Co-authored-by: Masen Furer <[email protected]>
1 parent 0f8c19d commit 6b043d6

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

reflex/compiler/templates.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import json
66
from collections.abc import Iterable, Mapping
7-
from typing import TYPE_CHECKING, Any
7+
from typing import TYPE_CHECKING, Any, Literal
88

99
from reflex import constants
1010
from reflex.constants import Hooks
@@ -492,13 +492,21 @@ def package_json_template(
492492
})
493493

494494

495-
def vite_config_template(base: str, hmr: bool, force_full_reload: bool):
495+
def vite_config_template(
496+
base: str,
497+
hmr: bool,
498+
force_full_reload: bool,
499+
experimental_hmr: bool,
500+
sourcemap: bool | Literal["inline", "hidden"],
501+
):
496502
"""Template for vite.config.js.
497503
498504
Args:
499505
base: The base path for the Vite config.
500506
hmr: Whether to enable hot module replacement.
501507
force_full_reload: Whether to force a full reload on changes.
508+
experimental_hmr: Whether to enable experimental HMR features.
509+
sourcemap: The sourcemap configuration.
502510
503511
Returns:
504512
Rendered vite.config.js content as string.
@@ -550,6 +558,7 @@ def vite_config_template(base: str, hmr: bool, force_full_reload: bool):
550558
].concat({"[fullReload()]" if force_full_reload else "[]"}),
551559
build: {{
552560
assetsDir: "{base}assets".slice(1),
561+
sourcemap: {"true" if sourcemap is True else "false" if sourcemap is False else repr(sourcemap)},
553562
rollupOptions: {{
554563
onwarn(warning, warn) {{
555564
if (warning.code === "EVAL" && warning.id && warning.id.endsWith("state.js")) return;
@@ -570,6 +579,7 @@ def vite_config_template(base: str, hmr: bool, force_full_reload: bool):
570579
}},
571580
experimental: {{
572581
enableNativePlugin: false,
582+
hmr: {"true" if experimental_hmr else "false"},
573583
}},
574584
server: {{
575585
port: process.env.PORT,

reflex/environment.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Annotated,
1818
Any,
1919
Generic,
20+
Literal,
2021
TypeVar,
2122
get_args,
2223
get_origin,
@@ -256,6 +257,27 @@ def interpret_env_var_value(
256257
return interpret_existing_path_env(value, field_name)
257258
if field_type is Plugin:
258259
return interpret_plugin_env(value, field_name)
260+
if get_origin(field_type) is Literal:
261+
literal_values = get_args(field_type)
262+
for literal_value in literal_values:
263+
if isinstance(literal_value, str) and literal_value == value:
264+
return literal_value
265+
if isinstance(literal_value, bool):
266+
try:
267+
interpreted_bool = interpret_boolean_env(value, field_name)
268+
if interpreted_bool == literal_value:
269+
return interpreted_bool
270+
except EnvironmentVarValueError:
271+
continue
272+
if isinstance(literal_value, int):
273+
try:
274+
interpreted_int = interpret_int_env(value, field_name)
275+
if interpreted_int == literal_value:
276+
return interpreted_int
277+
except EnvironmentVarValueError:
278+
continue
279+
msg = f"Invalid literal value: {value!r} for {field_name}, expected one of {literal_values}"
280+
raise EnvironmentVarValueError(msg)
259281
if get_origin(field_type) in (list, Sequence):
260282
return [
261283
interpret_env_var_value(
@@ -687,6 +709,12 @@ class EnvironmentVariables:
687709
# Whether to force a full reload on changes.
688710
VITE_FORCE_FULL_RELOAD: EnvVar[bool] = env_var(False)
689711

712+
# Whether to enable Rolldown's experimental HMR.
713+
VITE_EXPERIMENTAL_HMR: EnvVar[bool] = env_var(False)
714+
715+
# Whether to generate sourcemaps for the frontend.
716+
VITE_SOURCEMAP: EnvVar[Literal[False, True, "inline", "hidden"]] = env_var(False) # noqa: RUF038
717+
690718
# Whether to enable SSR for the frontend.
691719
REFLEX_SSR: EnvVar[bool] = env_var(True)
692720

reflex/utils/frontend_skeleton.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ def _compile_vite_config(config: Config):
195195
base=base,
196196
hmr=environment.VITE_HMR.get(),
197197
force_full_reload=environment.VITE_FORCE_FULL_RELOAD.get(),
198+
experimental_hmr=environment.VITE_EXPERIMENTAL_HMR.get(),
199+
sourcemap=environment.VITE_SOURCEMAP.get(),
198200
)
199201

200202

0 commit comments

Comments
 (0)