11from create_react_app .config import get_loader
2- from create_react_app .utils import _page_bundle , _filter_by_extension
2+ from create_react_app .utils import _filter_by_extension
33
44
55class AssetTag :
66 def __init__ (self , public_path ):
77 self .public_path = public_path
88
9- def css_tag (self , asset_path , attrib = "" ):
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 )
1012 return '<link type="text/css" href="{0}{1}" rel="stylesheet" {2}/>' .format (self .public_path , asset_path , attrib )
1113
12- def script_tag (self , asset_path , attrib = "" ):
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 )
1317 return '<script type="text/javascript" src="{0}{1}" {2}></script>' .format (self .public_path , asset_path , attrib )
1418
19+ def src (self , asset_path , ** extra ):
20+ return '{0}{1}' .format (self .public_path , asset_path )
1521
16- class AssetManager :
22+
23+ class AssetManager (object ):
1724 def __init__ (self , config_name ):
1825 self .loader = get_loader (config_name )
1926 self .asset = AssetTag (self .loader .asset_path )
@@ -26,19 +33,32 @@ def get_bundle(self, extension):
2633 return []
2734 return bundle
2835
29- def get_tag_bundle (self , extension , ends_with = []):
36+ def get_tag_bundle (self , extension , call_back , ends_with = [], ** extra ):
3037 tags = []
3138 bundle = self .get_bundle (extension = extension )
3239 for chunk in bundle :
3340 if not self .loader .is_dev :
3441 chunk = chunk .replace ("static/" , "" )
35-
3642 if chunk .endswith (tuple (ends_with )):
37- tags .append (self . asset . script_tag (chunk ))
43+ tags .append (call_back (chunk , ** extra ))
3844 return tags
3945
40- def css_tags (self ):
41- return self .get_tag_bundle (extension = "css" , ends_with = ['.css' , '.css.gz' ])
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 )
4262
43- def js_tags (self ):
44- return self .get_tag_bundle ( extension = "js" , ends_with = [ '.js' , '.js.gz' ] )
63+ def css_callback (self , chunk , ** extra ):
64+ return self .asset . src ( chunk , ** extra )
0 commit comments