compiler.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. """Compiler for the reflex apps."""
  2. from __future__ import annotations
  3. import os
  4. from pathlib import Path
  5. from typing import Dict, Iterable, Optional, Type, Union
  6. from reflex import constants
  7. from reflex.compiler import templates, utils
  8. from reflex.components.component import (
  9. BaseComponent,
  10. Component,
  11. ComponentStyle,
  12. CustomComponent,
  13. StatefulComponent,
  14. )
  15. from reflex.config import get_config
  16. from reflex.state import BaseState
  17. from reflex.style import LIGHT_COLOR_MODE
  18. from reflex.utils.exec import is_prod_mode
  19. from reflex.utils.imports import ImportVar
  20. from reflex.vars import Var
  21. def _compile_document_root(root: Component) -> str:
  22. """Compile the document root.
  23. Args:
  24. root: The document root to compile.
  25. Returns:
  26. The compiled document root.
  27. """
  28. return templates.DOCUMENT_ROOT.render(
  29. imports=utils.compile_imports(root.get_imports()),
  30. document=root.render(),
  31. )
  32. def _compile_app(app_root: Component) -> str:
  33. """Compile the app template component.
  34. Args:
  35. app_root: The app root to compile.
  36. Returns:
  37. The compiled app.
  38. """
  39. return templates.APP_ROOT.render(
  40. imports=utils.compile_imports(app_root.get_imports()),
  41. custom_codes=app_root.get_custom_code(),
  42. hooks=app_root.get_hooks(),
  43. render=app_root.render(),
  44. )
  45. def _compile_theme(theme: dict) -> str:
  46. """Compile the theme.
  47. Args:
  48. theme: The theme to compile.
  49. Returns:
  50. The compiled theme.
  51. """
  52. return templates.THEME.render(theme=theme)
  53. def _compile_contexts(state: Optional[Type[BaseState]], theme: Component) -> str:
  54. """Compile the initial state and contexts.
  55. Args:
  56. state: The app state.
  57. theme: The top-level app theme.
  58. Returns:
  59. The compiled context file.
  60. """
  61. appearance = getattr(theme, "appearance", None)
  62. if appearance is None:
  63. appearance = LIGHT_COLOR_MODE
  64. return (
  65. templates.CONTEXT.render(
  66. initial_state=utils.compile_state(state),
  67. state_name=state.get_name(),
  68. client_storage=utils.compile_client_storage(state),
  69. is_dev_mode=not is_prod_mode(),
  70. default_color_mode=appearance,
  71. )
  72. if state
  73. else templates.CONTEXT.render(
  74. is_dev_mode=not is_prod_mode(),
  75. default_color_mode=appearance,
  76. )
  77. )
  78. def _compile_page(
  79. component: Component,
  80. state: Type[BaseState],
  81. ) -> str:
  82. """Compile the component given the app state.
  83. Args:
  84. component: The component to compile.
  85. state: The app state.
  86. Returns:
  87. The compiled component.
  88. """
  89. imports = component.get_imports()
  90. imports = utils.compile_imports(imports)
  91. # Compile the code to render the component.
  92. kwargs = {"state_name": state.get_name()} if state else {}
  93. return templates.PAGE.render(
  94. imports=imports,
  95. dynamic_imports=component.get_dynamic_imports(),
  96. custom_codes=component.get_custom_code(),
  97. hooks=component.get_hooks(),
  98. render=component.render(),
  99. **kwargs,
  100. )
  101. def compile_root_stylesheet(stylesheets: list[str]) -> tuple[str, str]:
  102. """Compile the root stylesheet.
  103. Args:
  104. stylesheets: The stylesheets to include in the root stylesheet.
  105. Returns:
  106. The path and code of the compiled root stylesheet.
  107. """
  108. output_path = utils.get_root_stylesheet_path()
  109. code = _compile_root_stylesheet(stylesheets)
  110. return output_path, code
  111. def _compile_root_stylesheet(stylesheets: list[str]) -> str:
  112. """Compile the root stylesheet.
  113. Args:
  114. stylesheets: The stylesheets to include in the root stylesheet.
  115. Returns:
  116. The compiled root stylesheet.
  117. Raises:
  118. FileNotFoundError: If a specified stylesheet in assets directory does not exist.
  119. """
  120. # Add tailwind css if enabled.
  121. sheets = (
  122. [constants.Tailwind.ROOT_STYLE_PATH]
  123. if get_config().tailwind is not None
  124. else []
  125. )
  126. for stylesheet in stylesheets:
  127. if not utils.is_valid_url(stylesheet):
  128. # check if stylesheet provided exists.
  129. stylesheet_full_path = (
  130. Path.cwd() / constants.Dirs.APP_ASSETS / stylesheet.strip("/")
  131. )
  132. if not os.path.exists(stylesheet_full_path):
  133. raise FileNotFoundError(
  134. f"The stylesheet file {stylesheet_full_path} does not exist."
  135. )
  136. stylesheet = f"@/{stylesheet.strip('/')}"
  137. sheets.append(stylesheet) if stylesheet not in sheets else None
  138. return templates.STYLE.render(stylesheets=sheets)
  139. def _compile_component(component: Component) -> str:
  140. """Compile a single component.
  141. Args:
  142. component: The component to compile.
  143. Returns:
  144. The compiled component.
  145. """
  146. return templates.COMPONENT.render(component=component)
  147. def _compile_components(components: set[CustomComponent]) -> str:
  148. """Compile the components.
  149. Args:
  150. components: The components to compile.
  151. Returns:
  152. The compiled components.
  153. """
  154. imports = {
  155. "react": [ImportVar(tag="memo")],
  156. f"/{constants.Dirs.STATE_PATH}": [ImportVar(tag="E"), ImportVar(tag="isTrue")],
  157. }
  158. component_renders = []
  159. # Compile each component.
  160. for component in components:
  161. component_render, component_imports = utils.compile_custom_component(component)
  162. component_renders.append(component_render)
  163. imports = utils.merge_imports(imports, component_imports)
  164. # Compile the components page.
  165. return templates.COMPONENTS.render(
  166. imports=utils.compile_imports(imports),
  167. components=component_renders,
  168. )
  169. def _compile_stateful_components(
  170. page_components: list[BaseComponent],
  171. ) -> str:
  172. """Walk the page components and extract shared stateful components.
  173. Any StatefulComponent that is shared by more than one page will be rendered
  174. to a separate module and marked rendered_as_shared so subsequent
  175. renderings will import the component from the shared module instead of
  176. directly including the code for it.
  177. Args:
  178. page_components: The Components or StatefulComponents to compile.
  179. Returns:
  180. The rendered stateful components code.
  181. """
  182. all_import_dicts = []
  183. rendered_components = {}
  184. def get_shared_components_recursive(component: BaseComponent):
  185. """Get the shared components for a component and its children.
  186. A shared component is a StatefulComponent that appears in 2 or more
  187. pages and is a candidate for writing to a common file and importing
  188. into each page where it is used.
  189. Args:
  190. component: The component to collect shared StatefulComponents for.
  191. """
  192. for child in component.children:
  193. # Depth-first traversal.
  194. get_shared_components_recursive(child)
  195. # When the component is referenced by more than one page, render it
  196. # to be included in the STATEFUL_COMPONENTS module.
  197. # Skip this step in dev mode, thereby avoiding potential hot reload errors for larger apps
  198. if (
  199. isinstance(component, StatefulComponent)
  200. and component.references > 1
  201. and is_prod_mode()
  202. ):
  203. # Reset this flag to render the actual component.
  204. component.rendered_as_shared = False
  205. rendered_components.update(
  206. {code: None for code in component.get_custom_code()},
  207. )
  208. all_import_dicts.append(component.get_imports())
  209. # Indicate that this component now imports from the shared file.
  210. component.rendered_as_shared = True
  211. for page_component in page_components:
  212. get_shared_components_recursive(page_component)
  213. # Don't import from the file that we're about to create.
  214. all_imports = utils.merge_imports(*all_import_dicts)
  215. all_imports.pop(
  216. f"/{constants.Dirs.UTILS}/{constants.PageNames.STATEFUL_COMPONENTS}", None
  217. )
  218. return templates.STATEFUL_COMPONENTS.render(
  219. imports=utils.compile_imports(all_imports),
  220. memoized_code="\n".join(rendered_components),
  221. )
  222. def _compile_tailwind(
  223. config: dict,
  224. ) -> str:
  225. """Compile the Tailwind config.
  226. Args:
  227. config: The Tailwind config.
  228. Returns:
  229. The compiled Tailwind config.
  230. """
  231. return templates.TAILWIND_CONFIG.render(
  232. **config,
  233. )
  234. def compile_document_root(
  235. head_components: list[Component],
  236. html_lang: Optional[str] = None,
  237. html_custom_attrs: Optional[Dict[str, Union[Var, str]]] = None,
  238. ) -> tuple[str, str]:
  239. """Compile the document root.
  240. Args:
  241. head_components: The components to include in the head.
  242. html_lang: The language of the document, will be added to the html root element.
  243. html_custom_attrs: custom attributes added to the html root element.
  244. Returns:
  245. The path and code of the compiled document root.
  246. """
  247. # Get the path for the output file.
  248. output_path = utils.get_page_path(constants.PageNames.DOCUMENT_ROOT)
  249. # Create the document root.
  250. document_root = utils.create_document_root(
  251. head_components, html_lang=html_lang, html_custom_attrs=html_custom_attrs
  252. )
  253. # Compile the document root.
  254. code = _compile_document_root(document_root)
  255. return output_path, code
  256. def compile_app(app_root: Component) -> tuple[str, str]:
  257. """Compile the app root.
  258. Args:
  259. app_root: The app root component to compile.
  260. Returns:
  261. The path and code of the compiled app wrapper.
  262. """
  263. # Get the path for the output file.
  264. output_path = utils.get_page_path(constants.PageNames.APP_ROOT)
  265. # Compile the document root.
  266. code = _compile_app(app_root)
  267. return output_path, code
  268. def compile_theme(style: ComponentStyle) -> tuple[str, str]:
  269. """Compile the theme.
  270. Args:
  271. style: The style to compile.
  272. Returns:
  273. The path and code of the compiled theme.
  274. """
  275. output_path = utils.get_theme_path()
  276. # Create the theme.
  277. theme = utils.create_theme(style)
  278. # Compile the theme.
  279. code = _compile_theme(theme)
  280. return output_path, code
  281. def compile_contexts(
  282. state: Optional[Type[BaseState]],
  283. theme: Component,
  284. ) -> tuple[str, str]:
  285. """Compile the initial state / context.
  286. Args:
  287. state: The app state.
  288. theme: The top-level app theme.
  289. Returns:
  290. The path and code of the compiled context.
  291. """
  292. # Get the path for the output file.
  293. output_path = utils.get_context_path()
  294. return output_path, _compile_contexts(state, theme)
  295. def compile_page(
  296. path: str, component: Component, state: Type[BaseState]
  297. ) -> tuple[str, str]:
  298. """Compile a single page.
  299. Args:
  300. path: The path to compile the page to.
  301. component: The component to compile.
  302. state: The app state.
  303. Returns:
  304. The path and code of the compiled page.
  305. """
  306. # Get the path for the output file.
  307. output_path = utils.get_page_path(path)
  308. # Add the style to the component.
  309. code = _compile_page(component, state)
  310. return output_path, code
  311. def compile_components(components: set[CustomComponent]):
  312. """Compile the custom components.
  313. Args:
  314. components: The custom components to compile.
  315. Returns:
  316. The path and code of the compiled components.
  317. """
  318. # Get the path for the output file.
  319. output_path = utils.get_components_path()
  320. # Compile the components.
  321. code = _compile_components(components)
  322. return output_path, code
  323. def compile_stateful_components(
  324. pages: Iterable[Component],
  325. ) -> tuple[str, str, list[BaseComponent]]:
  326. """Separately compile components that depend on State vars.
  327. StatefulComponents are compiled as their own component functions with their own
  328. useContext declarations, which allows page components to be stateless and avoid
  329. re-rendering along with parts of the page that actually depend on state.
  330. Args:
  331. pages: The pages to extract stateful components from.
  332. Returns:
  333. The path and code of the compiled stateful components.
  334. """
  335. output_path = utils.get_stateful_components_path()
  336. # Compile the stateful components.
  337. page_components = [StatefulComponent.compile_from(page) or page for page in pages]
  338. code = _compile_stateful_components(page_components)
  339. return output_path, code, page_components
  340. def compile_tailwind(
  341. config: dict,
  342. ):
  343. """Compile the Tailwind config.
  344. Args:
  345. config: The Tailwind config.
  346. Returns:
  347. The compiled Tailwind config.
  348. """
  349. # Get the path for the output file.
  350. output_path = constants.Tailwind.CONFIG
  351. # Compile the config.
  352. code = _compile_tailwind(config)
  353. return output_path, code
  354. def remove_tailwind_from_postcss() -> tuple[str, str]:
  355. """If tailwind is not to be used, remove it from postcss.config.js.
  356. Returns:
  357. The path and code of the compiled postcss.config.js.
  358. """
  359. # Get the path for the output file.
  360. output_path = constants.Dirs.POSTCSS_JS
  361. code = [
  362. line
  363. for line in Path(output_path).read_text().splitlines(keepends=True)
  364. if "tailwindcss: " not in line
  365. ]
  366. # Compile the config.
  367. return output_path, "".join(code)
  368. def purge_web_pages_dir():
  369. """Empty out .web/pages directory."""
  370. if not is_prod_mode() and os.environ.get("REFLEX_PERSIST_WEB_DIR"):
  371. # Skip purging the web directory in dev mode if REFLEX_PERSIST_WEB_DIR is set.
  372. return
  373. # Empty out the web pages directory.
  374. utils.empty_dir(constants.Dirs.WEB_PAGES, keep_files=["_app.js"])