10
10
from functools import partial
11
11
from pathlib import Path
12
12
from typing import Any , List , NewType , Optional , Set , Tuple , Union , overload
13
+ from urllib .parse import urlparse
13
14
14
15
from idom .config import IDOM_DEBUG_MODE , IDOM_WED_MODULES_DIR
15
16
from idom .core .vdom import ImportSourceDict , VdomDictConstructor , make_vdom_constructor
@@ -70,11 +71,22 @@ def module_from_template(
70
71
resolve_exports : bool = IDOM_DEBUG_MODE .current ,
71
72
resolve_exports_depth : int = 5 ,
72
73
) -> WebModule :
73
- """Load a :class:`WebModule` from a :data:`URL_SOURCE` using a known framework
74
+ """Create a :class:`WebModule` from a framework template
75
+
76
+ This is useful for experimenting with component libraries that do not already
77
+ support IDOM's :ref:`Custom Javascript Component` interface.
78
+
79
+ .. warning::
80
+
81
+ This approach is not recommended for use in a production setting because the
82
+ framework templates may use unpinned dependencies that could change without
83
+ warning.cIt's best to author a module adhering to the
84
+ :ref:`Custom Javascript Component` interface instead.
74
85
75
86
Parameters:
76
87
template:
77
- The name of the template to use with the given ``package`` (``react`` | ``preact``)
88
+ The name of the framework template to use with the given ``package``
89
+ (``react`` | ``preact``).
78
90
package:
79
91
The name of a package to load. May include a file extension (defaults to
80
92
``.js`` if not given)
@@ -87,22 +99,28 @@ def module_from_template(
87
99
resolve_exports_depth:
88
100
How deeply to search for those exports.
89
101
"""
102
+ # We do this since the package may be any valid URL path. Thus we may need to strip
103
+ # object parameters or query information so we save the resulting template under the
104
+ # correct file name.
105
+ package_name = urlparse (package ).path
106
+
107
+ # downstream code assumes no trailing slash
90
108
cdn = cdn .rstrip ("/" )
91
109
92
- template_file_name = f"{ template } { module_name_suffix (package )} "
110
+ template_file_name = f"{ template } { module_name_suffix (package_name )} "
93
111
template_file = Path (__file__ ).parent / "templates" / template_file_name
94
112
if not template_file .exists ():
95
113
raise ValueError (f"No template for { template_file_name !r} exists" )
96
114
97
- target_file = _web_module_path (package )
115
+ target_file = _web_module_path (package_name )
98
116
if not target_file .exists ():
99
117
target_file .parent .mkdir (parents = True , exist_ok = True )
100
118
target_file .write_text (
101
119
template_file .read_text ().replace ("$PACKAGE" , package ).replace ("$CDN" , cdn )
102
120
)
103
121
104
122
return WebModule (
105
- source = package + module_name_suffix (package ),
123
+ source = package_name + module_name_suffix (package_name ),
106
124
source_type = NAME_SOURCE ,
107
125
default_fallback = fallback ,
108
126
file = target_file ,
0 commit comments