11import logging
22import re
3- from pathlib import Path
3+ from pathlib import Path , PurePosixPath
44from typing import Set , Tuple
55from urllib .parse import urlparse
66
1212logger = logging .getLogger (__name__ )
1313
1414
15+ def url_suffix (name : str ) -> str :
16+ head , _ , tail = name .partition ("@" ) # handle version identifier
17+ version , _ , tail = tail .partition ("/" ) # get section after version
18+ return PurePosixPath (tail or head ).suffix or ".js"
19+
20+
1521def web_module_path (name : str ) -> Path :
22+ name += url_suffix (name )
1623 path = IDOM_WED_MODULES_DIR .current .joinpath (* name .split ("/" ))
17- return path .with_suffix (path .suffix + ".js" )
24+ return path .with_suffix (path .suffix )
1825
1926
2027def resolve_module_exports_from_file (file : Path , max_depth : int ) -> Set [str ]:
@@ -31,7 +38,7 @@ def resolve_module_exports_from_file(file: Path, max_depth: int) -> Set[str]:
3138 if urlparse (ref ).scheme : # is an absolute URL
3239 export_names .update (resolve_module_exports_from_url (ref , max_depth - 1 ))
3340 else :
34- path = _resolve_relative_file_path ( file , ref )
41+ path = file . parent . joinpath ( * ref . split ( "/" ) )
3542 export_names .update (resolve_module_exports_from_file (path , max_depth - 1 ))
3643
3744 return export_names
@@ -102,23 +109,19 @@ def resolve_module_exports_from_source(content: str) -> Tuple[Set[str], Set[str]
102109 return {n .strip () for n in names }, {r .strip () for r in references }
103110
104111
105- def _resolve_relative_file_path (base_path : Path , rel_url : str ) -> Path :
106- if rel_url .startswith ("./" ):
107- return base_path .parent / rel_url [2 :]
108- while rel_url .startswith ("../" ):
109- base_path = base_path .parent
110- rel_url = rel_url [3 :]
111- return base_path / rel_url
112-
113-
114112def _resolve_relative_url (base_url : str , rel_url : str ) -> str :
115113 if not rel_url .startswith ("." ):
116114 return rel_url
117- elif rel_url .startswith ("./" ):
118- return base_url .rsplit ("/" )[0 ] + rel_url [1 :]
115+
116+ base_url = base_url .rsplit ("/" , 1 )[0 ]
117+
118+ if rel_url .startswith ("./" ):
119+ return base_url + rel_url [1 :]
120+
119121 while rel_url .startswith ("../" ):
120- base_url = base_url .rsplit ("/" )[0 ]
122+ base_url = base_url .rsplit ("/" , 1 )[0 ]
121123 rel_url = rel_url [3 :]
124+
122125 return f"{ base_url } /{ rel_url } "
123126
124127
0 commit comments