|
7 | 7 | from pathlib import Path |
8 | 8 | from typing import Any, NewType, overload |
9 | 9 |
|
| 10 | +from reactpy._warnings import warn |
10 | 11 | from reactpy.config import REACTPY_DEBUG, REACTPY_WEB_MODULES_DIR |
11 | 12 | from reactpy.core.vdom import Vdom |
12 | 13 | from reactpy.types import ImportSourceDict, VdomConstructor |
|
27 | 28 | """A source loaded from a URL, usually a CDN""" |
28 | 29 |
|
29 | 30 |
|
| 31 | +_URL_WEB_MODULE_CACHE: dict[str, WebModule] = {} |
| 32 | +_FILE_WEB_MODULE_CACHE: dict[str, WebModule] = {} |
| 33 | +_STRING_WEB_MODULE_CACHE: dict[str, WebModule] = {} |
| 34 | + |
| 35 | + |
| 36 | +def import_js_from_url( |
| 37 | + url: str, |
| 38 | + export_names: str | list[str] | tuple[str, ...], |
| 39 | + fallback: Any | None = None, |
| 40 | + resolve_exports: bool | None = None, |
| 41 | + resolve_exports_depth: int = 5, |
| 42 | + unmount_before_update: bool = False, |
| 43 | + allow_children: bool = True, |
| 44 | +) -> VdomConstructor | list[VdomConstructor]: |
| 45 | + """Import a component from a URL. |
| 46 | +
|
| 47 | + Parameters: |
| 48 | + url: |
| 49 | + The URL to import the component from. |
| 50 | + export_names: |
| 51 | + One or more names to export. If given as a string, a single component |
| 52 | + will be returned. If a list is given, then a list of components will be |
| 53 | + returned. |
| 54 | + fallback: |
| 55 | + What to temporarily display while the module is being loaded. |
| 56 | + resolve_exports: |
| 57 | + Whether to try and find all the named exports of this module. |
| 58 | + resolve_exports_depth: |
| 59 | + How deeply to search for those exports. |
| 60 | + unmount_before_update: |
| 61 | + Cause the component to be unmounted before each update. This option should |
| 62 | + only be used if the imported package fails to re-render when props change. |
| 63 | + Using this option has negative performance consequences since all DOM |
| 64 | + elements must be changed on each render. See :issue:`461` for more info. |
| 65 | + allow_children: |
| 66 | + Whether or not these components can have children. |
| 67 | + """ |
| 68 | + if url in _URL_WEB_MODULE_CACHE: |
| 69 | + module = _URL_WEB_MODULE_CACHE[url] |
| 70 | + else: |
| 71 | + module = _module_from_url( |
| 72 | + url, |
| 73 | + fallback=fallback, |
| 74 | + resolve_exports=resolve_exports, |
| 75 | + resolve_exports_depth=resolve_exports_depth, |
| 76 | + unmount_before_update=unmount_before_update, |
| 77 | + ) |
| 78 | + _URL_WEB_MODULE_CACHE[url] = module |
| 79 | + return _vdom_from_web_module(module, export_names, fallback, allow_children) |
| 80 | + |
| 81 | + |
| 82 | +def import_js_from_file( |
| 83 | + name: str, |
| 84 | + file: str | Path, |
| 85 | + export_names: str | list[str] | tuple[str, ...], |
| 86 | + fallback: Any | None = None, |
| 87 | + resolve_exports: bool | None = None, |
| 88 | + resolve_exports_depth: int = 5, |
| 89 | + unmount_before_update: bool = False, |
| 90 | + symlink: bool = False, |
| 91 | + allow_children: bool = True, |
| 92 | +) -> VdomConstructor | list[VdomConstructor]: |
| 93 | + """Import a component from a file. |
| 94 | +
|
| 95 | + Parameters: |
| 96 | + name: |
| 97 | + The name of the package |
| 98 | + file: |
| 99 | + The file from which the content of the web module will be created. |
| 100 | + export_names: |
| 101 | + One or more names to export. If given as a string, a single component |
| 102 | + will be returned. If a list is given, then a list of components will be |
| 103 | + returned. |
| 104 | + fallback: |
| 105 | + What to temporarily display while the module is being loaded. |
| 106 | + resolve_exports: |
| 107 | + Whether to try and find all the named exports of this module. |
| 108 | + resolve_exports_depth: |
| 109 | + How deeply to search for those exports. |
| 110 | + unmount_before_update: |
| 111 | + Cause the component to be unmounted before each update. This option should |
| 112 | + only be used if the imported package fails to re-render when props change. |
| 113 | + Using this option has negative performance consequences since all DOM |
| 114 | + elements must be changed on each render. See :issue:`461` for more info. |
| 115 | + symlink: |
| 116 | + Whether the web module should be saved as a symlink to the given ``file``. |
| 117 | + allow_children: |
| 118 | + Whether or not these components can have children. |
| 119 | + """ |
| 120 | + if name in _FILE_WEB_MODULE_CACHE: |
| 121 | + module = _FILE_WEB_MODULE_CACHE[name] |
| 122 | + else: |
| 123 | + module = _module_from_file( |
| 124 | + name, |
| 125 | + file, |
| 126 | + fallback=fallback, |
| 127 | + resolve_exports=resolve_exports, |
| 128 | + resolve_exports_depth=resolve_exports_depth, |
| 129 | + unmount_before_update=unmount_before_update, |
| 130 | + symlink=symlink, |
| 131 | + ) |
| 132 | + _FILE_WEB_MODULE_CACHE[name] = module |
| 133 | + return _vdom_from_web_module(module, export_names, fallback, allow_children) |
| 134 | + |
| 135 | + |
| 136 | +def import_js_from_string( |
| 137 | + name: str, |
| 138 | + content: str, |
| 139 | + export_names: str | list[str] | tuple[str, ...], |
| 140 | + fallback: Any | None = None, |
| 141 | + resolve_exports: bool | None = None, |
| 142 | + resolve_exports_depth: int = 5, |
| 143 | + unmount_before_update: bool = False, |
| 144 | + allow_children: bool = True, |
| 145 | +) -> VdomConstructor | list[VdomConstructor]: |
| 146 | + """Import a component from a string. |
| 147 | +
|
| 148 | + Parameters: |
| 149 | + name: |
| 150 | + The name of the package |
| 151 | + content: |
| 152 | + The contents of the web module |
| 153 | + export_names: |
| 154 | + One or more names to export. If given as a string, a single component |
| 155 | + will be returned. If a list is given, then a list of components will be |
| 156 | + returned. |
| 157 | + fallback: |
| 158 | + What to temporarily display while the module is being loaded. |
| 159 | + resolve_exports: |
| 160 | + Whether to try and find all the named exports of this module. |
| 161 | + resolve_exports_depth: |
| 162 | + How deeply to search for those exports. |
| 163 | + unmount_before_update: |
| 164 | + Cause the component to be unmounted before each update. This option should |
| 165 | + only be used if the imported package fails to re-render when props change. |
| 166 | + Using this option has negative performance consequences since all DOM |
| 167 | + elements must be changed on each render. See :issue:`461` for more info. |
| 168 | + allow_children: |
| 169 | + Whether or not these components can have children. |
| 170 | + """ |
| 171 | + if name in _STRING_WEB_MODULE_CACHE: |
| 172 | + module = _STRING_WEB_MODULE_CACHE[name] |
| 173 | + else: |
| 174 | + module = _module_from_string( |
| 175 | + name, |
| 176 | + content, |
| 177 | + fallback=fallback, |
| 178 | + resolve_exports=resolve_exports, |
| 179 | + resolve_exports_depth=resolve_exports_depth, |
| 180 | + unmount_before_update=unmount_before_update, |
| 181 | + ) |
| 182 | + _STRING_WEB_MODULE_CACHE[name] = module |
| 183 | + return _vdom_from_web_module(module, export_names, fallback, allow_children) |
| 184 | + |
| 185 | + |
30 | 186 | def module_from_url( |
31 | 187 | url: str, |
32 | 188 | fallback: Any | None = None, |
33 | 189 | resolve_exports: bool | None = None, |
34 | 190 | resolve_exports_depth: int = 5, |
35 | 191 | unmount_before_update: bool = False, |
| 192 | +) -> WebModule: |
| 193 | + warn( |
| 194 | + "module_from_url is deprecated, use import_js_from_url instead", |
| 195 | + DeprecationWarning, |
| 196 | + ) |
| 197 | + return _module_from_url( |
| 198 | + url, |
| 199 | + fallback=fallback, |
| 200 | + resolve_exports=resolve_exports, |
| 201 | + resolve_exports_depth=resolve_exports_depth, |
| 202 | + unmount_before_update=unmount_before_update, |
| 203 | + ) |
| 204 | + |
| 205 | + |
| 206 | +def module_from_file( |
| 207 | + name: str, |
| 208 | + file: str | Path, |
| 209 | + fallback: Any | None = None, |
| 210 | + resolve_exports: bool | None = None, |
| 211 | + resolve_exports_depth: int = 5, |
| 212 | + unmount_before_update: bool = False, |
| 213 | + symlink: bool = False, |
| 214 | +) -> WebModule: |
| 215 | + warn( |
| 216 | + "module_from_file is deprecated, use import_js_from_file instead", |
| 217 | + DeprecationWarning, |
| 218 | + ) |
| 219 | + return _module_from_file( |
| 220 | + name, |
| 221 | + file, |
| 222 | + fallback=fallback, |
| 223 | + resolve_exports=resolve_exports, |
| 224 | + resolve_exports_depth=resolve_exports_depth, |
| 225 | + unmount_before_update=unmount_before_update, |
| 226 | + symlink=symlink, |
| 227 | + ) |
| 228 | + |
| 229 | + |
| 230 | +def module_from_string( |
| 231 | + name: str, |
| 232 | + content: str, |
| 233 | + fallback: Any | None = None, |
| 234 | + resolve_exports: bool | None = None, |
| 235 | + resolve_exports_depth: int = 5, |
| 236 | + unmount_before_update: bool = False, |
| 237 | +) -> WebModule: |
| 238 | + warn( |
| 239 | + "module_from_string is deprecated, use import_js_from_string instead", |
| 240 | + DeprecationWarning, |
| 241 | + ) |
| 242 | + return _module_from_string( |
| 243 | + name, |
| 244 | + content, |
| 245 | + fallback=fallback, |
| 246 | + resolve_exports=resolve_exports, |
| 247 | + resolve_exports_depth=resolve_exports_depth, |
| 248 | + unmount_before_update=unmount_before_update, |
| 249 | + ) |
| 250 | + |
| 251 | + |
| 252 | +def _module_from_url( |
| 253 | + url: str, |
| 254 | + fallback: Any | None = None, |
| 255 | + resolve_exports: bool | None = None, |
| 256 | + resolve_exports_depth: int = 5, |
| 257 | + unmount_before_update: bool = False, |
36 | 258 | ) -> WebModule: |
37 | 259 | """Load a :class:`WebModule` from a :data:`URL_SOURCE` |
38 | 260 |
|
@@ -70,7 +292,7 @@ def module_from_url( |
70 | 292 | ) |
71 | 293 |
|
72 | 294 |
|
73 | | -def module_from_file( |
| 295 | +def _module_from_file( |
74 | 296 | name: str, |
75 | 297 | file: str | Path, |
76 | 298 | fallback: Any | None = None, |
@@ -152,7 +374,7 @@ def _copy_file(target: Path, source: Path, symlink: bool) -> None: |
152 | 374 | shutil.copy(source, target) |
153 | 375 |
|
154 | 376 |
|
155 | | -def module_from_string( |
| 377 | +def _module_from_string( |
156 | 378 | name: str, |
157 | 379 | content: str, |
158 | 380 | fallback: Any | None = None, |
@@ -244,6 +466,19 @@ def export( |
244 | 466 | export_names: str | list[str] | tuple[str, ...], |
245 | 467 | fallback: Any | None = None, |
246 | 468 | allow_children: bool = True, |
| 469 | +) -> VdomConstructor | list[VdomConstructor]: |
| 470 | + warn( |
| 471 | + "export is deprecated, use import_js_from_* functions instead", |
| 472 | + DeprecationWarning, |
| 473 | + ) |
| 474 | + return _vdom_from_web_module(web_module, export_names, fallback, allow_children) |
| 475 | + |
| 476 | + |
| 477 | +def _vdom_from_web_module( |
| 478 | + web_module: WebModule, |
| 479 | + export_names: str | list[str] | tuple[str, ...], |
| 480 | + fallback: Any | None = None, |
| 481 | + allow_children: bool = True, |
247 | 482 | ) -> VdomConstructor | list[VdomConstructor]: |
248 | 483 | """Return one or more VDOM constructors from a :class:`WebModule` |
249 | 484 |
|
|
0 commit comments