1+ from __future__ import annotations
2+
13import itertools
24import json
35import re
46from pathlib import Path
57
68from django .conf import settings
7- from django .contrib .staticfiles .finders import get_finders
8- from django .contrib .staticfiles .storage import staticfiles_storage
99
1010
1111def parse_root_package (package_json ):
@@ -26,32 +26,32 @@ def parse_root_package(package_json):
2626 url = mod
2727 if mod [0 ] in ["." , "/" ]:
2828 # local file
29- yield from get_static_from_abs_path (module_name , settings .BASE_DIR / mod )
29+ yield from get_static_from_abs_path (
30+ module_name , settings .BASE_DIR / mod , settings .BASE_DIR
31+ )
3032 else :
3133 yield module_name , url
3234
35+
36+ def parse_dependencies (package_json ):
3337 for dep_name , dep_version in package_json .get ("dependencies" , {}).items ():
3438 yield from parse_package_json (settings .BASE_DIR / "node_modules" / dep_name )
3539
3640
37- def get_static_from_abs_path (mod : str , path : Path ):
38- for finder in get_finders ():
39- for storage in finder .storages .values ():
40- try :
41- rel_path = path .relative_to (Path (storage .location ).resolve ())
42- except ValueError :
43- pass
44- else :
45- if "*" in mod :
46- for match in Path (storage .location ).rglob (
47- str (rel_path ).replace ("*" , "**/*" )
48- ):
49- sp = str (match .relative_to (Path (storage .location ).resolve ()))
50- pattern = re .escape (str (rel_path )).replace (r"\*" , r"(.*)" )
51- bit = re .match (pattern , sp ).group (1 )
52- yield mod .replace ("*" , bit ), staticfiles_storage .url (sp )
53- else :
54- yield mod , staticfiles_storage .url (str (rel_path ))
41+ def get_static_from_abs_path (mod : str , path : Path , location : Path ):
42+ try :
43+ rel_path = path .relative_to (location .resolve ())
44+ except ValueError :
45+ pass
46+ else :
47+ if "*" in mod :
48+ for match in location .rglob (str (rel_path ).replace ("*" , "**/*" )):
49+ sp = str (match .relative_to (location .resolve ()))
50+ pattern = re .escape (str (rel_path )).replace (r"\*" , r"(.*)" )
51+ bit = re .match (pattern , sp ).group (1 )
52+ yield mod .replace ("*" , bit ), sp
53+ else :
54+ yield mod , str (rel_path )
5555
5656
5757# There is a long history how ESM is supported in Node.js
@@ -101,7 +101,9 @@ def parse_package_json(path: Path = None):
101101 module = next (find_default_key (module ))
102102
103103 yield from get_static_from_abs_path (
104- str (Path (name ) / module_name ), path / module
104+ str (Path (name ) / module_name ),
105+ path / module ,
106+ settings .BASE_DIR / "node_modules" ,
105107 )
106108
107109 for dep_name , dep_version in dependencies .items ():
0 commit comments