compiler.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. """Compiler for the pynecone apps."""
  2. from __future__ import annotations
  3. from functools import wraps
  4. from typing import Callable, List, Set, Tuple, Type
  5. from pynecone import constants
  6. from pynecone.compiler import templates, utils
  7. from pynecone.components.component import Component, CustomComponent
  8. from pynecone.state import State
  9. from pynecone.style import Style
  10. from pynecone.utils import imports
  11. from pynecone.vars import ImportVar
  12. # Imports to be included in every Pynecone app.
  13. DEFAULT_IMPORTS: imports.ImportDict = {
  14. "react": {
  15. ImportVar(tag="Fragment"),
  16. ImportVar(tag="useEffect"),
  17. ImportVar(tag="useRef"),
  18. ImportVar(tag="useState"),
  19. },
  20. "next/router": {ImportVar(tag="useRouter")},
  21. f"/{constants.STATE_PATH}": {
  22. ImportVar(tag="connect"),
  23. ImportVar(tag="processEvent"),
  24. ImportVar(tag="uploadFiles"),
  25. ImportVar(tag="E"),
  26. ImportVar(tag="isTrue"),
  27. ImportVar(tag="preventDefault"),
  28. ImportVar(tag="refs"),
  29. ImportVar(tag="getRefValue"),
  30. },
  31. "": {ImportVar(tag="focus-visible/dist/focus-visible")},
  32. "@chakra-ui/react": {
  33. ImportVar(tag=constants.USE_COLOR_MODE),
  34. ImportVar(tag="Box"),
  35. ImportVar(tag="Text"),
  36. },
  37. }
  38. def _compile_document_root(root: Component) -> str:
  39. """Compile the document root.
  40. Args:
  41. root: The document root to compile.
  42. Returns:
  43. The compiled document root.
  44. """
  45. return templates.DOCUMENT_ROOT.render(
  46. imports=utils.compile_imports(root.get_imports()),
  47. document=root.render(),
  48. )
  49. def _compile_theme(theme: dict) -> str:
  50. """Compile the theme.
  51. Args:
  52. theme: The theme to compile.
  53. Returns:
  54. The compiled theme.
  55. """
  56. return templates.THEME.render(theme=theme)
  57. def _compile_page(
  58. component: Component, state: Type[State], connect_error_component
  59. ) -> str:
  60. """Compile the component given the app state.
  61. Args:
  62. component: The component to compile.
  63. state: The app state.
  64. connect_error_component: The component to render on sever connection error.
  65. Returns:
  66. The compiled component.
  67. """
  68. # Merge the default imports with the app-specific imports.
  69. imports = utils.merge_imports(DEFAULT_IMPORTS, component.get_imports())
  70. imports = utils.compile_imports(imports)
  71. # Compile the code to render the component.
  72. return templates.PAGE.render(
  73. imports=imports,
  74. custom_codes=component.get_custom_code(),
  75. initial_state=utils.compile_state(state),
  76. state_name=state.get_name(),
  77. hooks=component.get_hooks(),
  78. render=component.render(),
  79. transports=constants.Transports.POLLING_WEBSOCKET.get_transports(),
  80. err_comp=connect_error_component.render() if connect_error_component else None,
  81. )
  82. def _compile_components(components: Set[CustomComponent]) -> str:
  83. """Compile the components.
  84. Args:
  85. components: The components to compile.
  86. Returns:
  87. The compiled components.
  88. """
  89. imports = {
  90. "react": {ImportVar(tag="memo")},
  91. f"/{constants.STATE_PATH}": {ImportVar(tag="E"), ImportVar(tag="isTrue")},
  92. }
  93. component_renders = []
  94. # Compile each component.
  95. for component in components:
  96. component_render, component_imports = utils.compile_custom_component(component)
  97. component_renders.append(component_render)
  98. imports = utils.merge_imports(imports, component_imports)
  99. # Compile the components page.
  100. return templates.COMPONENTS.render(
  101. imports=utils.compile_imports(imports),
  102. components=component_renders,
  103. )
  104. def _compile_tailwind(
  105. config: dict,
  106. ) -> str:
  107. """Compile the Tailwind config.
  108. Args:
  109. config: The Tailwind config.
  110. Returns:
  111. The compiled Tailwind config.
  112. """
  113. return templates.TAILWIND_CONFIG.render(
  114. **config,
  115. )
  116. def write_output(fn: Callable[..., Tuple[str, str]]):
  117. """Write the output of the function to a file.
  118. Args:
  119. fn: The function to decorate.
  120. Returns:
  121. The decorated function.
  122. """
  123. @wraps(fn)
  124. def wrapper(*args, write: bool = True) -> Tuple[str, str]:
  125. """Write the output of the function to a file.
  126. Args:
  127. *args: The arguments to pass to the function.
  128. write: Whether to write the output to a file.
  129. Returns:
  130. The path and code of the output.
  131. """
  132. path, code = fn(*args)
  133. if write:
  134. utils.write_page(path, code)
  135. return path, code
  136. return wrapper
  137. @write_output
  138. def compile_document_root(stylesheets: List[str]) -> Tuple[str, str]:
  139. """Compile the document root.
  140. Args:
  141. stylesheets: The stylesheets to include in the document root.
  142. Returns:
  143. The path and code of the compiled document root.
  144. """
  145. # Get the path for the output file.
  146. output_path = utils.get_page_path(constants.DOCUMENT_ROOT)
  147. # Create the document root.
  148. document_root = utils.create_document_root(stylesheets)
  149. # Compile the document root.
  150. code = _compile_document_root(document_root)
  151. return output_path, code
  152. @write_output
  153. def compile_theme(style: Style) -> Tuple[str, str]:
  154. """Compile the theme.
  155. Args:
  156. style: The style to compile.
  157. Returns:
  158. The path and code of the compiled theme.
  159. """
  160. output_path = utils.get_theme_path()
  161. # Create the theme.
  162. theme = utils.create_theme(style)
  163. # Compile the theme.
  164. code = _compile_theme(theme)
  165. return output_path, code
  166. @write_output
  167. def compile_page(
  168. path: str,
  169. component: Component,
  170. state: Type[State],
  171. connect_error_component: Component,
  172. ) -> Tuple[str, str]:
  173. """Compile a single page.
  174. Args:
  175. path: The path to compile the page to.
  176. component: The component to compile.
  177. state: The app state.
  178. connect_error_component: The component to render on sever connection error.
  179. Returns:
  180. The path and code of the compiled page.
  181. """
  182. # Get the path for the output file.
  183. output_path = utils.get_page_path(path)
  184. # Add the style to the component.
  185. code = _compile_page(component, state, connect_error_component)
  186. return output_path, code
  187. @write_output
  188. def compile_components(components: Set[CustomComponent]):
  189. """Compile the custom components.
  190. Args:
  191. components: The custom components to compile.
  192. Returns:
  193. The path and code of the compiled components.
  194. """
  195. # Get the path for the output file.
  196. output_path = utils.get_components_path()
  197. # Compile the components.
  198. code = _compile_components(components)
  199. return output_path, code
  200. @write_output
  201. def compile_tailwind(
  202. config: dict,
  203. ):
  204. """Compile the Tailwind config.
  205. Args:
  206. config: The Tailwind config.
  207. Returns:
  208. The compiled Tailwind config.
  209. """
  210. # Get the path for the output file.
  211. output_path = constants.TAILWIND_CONFIG
  212. # Compile the config.
  213. code = _compile_tailwind(config)
  214. return output_path, code
  215. def purge_web_pages_dir():
  216. """Empty out .web directory."""
  217. template_files = ["_app.js", "404.js"]
  218. utils.empty_dir(constants.WEB_PAGES_DIR, keep_files=template_files)