Skip to content

Commit dfb434f

Browse files
committed
parse package as url path in module_from_template
1 parent 77f47f8 commit dfb434f

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

docs/source/javascript-components.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ Javascript ecosystem. This can be accomplished in different ways for different r
2222
- You want to create polished software that can be **easily shared** with others.
2323

2424

25+
.. _Dynamically Loaded Component:
26+
2527
Dynamically Loaded Components
2628
-----------------------------
2729

2830
.. note::
2931

3032
This method is not recommended in production systems - see
31-
:ref:`Distributing Javascript Components` for more info.
33+
:ref:`Distributing Javascript Components` for more info. Instead, it's best used
34+
during exploratory phases of development.
3235

3336
IDOM makes it easy to draft your code when you're in the early stages of development by
3437
using a CDN_ to dynamically load Javascript packages on the fly. In this example we'll
@@ -44,6 +47,8 @@ an ``onClick`` handler to the component:
4447
.. example:: material_ui_button_on_click
4548

4649

50+
.. _Custom Javascript Component:
51+
4752
Custom Javascript Components
4853
----------------------------
4954

src/idom/web/module.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from functools import partial
1111
from pathlib import Path
1212
from typing import Any, List, NewType, Optional, Set, Tuple, Union, overload
13+
from urllib.parse import urlparse
1314

1415
from idom.config import IDOM_DEBUG_MODE, IDOM_WED_MODULES_DIR
1516
from 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

Comments
 (0)