Repository navigation
Description
Summary
Replace the current compilation orchestration in App._compile() with the new CompileContext-based plugin pipeline. This is the integration point where the new compiler architecture actually takes over from the old one. Also moves compilation logic out of reflex/app.py into the compiler module.
Background
App._compile() in reflex/app.py (~lines 1151-1549) is currently a ~400-line method that orchestrates the entire compilation process. It:
- Evaluates unevaluated pages via
compile_unevaluated_page() - Applies styles via
_add_style_recursive() - Collects imports via
_get_all_imports()on each page - Collects app wrap components via
_get_all_app_wrap_components() - Runs
StatefulComponent.compile_from()on each page (auto-memoization) - Compiles custom components via
compile_memo_components() - Compiles shared stateful components via
_compile_stateful_components() - Submits page compilation tasks to a thread/process pool executor
- Compiles document root, app root, contexts, theme, stylesheets
- Writes output files
The goal is to replace steps 1-7 with the CompileContext.compile() method and the registered plugins, and to move the remaining orchestration out of App._compile() into a standalone compile_app() function in the compiler module.
What Changes
Move compilation out of App
The App object should be part of the CompileContext (it holds config, style, pages, etc.), but the compilation logic itself should live in reflex/compiler/. This keeps App focused on being the runtime application object.
# New entry point in reflex/compiler/compiler.py (or a new orchestrator module)
async def compile_app(app: App, *, prerender_routes: bool = False, dry_run: bool = False):
"""Compile the entire application using the plugin pipeline."""
compile_ctx = CompileContext(
app=app,
pages=[...],
hooks=CompilerHooks(plugins=(...)),
)
async with compile_ctx:
await compile_ctx.compile()
# ... write output filesReplace tree walks with plugin results
Instead of:
for component in self._pages.values():
all_imports.update(component._get_all_imports()) # walk
app_wrappers.update(component._get_all_app_wrap_components()) # walkThe plugins have already collected this data into PageContext during the single walk:
for page_ctx in compile_ctx.compiled_pages.values():
all_imports.update(merge_imports(*page_ctx.imports))
app_wrappers.update(page_ctx.app_wrap_components)Remove ExecutorSafeFunctions and executor-based parallelism
The current code uses concurrent.futures with thread/process pool executors. As noted in the project brief, the compiler is CPU-bound and doesn't benefit from Python threads (GIL) or multiprocessing (overhead). Remove:
ExecutorSafeFunctionsclass inreflex/compiler/compiler.pyExecutorType.get_executor_from_environment()usage inApp._compile()- The
REFLEX_COMPILE_EXECUTORenvironment variable / config
Replace with straightforward sequential (or later, async) compilation.
Page template rendering
The final step of rendering each page to JS via templates.page_template() should also become a plugin or a step in CompileContext.compile() that uses the accumulated PageContext data:
code = templates.page_template(
imports=utils.compile_imports(page_ctx.imports[0]),
dynamic_imports=sorted(page_ctx.dynamic_imports),
custom_codes=page_ctx.module_code,
hooks=page_ctx.hooks,
render=page_ctx.root_component.render(),
)Acceptance Criteria
-
App._compile()is replaced by a new top-level compilation function in the compiler module (or significantly simplified to just call it) - The new function uses
CompileContextand the default plugin set to compile pages -
ExecutorSafeFunctionsclass is removed - Thread/process pool executor usage for page compilation is removed
- All existing tests pass (the output should be functionally identical)
- Benchmark: compile a non-trivial app (e.g., the reflex-web docs site) with both old and new paths to verify no performance regression
-
App._compile()no longer directly calls any_get_all_*methods on components
Key Files
reflex/app.py—App._compile()(the main target)reflex/compiler/compiler.py—ExecutorSafeFunctions,compile_page(),compile_stateful_components()reflex/compiler/utils.py—write_file(), output path helpersreflex/environment.py—ExecutorTypeenum (to be removed)
Migration Strategy
To reduce risk, consider a phased approach:
- First, add the new compilation path behind a feature flag / environment variable
- Run both old and new paths in CI, comparing outputs
- Once validated, remove the old path
Notes
- The
_compile()method also handles non-compilation concerns (asset copying, frontend package installation, pluginpre_compilehooks, react-router config). These should remain but be clearly separated from the component compilation pipeline. - The current progress bar tracking (
rich.progress) should be preserved — the new pipeline should report progress as pages are compiled. - The longer-term vision is for
CompileContextto be adaptable to different targets (React Native, Vue, etc.), so the page template rendering step should be pluggable too.
Activity
linear commentedon Mar 25, 2026
ENG-9144
Add compiler plugin hooks and plugins and move compilation pipeline o…