3333from reflex .utils .exceptions import ReflexError
3434from reflex .utils .exec import is_prod_mode
3535from reflex .utils .format import to_title_case
36- from reflex .utils .imports import ImportVar
36+ from reflex .utils .imports import ImportVar , ParsedImportDict
3737from reflex .utils .prerequisites import get_web_dir
3838from reflex .vars .base import LiteralVar , Var
3939
@@ -411,6 +411,47 @@ def _compile_memo_components(
411411 )
412412
413413
414+ def _get_shared_components_recursive (
415+ component : BaseComponent ,
416+ rendered_components : dict [str , None ],
417+ all_import_dicts : list [ParsedImportDict ],
418+ ):
419+ """Get the shared components for a component and its children.
420+
421+ A shared component is a StatefulComponent that appears in 2 or more
422+ pages and is a candidate for writing to a common file and importing
423+ into each page where it is used.
424+
425+ Args:
426+ component: The component to collect shared StatefulComponents for.
427+ rendered_components: A dict to store the rendered shared components in.
428+ all_import_dicts: A list to store the imports of all shared components in.
429+ """
430+ for child in component .children :
431+ # Depth-first traversal.
432+ _get_shared_components_recursive (child , rendered_components , all_import_dicts )
433+
434+ # When the component is referenced by more than one page, render it
435+ # to be included in the STATEFUL_COMPONENTS module.
436+ # Skip this step in dev mode, thereby avoiding potential hot reload errors for larger apps
437+ if isinstance (component , StatefulComponent ) and component .references > 1 :
438+ # Reset this flag to render the actual component.
439+ component .rendered_as_shared = False
440+
441+ # Include dynamic imports in the shared component.
442+ if dynamic_imports := component ._get_all_dynamic_imports ():
443+ rendered_components .update (dict .fromkeys (dynamic_imports ))
444+
445+ # Include custom code in the shared component.
446+ rendered_components .update (component ._get_all_custom_code (export = True ))
447+
448+ # Include all imports in the shared component.
449+ all_import_dicts .append (component ._get_all_imports ())
450+
451+ # Indicate that this component now imports from the shared file.
452+ component .rendered_as_shared = True
453+
454+
414455def _compile_stateful_components (
415456 page_components : list [BaseComponent ],
416457) -> str :
@@ -430,46 +471,10 @@ def _compile_stateful_components(
430471 all_import_dicts = []
431472 rendered_components = {}
432473
433- def get_shared_components_recursive (component : BaseComponent ):
434- """Get the shared components for a component and its children.
435-
436- A shared component is a StatefulComponent that appears in 2 or more
437- pages and is a candidate for writing to a common file and importing
438- into each page where it is used.
439-
440- Args:
441- component: The component to collect shared StatefulComponents for.
442- """
443- for child in component .children :
444- # Depth-first traversal.
445- get_shared_components_recursive (child )
446-
447- # When the component is referenced by more than one page, render it
448- # to be included in the STATEFUL_COMPONENTS module.
449- # Skip this step in dev mode, thereby avoiding potential hot reload errors for larger apps
450- if (
451- isinstance (component , StatefulComponent )
452- and component .references > 1
453- and is_prod_mode ()
454- ):
455- # Reset this flag to render the actual component.
456- component .rendered_as_shared = False
457-
458- # Include dynamic imports in the shared component.
459- if dynamic_imports := component ._get_all_dynamic_imports ():
460- rendered_components .update (dict .fromkeys (dynamic_imports ))
461-
462- # Include custom code in the shared component.
463- rendered_components .update (component ._get_all_custom_code (export = True ))
464-
465- # Include all imports in the shared component.
466- all_import_dicts .append (component ._get_all_imports ())
467-
468- # Indicate that this component now imports from the shared file.
469- component .rendered_as_shared = True
470-
471474 for page_component in page_components :
472- get_shared_components_recursive (page_component )
475+ _get_shared_components_recursive (
476+ page_component , rendered_components , all_import_dicts
477+ )
473478
474479 # Don't import from the file that we're about to create.
475480 all_imports = utils .merge_imports (* all_import_dicts )
@@ -637,7 +642,7 @@ def compile_stateful_components(
637642 progress_function ()
638643 page_components .append (page_component )
639644
640- code = _compile_stateful_components (page_components )
645+ code = _compile_stateful_components (page_components ) if is_prod_mode () else ""
641646 return output_path , code , page_components
642647
643648
0 commit comments