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

42 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-24 12:46 +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 == 'app': 

20 return _make_app_js(root, locale) 

21 

22 

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

24 if category == 'app': 

25 bundles = _load_app_bundles(root) 

26 theme = theme or DEFAULT_THEME 

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

28 return '' 

29 

30 

31## 

32 

33def _load_app_bundles(root): 

34 

35 def _load(): 

36 bundles = {} 

37 

38 for path in root.specs.appBundlePaths: 

39 if gws.u.is_file(path): 

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

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

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

43 if key not in bundles: 

44 bundles[key] = '' 

45 bundles[key] += val 

46 

47 return bundles 

48 

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

50 return _load() 

51 

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

53 

54 

55def _make_app_js(root, locale): 

56 bundles = _load_app_bundles(root) 

57 

58 modules = bundles[BUNDLE_KEY_MODULES] 

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

60 

61 js = bundles[BUNDLE_KEY_TEMPLATE] 

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

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

64 

65 return js