1+ from create_react_app .config import get_loader
2+ from create_react_app .utils import _filter_by_extension
3+
4+
5+ class AssetTag :
6+ def __init__ (self , public_path ):
7+ self .public_path = public_path
8+
9+ def css_tag (self , asset_path , attrib = "" , is_preload = False ):
10+ if is_preload :
11+ return '<link rel="preload" href="{0}{1}" {2} as="style" />' .format (self .public_path , asset_path , attrib )
12+ return '<link type="text/css" href="{0}{1}" rel="stylesheet" {2}/>' .format (self .public_path , asset_path , attrib )
13+
14+ def script_tag (self , asset_path , attrib = "" , is_preload = False ):
15+ if is_preload :
16+ return '<link rel="preload" href="{0}{1}" {2} as="script" />' .format (self .public_path , asset_path , attrib )
17+ return '<script type="text/javascript" src="{0}{1}" {2}></script>' .format (self .public_path , asset_path , attrib )
18+
19+ def src (self , asset_path , ** extra ):
20+ return '{0}{1}' .format (self .public_path , asset_path )
21+
22+
23+ class AssetManager (object ):
24+ def __init__ (self , config_name ):
25+ self .loader = get_loader (config_name )
26+ self .asset = AssetTag (self .loader .asset_path )
27+
28+ def get_bundle (self , extension ):
29+ bundle = self .loader .get_bundle ()
30+ if extension :
31+ bundle = _filter_by_extension (bundle , extension )
32+ if not bundle :
33+ return []
34+ return bundle
35+
36+ def get_tag_bundle (self , extension , call_back , ends_with = [], ** extra ):
37+ tags = []
38+ bundle = self .get_bundle (extension = extension )
39+ for chunk in bundle :
40+ if not self .loader .is_dev :
41+ chunk = chunk .replace ("static/" , "" )
42+ if chunk .endswith (tuple (ends_with )):
43+ tags .append (call_back (chunk , ** extra ))
44+ return tags
45+
46+ def css_callback (self , chunk , ** extra ):
47+ return self .asset .css_tag (chunk , ** extra )
48+
49+ def js_callback (self , chunk , ** extra ):
50+ return self .asset .script_tag (chunk , ** extra )
51+
52+ def css_tags (self , ** extra ):
53+ return self .get_tag_bundle (extension = "css" , call_back = self .css_callback , ends_with = ['.css' , '.css.gz' ], ** extra )
54+
55+ def js_tags (self , ** extra ):
56+ return self .get_tag_bundle (extension = "js" , call_back = self .js_callback , ends_with = ['.js' , '.js.gz' ], ** extra )
57+
58+
59+ class SrcAssetManager (AssetManager ):
60+ def js_callback (self , chunk , ** extra ):
61+ return self .asset .src (chunk , ** extra )
62+
63+ def css_callback (self , chunk , ** extra ):
64+ return self .asset .src (chunk , ** extra )
0 commit comments