Skip to content

Commit 5578f62

Browse files
authored
Split prerequisites into smaller files (#5690)
* split prerequisites into smaller files * set bun path * dont install bun if REFLEX_USE_NPM is set
1 parent 4546bc9 commit 5578f62

File tree

16 files changed

+1463
-1465
lines changed

16 files changed

+1463
-1465
lines changed

reflex/app.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
console,
110110
exceptions,
111111
format,
112+
frontend_skeleton,
113+
js_runtimes,
112114
path_ops,
113115
prerequisites,
114116
types,
@@ -598,10 +600,6 @@ def __call__(self) -> ASGIApp:
598600
"""
599601
from reflex.vars.base import GLOBAL_CACHE
600602

601-
# For py3.9 compatibility when redis is used, we MUST add any decorator pages
602-
# before compiling the app in a thread to avoid event loop error (REF-2172).
603-
self._apply_decorated_pages()
604-
605603
self._compile(prerender_routes=is_prod_mode())
606604

607605
config = get_config()
@@ -992,7 +990,7 @@ def _get_frontend_packages(self, imports: dict[str, set[ImportVar]]):
992990
continue
993991
_frontend_packages.append(package)
994992
page_imports.update(_frontend_packages)
995-
prerequisites.install_frontend_packages(page_imports, get_config())
993+
js_runtimes.install_frontend_packages(page_imports, get_config())
996994

997995
def _app_root(self, app_wrappers: dict[tuple[int, str], Component]) -> Component:
998996
for component in tuple(app_wrappers.values()):
@@ -1065,16 +1063,8 @@ def memoized_badge():
10651063
self.app_wraps[(0, "StickyBadge")] = lambda _: memoized_badge()
10661064

10671065
def _apply_decorated_pages(self):
1068-
"""Add @rx.page decorated pages to the app.
1069-
1070-
This has to be done in the MainThread for py38 and py39 compatibility, so the
1071-
decorated pages are added to the app before the app is compiled (in a thread)
1072-
to workaround REF-2172.
1073-
1074-
This can move back into `compile_` when py39 support is dropped.
1075-
"""
1066+
"""Add @rx.page decorated pages to the app."""
10761067
app_name = get_config().app_name
1077-
# Add the @rx.page decorated pages to collect on_load events.
10781068
for render, kwargs in DECORATED_PAGES[app_name]:
10791069
self.add_page(render, **kwargs)
10801070

@@ -1124,6 +1114,8 @@ def _compile(self, prerender_routes: bool = False, dry_run: bool = False):
11241114
"""
11251115
from reflex.utils.exceptions import ReflexRuntimeError
11261116

1117+
self._apply_decorated_pages()
1118+
11271119
self._pages = {}
11281120

11291121
def get_compilation_time() -> str:
@@ -1440,7 +1432,7 @@ def _submit_work_without_advancing(
14401432
self._get_frontend_packages(all_imports)
14411433

14421434
# Setup the react-router.config.js
1443-
prerequisites.update_react_router_config(
1435+
frontend_skeleton.update_react_router_config(
14441436
prerender_routes=prerender_routes,
14451437
)
14461438

reflex/custom_components/custom_components.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from reflex import constants
1717
from reflex.constants import CustomComponents
18-
from reflex.utils import console
18+
from reflex.utils import console, frontend_skeleton
1919

2020

2121
def set_loglevel(ctx: Any, self: Any, value: str | None):
@@ -328,7 +328,7 @@ def init(
328328
Raises:
329329
Exit: If the pyproject.toml already exists.
330330
"""
331-
from reflex.utils import exec, prerequisites
331+
from reflex.utils import exec
332332

333333
if CustomComponents.PYPROJECT_TOML.exists():
334334
console.error(f"A {CustomComponents.PYPROJECT_TOML} already exists. Aborting.")
@@ -347,7 +347,7 @@ def init(
347347
_populate_demo_app(name_variants)
348348

349349
# Initialize the .gitignore.
350-
prerequisites.initialize_gitignore(
350+
frontend_skeleton.initialize_gitignore(
351351
gitignore_file=CustomComponents.FILE, files_to_ignore=CustomComponents.DEFAULTS
352352
)
353353

reflex/reflex.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def _init(
5858
ai: bool = False,
5959
):
6060
"""Initialize a new Reflex app in the given directory."""
61-
from reflex.utils import exec, prerequisites
61+
from reflex.utils import exec, frontend_skeleton, prerequisites, templates
6262

6363
# Show system info
6464
exec.output_system_info()
@@ -80,13 +80,13 @@ def _init(
8080
prerequisites.initialize_frontend_dependencies()
8181

8282
# Initialize the app.
83-
template = prerequisites.initialize_app(app_name, template)
83+
template = templates.initialize_app(app_name, template)
8484

8585
# Initialize the .gitignore.
86-
prerequisites.initialize_gitignore()
86+
frontend_skeleton.initialize_gitignore()
8787

8888
# Initialize the requirements.txt.
89-
needs_user_manual_update = prerequisites.initialize_requirements_txt()
89+
needs_user_manual_update = frontend_skeleton.initialize_requirements_txt()
9090

9191
template_msg = f" using the {template} template" if template else ""
9292
# Finish initializing the app.
@@ -371,7 +371,7 @@ def compile(dry: bool):
371371
_init(name=get_config().app_name)
372372
get_config(reload=True)
373373
starting_time = time.monotonic()
374-
prerequisites.compile_app(dry_run=dry)
374+
prerequisites.get_compiled_app(dry_run=dry)
375375
elapsed_time = time.monotonic() - starting_time
376376
console.success(f"App compiled successfully in {elapsed_time:.3f} seconds.")
377377

@@ -737,9 +737,10 @@ def deploy(
737737
def rename(new_name: str):
738738
"""Rename the app in the current directory."""
739739
from reflex.utils import prerequisites
740+
from reflex.utils.rename import rename_app
740741

741742
prerequisites.validate_app_name(new_name)
742-
prerequisites.rename_app(new_name, get_config().loglevel)
743+
rename_app(new_name, get_config().loglevel)
743744

744745

745746
if TYPE_CHECKING:

reflex/testing.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
StateManagerRedis,
4646
reload_state_module,
4747
)
48-
from reflex.utils import console
48+
from reflex.utils import console, js_runtimes
4949
from reflex.utils.export import export
5050
from reflex.utils.types import ASGIApp
5151

@@ -403,9 +403,7 @@ def _start_frontend(self):
403403
# Start the frontend.
404404
self.frontend_process = reflex.utils.processes.new_process(
405405
[
406-
*reflex.utils.prerequisites.get_js_package_executor(raise_on_none=True)[
407-
0
408-
],
406+
*js_runtimes.get_js_package_executor(raise_on_none=True)[0],
409407
"run",
410408
"dev",
411409
],

reflex/utils/build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from rich.progress import MofNCompleteColumn, Progress, TimeElapsedColumn
1010

1111
from reflex import constants
12-
from reflex.utils import console, path_ops, prerequisites, processes
12+
from reflex.utils import console, js_runtimes, path_ops, prerequisites, processes
1313
from reflex.utils.exec import is_in_app_harness
1414

1515

@@ -187,7 +187,7 @@ def build():
187187
# Start the subprocess with the progress bar.
188188
process = processes.new_process(
189189
[
190-
*prerequisites.get_js_package_executor(raise_on_none=True)[0],
190+
*js_runtimes.get_js_package_executor(raise_on_none=True)[0],
191191
"run",
192192
"export",
193193
],

reflex/utils/exec.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,17 @@ def run_frontend(root: Path, port: str, backend_present: bool = True):
231231
port: The port to run the frontend on.
232232
backend_present: Whether the backend is present.
233233
"""
234-
from reflex.utils import prerequisites
234+
from reflex.utils import js_runtimes
235235

236236
# validate dependencies before run
237-
prerequisites.validate_frontend_dependencies(init=False)
237+
js_runtimes.validate_frontend_dependencies(init=False)
238238

239239
# Run the frontend in development mode.
240240
console.rule("[bold green]App Running")
241241
os.environ["PORT"] = str(get_config().frontend_port if port is None else port)
242242
run_process_and_launch_url(
243243
[
244-
*prerequisites.get_js_package_executor(raise_on_none=True)[0],
244+
*js_runtimes.get_js_package_executor(raise_on_none=True)[0],
245245
"run",
246246
"dev",
247247
],
@@ -257,16 +257,16 @@ def run_frontend_prod(root: Path, port: str, backend_present: bool = True):
257257
port: The port to run the frontend on.
258258
backend_present: Whether the backend is present.
259259
"""
260-
from reflex.utils import prerequisites
260+
from reflex.utils import js_runtimes
261261

262262
# Set the port.
263263
os.environ["PORT"] = str(get_config().frontend_port if port is None else port)
264264
# validate dependencies before run
265-
prerequisites.validate_frontend_dependencies(init=False)
265+
js_runtimes.validate_frontend_dependencies(init=False)
266266
# Run the frontend in production mode.
267267
console.rule("[bold green]App Running")
268268
run_process_and_launch_url(
269-
[*prerequisites.get_js_package_executor(raise_on_none=True)[0], "run", "prod"],
269+
[*js_runtimes.get_js_package_executor(raise_on_none=True)[0], "run", "prod"],
270270
backend_present,
271271
)
272272

@@ -670,7 +670,7 @@ def output_system_info():
670670
if console._LOG_LEVEL > constants.LogLevel.DEBUG:
671671
return
672672

673-
from reflex.utils import prerequisites
673+
from reflex.utils import js_runtimes
674674

675675
config = get_config()
676676
try:
@@ -684,13 +684,13 @@ def output_system_info():
684684

685685
dependencies = [
686686
f"[Reflex {constants.Reflex.VERSION} with Python {platform.python_version()} (PATH: {sys.executable})]",
687-
f"[Node {prerequisites.get_node_version()} (Minimum: {constants.Node.MIN_VERSION}) (PATH:{path_ops.get_node_path()})]",
687+
f"[Node {js_runtimes.get_node_version()} (Minimum: {constants.Node.MIN_VERSION}) (PATH:{path_ops.get_node_path()})]",
688688
]
689689

690690
system = platform.system()
691691

692692
dependencies.append(
693-
f"[Bun {prerequisites.get_bun_version()} (Minimum: {constants.Bun.MIN_VERSION}) (PATH: {path_ops.get_bun_path()})]"
693+
f"[Bun {js_runtimes.get_bun_version()} (Minimum: {constants.Bun.MIN_VERSION}) (PATH: {path_ops.get_bun_path()})]"
694694
)
695695

696696
if system == "Linux":
@@ -704,10 +704,10 @@ def output_system_info():
704704
console.debug(f"{dep}")
705705

706706
console.debug(
707-
f"Using package installer at: {prerequisites.get_nodejs_compatible_package_managers(raise_on_none=False)}"
707+
f"Using package installer at: {js_runtimes.get_nodejs_compatible_package_managers(raise_on_none=False)}"
708708
)
709709
console.debug(
710-
f"Using package executer at: {prerequisites.get_js_package_executor(raise_on_none=False)}"
710+
f"Using package executer at: {js_runtimes.get_js_package_executor(raise_on_none=False)}"
711711
)
712712
if system != "Windows":
713713
console.debug(f"Unzip path: {path_ops.which('unzip')}")

0 commit comments

Comments
 (0)