|
5 | 5 | import shutil |
6 | 6 | from dataclasses import dataclass |
7 | 7 | from pathlib import Path |
8 | | -from string import Template |
9 | 8 | from typing import Any, NewType, overload |
10 | | -from urllib.parse import urlparse |
11 | 9 |
|
12 | | -from reactpy._warnings import warn |
13 | 10 | from reactpy.config import REACTPY_DEBUG_MODE, REACTPY_WEB_MODULES_DIR |
14 | 11 | from reactpy.core.types import ImportSourceDict, VdomDictConstructor |
15 | 12 | from reactpy.core.vdom import make_vdom_constructor |
@@ -73,90 +70,6 @@ def module_from_url( |
73 | 70 | ) |
74 | 71 |
|
75 | 72 |
|
76 | | -_FROM_TEMPLATE_DIR = "__from_template__" |
77 | | - |
78 | | - |
79 | | -def module_from_template( |
80 | | - template: str, |
81 | | - package: str, |
82 | | - cdn: str = "https://esm.sh", |
83 | | - fallback: Any | None = None, |
84 | | - resolve_exports: bool | None = None, |
85 | | - resolve_exports_depth: int = 5, |
86 | | - unmount_before_update: bool = False, |
87 | | -) -> WebModule: |
88 | | - """Create a :class:`WebModule` from a framework template |
89 | | -
|
90 | | - This is useful for experimenting with component libraries that do not already |
91 | | - support ReactPy's :ref:`Custom Javascript Component` interface. |
92 | | -
|
93 | | - .. warning:: |
94 | | -
|
95 | | - This approach is not recommended for use in a production setting because the |
96 | | - framework templates may use unpinned dependencies that could change without |
97 | | - warning. It's best to author a module adhering to the |
98 | | - :ref:`Custom Javascript Component` interface instead. |
99 | | -
|
100 | | - **Templates** |
101 | | -
|
102 | | - - ``react``: for modules exporting React components |
103 | | -
|
104 | | - Parameters: |
105 | | - template: |
106 | | - The name of the framework template to use with the given ``package``. |
107 | | - package: |
108 | | - The name of a package to load. May include a file extension (defaults to |
109 | | - ``.js`` if not given) |
110 | | - cdn: |
111 | | - Where the package should be loaded from. The CDN must distribute ESM modules |
112 | | - fallback: |
113 | | - What to temporarily display while the module is being loaded. |
114 | | - resolve_imports: |
115 | | - Whether to try and find all the named exports of this module. |
116 | | - resolve_exports_depth: |
117 | | - How deeply to search for those exports. |
118 | | - unmount_before_update: |
119 | | - Cause the component to be unmounted before each update. This option should |
120 | | - only be used if the imported package fails to re-render when props change. |
121 | | - Using this option has negative performance consequences since all DOM |
122 | | - elements must be changed on each render. See :issue:`461` for more info. |
123 | | - """ |
124 | | - warn( |
125 | | - "module_from_template() is deprecated due to instability - use the Javascript " |
126 | | - "Components API instead. This function will be removed in a future release.", |
127 | | - DeprecationWarning, |
128 | | - ) |
129 | | - template_name, _, template_version = template.partition("@") |
130 | | - template_version = "@" + template_version if template_version else "" |
131 | | - |
132 | | - # We do this since the package may be any valid URL path. Thus we may need to strip |
133 | | - # object parameters or query information so we save the resulting template under the |
134 | | - # correct file name. |
135 | | - package_name = urlparse(package).path |
136 | | - |
137 | | - # downstream code assumes no trailing slash |
138 | | - cdn = cdn.rstrip("/") |
139 | | - |
140 | | - template_file_name = template_name + module_name_suffix(package_name) |
141 | | - |
142 | | - template_file = Path(__file__).parent / "templates" / template_file_name |
143 | | - if not template_file.exists(): |
144 | | - msg = f"No template for {template_file_name!r} exists" |
145 | | - raise ValueError(msg) |
146 | | - |
147 | | - variables = {"PACKAGE": package, "CDN": cdn, "VERSION": template_version} |
148 | | - content = Template(template_file.read_text(encoding="utf-8")).substitute(variables) |
149 | | - |
150 | | - return module_from_string( |
151 | | - _FROM_TEMPLATE_DIR + "/" + package_name, |
152 | | - content, |
153 | | - fallback, |
154 | | - resolve_exports, |
155 | | - resolve_exports_depth, |
156 | | - unmount_before_update=unmount_before_update, |
157 | | - ) |
158 | | - |
159 | | - |
160 | 73 | def module_from_file( |
161 | 74 | name: str, |
162 | 75 | file: str | Path, |
|
0 commit comments