Coverage for gws-app/gws/base/client/bundles.py: 29%
42 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
1"""Client JavaScript and CSS bundles created by the JS bundler."""
3import gws
4import gws.lib.jsonx
6BUNDLE_KEY_TEMPLATE = 'TEMPLATE'
7BUNDLE_KEY_MODULES = 'MODULES'
8BUNDLE_KEY_STRINGS = 'STRINGS'
9BUNDLE_KEY_CSS = 'CSS'
11DEFAULT_LANG = 'de'
12DEFAULT_THEME = 'light'
15def javascript(root: gws.Root, category: str, locale: gws.Locale) -> str:
16 """Return a JavaScript bundle.
18 Args:
19 root: The configuration root.
20 category: ``vendor`` for the vendor libraries, ``app`` for the client
21 application with the UI strings for the locale language
22 (German if not available).
23 locale: The client locale.
25 Returns:
26 The JavaScript code, or ``None`` for an unknown category.
27 """
28 if category == 'vendor':
29 return gws.u.read_file(gws.c.APP_DIR + '/' + gws.c.JS_VENDOR_BUNDLE)
31 if category == 'app':
32 return _make_app_js(root, locale)
35def css(root: gws.Root, category: str, theme: str):
36 """Return a CSS bundle.
38 Args:
39 root: The configuration root.
40 category: Bundle category, only ``app`` has CSS.
41 theme: Theme name, ``light`` if empty.
43 Returns:
44 The CSS code, ``None`` if the theme is not found, or an empty string for other categories.
45 """
46 if category == 'app':
47 bundles = _load_app_bundles(root)
48 theme = theme or DEFAULT_THEME
49 return bundles.get(BUNDLE_KEY_CSS + '_' + theme)
50 return ''
53##
55def _load_app_bundles(root):
56 """Load and concatenate the application bundle files, cached per server unless the ``web.reload_bundles`` developer option is set."""
58 def _load():
59 bundles = {}
61 for path in root.specs.appBundlePaths:
62 if gws.u.is_file(path):
63 gws.log.debug(f'bundle {path!r}: loading')
64 bundle = gws.lib.jsonx.from_path(path)
65 for key, val in bundle.items():
66 if key not in bundles:
67 bundles[key] = ''
68 bundles[key] += val
70 return bundles
72 if root.app.developer_option('web.reload_bundles'):
73 return _load()
75 return gws.u.get_server_global('APP_BUNDLES', _load)
78def _make_app_js(root, locale):
79 """Build the application script from the bundle template, the modules and the UI strings."""
80 bundles = _load_app_bundles(root)
82 modules = bundles[BUNDLE_KEY_MODULES]
83 strings = bundles.get(BUNDLE_KEY_STRINGS + '_' + locale.language) or bundles.get(BUNDLE_KEY_STRINGS + '_' + DEFAULT_LANG)
85 js = bundles[BUNDLE_KEY_TEMPLATE]
86 js = js.replace('__MODULES__', modules)
87 js = js.replace('__STRINGS__', strings)
89 return js