|
9 | 9 | BuildConfigItem, |
10 | 10 | find_python_packages_build_config_items, |
11 | 11 | ) |
12 | | -from .utils import open_modifiable_json |
| 12 | +from .utils import open_modifiable_json, find_js_module_exports |
13 | 13 |
|
14 | 14 | from idom.cli import console |
15 | 15 |
|
16 | 16 |
|
17 | 17 | APP_DIR = Path(__file__).parent / "app" |
18 | 18 | BUILD_DIR = APP_DIR / "build" |
| 19 | +_BUILD_CONFIG: Optional[BuildConfig] = None |
19 | 20 |
|
20 | 21 |
|
21 | 22 | def build_config() -> BuildConfig: |
22 | | - return BuildConfig(BUILD_DIR) |
| 23 | + global _BUILD_CONFIG |
| 24 | + if _BUILD_CONFIG is None: |
| 25 | + _BUILD_CONFIG = BuildConfig(BUILD_DIR) |
| 26 | + return _BUILD_CONFIG |
23 | 27 |
|
24 | 28 |
|
25 | | -def find_path(url_path: str) -> Optional[Path]: |
26 | | - url_path = url_path.strip("/") |
27 | | - |
28 | | - builtin_path = BUILD_DIR.joinpath(*url_path.split("/")) |
29 | | - if builtin_path.exists(): |
30 | | - return builtin_path |
31 | | - else: |
32 | | - return None |
| 29 | +def web_module_exports(source_name: str, package_name: str) -> List[str]: |
| 30 | + dep_alias = build_config().get_js_dependency_alias(source_name, package_name) |
| 31 | + if dep_alias is None: |
| 32 | + return [] |
| 33 | + module_file = find_client_build_path(f"web_modules/{dep_alias}.js") |
| 34 | + if module_file is None: |
| 35 | + return [] |
| 36 | + return find_js_module_exports(module_file) |
33 | 37 |
|
34 | 38 |
|
35 | 39 | def web_module_url(source_name: str, package_name: str) -> Optional[str]: |
36 | | - config = build_config().configs.get(source_name) |
37 | | - if config is None: |
38 | | - return None |
39 | | - if package_name not in config.js_dependency_aliases: |
| 40 | + dep_alias = build_config().get_js_dependency_alias(source_name, package_name) |
| 41 | + if dep_alias is None: |
40 | 42 | return None |
41 | | - alias = config.js_dependency_aliases[package_name] |
42 | | - if find_path(f"web_modules/{alias}.js") is None: |
| 43 | + if find_client_build_path(f"web_modules/{dep_alias}.js") is None: |
43 | 44 | return None |
44 | 45 | # need to go back a level since the JS that import this is in `core_components` |
45 | | - return f"../web_modules/{alias}.js" |
| 46 | + return f"../web_modules/{dep_alias}.js" |
| 47 | + |
| 48 | + |
| 49 | +def find_client_build_path(rel_path: str) -> Optional[Path]: |
| 50 | + if rel_path.startswith("/"): |
| 51 | + raise ValueError(f"{rel_path!r} is not a relative path") |
| 52 | + builtin_path = BUILD_DIR.joinpath(*rel_path.split("/")) |
| 53 | + if builtin_path.exists(): |
| 54 | + return builtin_path |
| 55 | + else: |
| 56 | + return None |
46 | 57 |
|
47 | 58 |
|
48 | | -def build( |
49 | | - config_items: Optional[Iterable[BuildConfigItem]] = None, |
50 | | - output_dir: Path = BUILD_DIR, |
51 | | -) -> None: |
| 59 | +def build(config_items: Iterable[BuildConfigItem] = ()) -> None: |
52 | 60 | config = build_config() |
53 | 61 |
|
54 | 62 | with config.transaction(): |
55 | | - if config_items is not None: |
56 | | - config.update_config_items(config_items) |
| 63 | + config.update_config_items(config_items) |
57 | 64 |
|
58 | 65 | with console.spinner("Discovering dependencies"): |
59 | 66 | configs, errors = find_python_packages_build_config_items() |
@@ -88,10 +95,10 @@ def build( |
88 | 95 | with console.spinner("Building client"): |
89 | 96 | _npm_run_build(temp_app_dir) |
90 | 97 |
|
91 | | - if output_dir.exists(): |
92 | | - shutil.rmtree(output_dir) |
| 98 | + if BUILD_DIR.exists(): |
| 99 | + shutil.rmtree(BUILD_DIR) |
93 | 100 |
|
94 | | - shutil.copytree(temp_build_dir, output_dir, symlinks=True) |
| 101 | + shutil.copytree(temp_build_dir, BUILD_DIR, symlinks=True) |
95 | 102 |
|
96 | 103 |
|
97 | 104 | def restore() -> None: |
|
0 commit comments