1010from functools import partial
1111from pathlib import Path
1212from typing import Any , List , NewType , Optional , Set , Tuple , Union , overload
13+ from urllib .parse import urlparse
1314
1415from idom .config import IDOM_DEBUG_MODE , IDOM_WED_MODULES_DIR
1516from idom .core .vdom import ImportSourceDict , VdomDictConstructor , make_vdom_constructor
@@ -70,11 +71,22 @@ def module_from_template(
7071 resolve_exports : bool = IDOM_DEBUG_MODE .current ,
7172 resolve_exports_depth : int = 5 ,
7273) -> 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.
7485
7586 Parameters:
7687 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``).
7890 package:
7991 The name of a package to load. May include a file extension (defaults to
8092 ``.js`` if not given)
@@ -87,22 +99,28 @@ def module_from_template(
8799 resolve_exports_depth:
88100 How deeply to search for those exports.
89101 """
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
90108 cdn = cdn .rstrip ("/" )
91109
92- template_file_name = f"{ template } { module_name_suffix (package )} "
110+ template_file_name = f"{ template } { module_name_suffix (package_name )} "
93111 template_file = Path (__file__ ).parent / "templates" / template_file_name
94112 if not template_file .exists ():
95113 raise ValueError (f"No template for { template_file_name !r} exists" )
96114
97- target_file = _web_module_path (package )
115+ target_file = _web_module_path (package_name )
98116 if not target_file .exists ():
99117 target_file .parent .mkdir (parents = True , exist_ok = True )
100118 target_file .write_text (
101119 template_file .read_text ().replace ("$PACKAGE" , package ).replace ("$CDN" , cdn )
102120 )
103121
104122 return WebModule (
105- source = package + module_name_suffix (package ),
123+ source = package_name + module_name_suffix (package_name ),
106124 source_type = NAME_SOURCE ,
107125 default_fallback = fallback ,
108126 file = target_file ,
0 commit comments