Skip to content

Commit fd408fa

Browse files
committed
add a template for react modules that export default
1 parent 85ab10a commit fd408fa

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

Diff for: src/idom/web/module.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from dataclasses import dataclass
1010
from functools import partial
1111
from pathlib import Path
12-
from typing import Any, List, NewType, Optional, Set, Tuple, Union, overload
12+
from typing import Any, Dict, List, NewType, Optional, Set, Tuple, Union, overload
1313
from urllib.parse import urlparse
1414

1515
from typing_extensions import Protocol
@@ -99,10 +99,14 @@ def module_from_template(
9999
warning.cIt's best to author a module adhering to the
100100
:ref:`Custom Javascript Component` interface instead.
101101
102+
**Templates**
103+
104+
- ``react``: for modules exporting React components
105+
- ``react-default``: for React modules that use ``export default``
106+
102107
Parameters:
103108
template:
104-
The name of the framework template to use with the given ``package``
105-
(only ``react`` is supported at the moment).
109+
The name of the framework template to use with the given ``package``.
106110
package:
107111
The name of a package to load. May include a file extension (defaults to
108112
``.js`` if not given)
@@ -137,7 +141,7 @@ def module_from_template(
137141
if not target_file.exists():
138142
target_file.parent.mkdir(parents=True, exist_ok=True)
139143
target_file.write_text(
140-
template_file.read_text().replace("$PACKAGE", package).replace("$CDN", cdn)
144+
_resolve_template(template_file, {"$PACKAGE": package, "$CDN": cdn})
141145
)
142146

143147
return WebModule(
@@ -316,3 +320,24 @@ def _web_module_path(name: str, prefix: str = "") -> Path:
316320
directory /= prefix
317321
path = directory.joinpath(*name.split("/"))
318322
return path.with_suffix(path.suffix)
323+
324+
325+
def _resolve_template(file: Path, substitutions: Dict[str, str]) -> str:
326+
# NOTE: If this needs to be any more complex than it is, we should really
327+
# reconsider this solution. Either use a real templating solution like Jinja
328+
# or do something else entirely.
329+
resolved_lines = []
330+
for line in file.read_text().splitlines():
331+
if line.startswith("$TEMPLATE:"):
332+
relative_path = line.split(":", 1)[1].strip()
333+
inner_template_file = file.parent.joinpath(*relative_path.split("/"))
334+
resolved_lines.append(_resolve_template(inner_template_file, {}))
335+
else:
336+
resolved_lines.append(line)
337+
338+
result = "\n".join(resolved_lines)
339+
if substitutions:
340+
for k, v in substitutions.items():
341+
result = result.replace(k, v)
342+
343+
return result

Diff for: src/idom/web/templates/react-default.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// add the default export
2+
export { default } from "$CDN/$PACKAGE";
3+
4+
// insert the normal react template
5+
$TEMPLATE:react.js

0 commit comments

Comments
 (0)