Skip to content

Commit 11e6a9a

Browse files
committed
Support auto-naming for ReactJS component imports
1 parent fa7d60e commit 11e6a9a

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/reactpy/web/module.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import filecmp
4+
import hashlib
45
import logging
56
import shutil
67
from dataclasses import dataclass
@@ -106,9 +107,9 @@ def reactjs_component_from_url(
106107

107108
@overload
108109
def reactjs_component_from_file(
109-
name: str,
110110
file: str | Path,
111111
import_names: str,
112+
name: str = "",
112113
fallback: Any | None = ...,
113114
resolve_imports: bool | None = ...,
114115
resolve_imports_depth: int = ...,
@@ -120,9 +121,9 @@ def reactjs_component_from_file(
120121

121122
@overload
122123
def reactjs_component_from_file(
123-
name: str,
124124
file: str | Path,
125125
import_names: list[str] | tuple[str, ...],
126+
name: str = "",
126127
fallback: Any | None = ...,
127128
resolve_imports: bool | None = ...,
128129
resolve_imports_depth: int = ...,
@@ -133,9 +134,9 @@ def reactjs_component_from_file(
133134

134135

135136
def reactjs_component_from_file(
136-
name: str,
137137
file: str | Path,
138138
import_names: str | list[str] | tuple[str, ...],
139+
name: str = "",
139140
fallback: Any | None = None,
140141
resolve_imports: bool | None = None,
141142
resolve_imports_depth: int = 5,
@@ -146,14 +147,14 @@ def reactjs_component_from_file(
146147
"""Import a component from a file.
147148
148149
Parameters:
149-
name:
150-
The name of the package
151150
file:
152151
The file from which the content of the web module will be created.
153152
import_names:
154153
One or more component names to import. If given as a string, a single component
155154
will be returned. If a list is given, then a list of components will be
156155
returned.
156+
name:
157+
The human-readable name of the ReactJS package
157158
fallback:
158159
What to temporarily display while the module is being loaded.
159160
resolve_imports:
@@ -170,6 +171,7 @@ def reactjs_component_from_file(
170171
allow_children:
171172
Whether or not these components can have children.
172173
"""
174+
name = name or hashlib.sha256(str(file).encode()).hexdigest()[:10]
173175
key = f"{name}{resolve_imports}{resolve_imports_depth}{unmount_before_update}"
174176
if key in _FILE_WEB_MODULE_CACHE:
175177
module = _FILE_WEB_MODULE_CACHE[key]
@@ -189,9 +191,9 @@ def reactjs_component_from_file(
189191

190192
@overload
191193
def reactjs_component_from_string(
192-
name: str,
193194
content: str,
194195
import_names: str,
196+
name: str = "",
195197
fallback: Any | None = ...,
196198
resolve_imports: bool | None = ...,
197199
resolve_imports_depth: int = ...,
@@ -202,9 +204,9 @@ def reactjs_component_from_string(
202204

203205
@overload
204206
def reactjs_component_from_string(
205-
name: str,
206207
content: str,
207208
import_names: list[str] | tuple[str, ...],
209+
name: str = "",
208210
fallback: Any | None = ...,
209211
resolve_imports: bool | None = ...,
210212
resolve_imports_depth: int = ...,
@@ -214,9 +216,9 @@ def reactjs_component_from_string(
214216

215217

216218
def reactjs_component_from_string(
217-
name: str,
218219
content: str,
219220
import_names: str | list[str] | tuple[str, ...],
221+
name: str = "",
220222
fallback: Any | None = None,
221223
resolve_imports: bool | None = None,
222224
resolve_imports_depth: int = 5,
@@ -226,14 +228,14 @@ def reactjs_component_from_string(
226228
"""Import a component from a string.
227229
228230
Parameters:
229-
name:
230-
The name of the package
231231
content:
232232
The contents of the web module
233233
import_names:
234234
One or more component names to import. If given as a string, a single component
235235
will be returned. If a list is given, then a list of components will be
236236
returned.
237+
name:
238+
The human-readable name of the ReactJS package
237239
fallback:
238240
What to temporarily display while the module is being loaded.
239241
resolve_imports:
@@ -248,6 +250,7 @@ def reactjs_component_from_string(
248250
allow_children:
249251
Whether or not these components can have children.
250252
"""
253+
name = name or hashlib.sha256(content.encode()).hexdigest()[:10]
251254
key = f"{name}{resolve_imports}{resolve_imports_depth}{unmount_before_update}"
252255
if key in _STRING_WEB_MODULE_CACHE:
253256
module = _STRING_WEB_MODULE_CACHE[key]

0 commit comments

Comments
 (0)