Coverage for gws-app/gws/base/client/bundles.py: 0%

44 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1"""Deal with client bundles created by the js bundler (app/js/helpers/builder.js)""" 

2 

3import gws 

4import gws.lib.jsonx 

5 

6BUNDLE_KEY_TEMPLATE = 'TEMPLATE' 

7BUNDLE_KEY_MODULES = 'MODULES' 

8BUNDLE_KEY_STRINGS = 'STRINGS' 

9BUNDLE_KEY_CSS = 'CSS' 

10 

11DEFAULT_LANG = 'de' 

12DEFAULT_THEME = 'light' 

13 

14 

15def javascript(root: gws.Root, category: str, locale: gws.Locale) -> str: 

16 if category == 'vendor': 

17 return gws.u.read_file(gws.c.APP_DIR + '/' + gws.c.JS_VENDOR_BUNDLE) 

18 

19 if category == 'util': 

20 return gws.u.read_file(gws.c.APP_DIR + '/' + gws.c.JS_UTIL_BUNDLE) 

21 

22 if category == 'app': 

23 return _make_app_js(root, locale) 

24 

25 

26def css(root: gws.Root, category: str, theme: str): 

27 if category == 'app': 

28 bundles = _load_app_bundles(root) 

29 theme = theme or DEFAULT_THEME 

30 return bundles.get(BUNDLE_KEY_CSS + '_' + theme) 

31 return '' 

32 

33 

34## 

35 

36def _load_app_bundles(root): 

37 

38 def _load(): 

39 bundles = {} 

40 

41 for path in root.specs.appBundlePaths: 

42 if gws.u.is_file(path): 

43 gws.log.debug(f'bundle {path!r}: loading') 

44 bundle = gws.lib.jsonx.from_path(path) 

45 for key, val in bundle.items(): 

46 if key not in bundles: 

47 bundles[key] = '' 

48 bundles[key] += val 

49 

50 return bundles 

51 

52 if root.app.developer_option('web.reload_bundles'): 

53 return _load() 

54 

55 return gws.u.get_server_global('APP_BUNDLES', _load) 

56 

57 

58def _make_app_js(root, locale): 

59 bundles = _load_app_bundles(root) 

60 

61 modules = bundles[BUNDLE_KEY_MODULES] 

62 strings = bundles.get(BUNDLE_KEY_STRINGS + '_' + locale.language) or bundles.get(BUNDLE_KEY_STRINGS + '_' + DEFAULT_LANG) 

63 

64 js = bundles[BUNDLE_KEY_TEMPLATE] 

65 js = js.replace('__MODULES__', modules) 

66 js = js.replace('__STRINGS__', strings) 

67 

68 return js