annotate libervia/web/server/tasks/implicit/task_js_modules.py @ 1612:ebd538cb26cb

server, pages (files/view): use new `use_local_shared_tmp` option.
author Goffi <goffi@goffi.org>
date Wed, 12 Jun 2024 23:11:08 +0200
parents d07838fc9d99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/ur/bin/env python3
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 import json
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from pathlib import Path
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
5 from libervia.backend.core.i18n import _
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
6 from libervia.backend.core.log import getLogger
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
7 from libervia.backend.core import exceptions
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
8 from libervia.web.server.constants import Const as C
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
9 from libervia.web.server.tasks import task
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 log = getLogger(__name__)
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
13
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
14
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 class Task(task.Task):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 async def prepare(self):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 if "js" not in self.resource.browser_modules:
1434
2f6dac783c8e tasks(js_modules, sass): remove support for `npm`:
Goffi <goffi@goffi.org>
parents: 1273
diff changeset
19 raise exceptions.CancelError("No JS module needed")
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 async def start(self):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 js_data = self.resource.browser_modules['js']
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 package = js_data.get('package', {})
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 package_path = self.build_path / 'package.json'
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 with package_path.open('w') as f:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 json.dump(package, f)
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1434
diff changeset
28 cmd = self.find_command('yarnpkg', 'yarn')
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 await self.runCommand(cmd, 'install', path=str(self.build_path))
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 try:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 brython_map = js_data['brython_map']
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 except KeyError:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 pass
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 else:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 log.info(_("creating JS modules mapping for Brython"))
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 js_modules_path = self.build_path / 'js_modules'
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 js_modules_path.mkdir(exist_ok=True)
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 init_path = js_modules_path / '__init__.py'
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 init_path.touch()
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 for module_name, module_data in brython_map.items():
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 log.debug(f"generating mapping for {module_name}")
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 if ' ' in module_name:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 raise ValueError(
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 f"module {module_name!r} has space(s), it must not!")
1570
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
47 module_python_name = module_name.replace(".", "_").replace("-", "_")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
48 if module_python_name != module_name:
1601
d07838fc9d99 minor updates
Goffi <goffi@goffi.org>
parents: 1570
diff changeset
49 log.info(f"{module_python_name!r} will be used as python module name")
1570
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
50 module_path = js_modules_path / f"{module_python_name}.py"
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 if isinstance(module_data, str):
1570
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
52 module_data = {'path': [module_data]}
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 try:
1570
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
54 js_paths = module_data.pop('path')
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 except KeyError:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 raise ValueError(
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 f'module data for {module_name} must have a "path" key')
1570
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
58 if isinstance(js_paths, str):
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
59 js_paths = [js_paths]
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
60 module_data['path'] = [
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
61 Path('node_modules') / js_path.strip(' /') for js_path in js_paths
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
62 ]
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
63 to_export = module_data.get("export", [module_name])
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
64 if isinstance(to_export, str):
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
65 to_export = to_export.split(",")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
66 to_export = [e.strip() for e in to_export]
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
67
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
68 init_code = []
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
69
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
70 # CSS if any
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
71 css_to_load = module_data.get("css")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
72 if css_to_load:
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
73 if isinstance(css_to_load, str):
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
74 css_to_load = [css_to_load]
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
75
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
76 add_styles_lines = []
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
77 for css_path in css_to_load:
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
78 normalized_css_path = Path("node_modules") / css_path.strip(" /")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
79 css_href = Path("/").joinpath(C.BUILD_DIR, normalized_css_path)
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
80 style_tag = (
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
81 f'<link rel="stylesheet" type="text/css" href="{css_href}">'
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
82 )
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
83 add_style_code = (
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
84 'document.head.insertAdjacentHTML('
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
85 '"beforeend", '
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
86 f"'{style_tag}')"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
87 )
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
88 add_styles_lines.append(add_style_code)
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
89
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
90 add_styles = "\n".join(add_styles_lines)
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
91 else:
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
92 add_styles = ""
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 with module_path.open('w') as f:
1570
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
95 browser_imports = ["aio", "document"]
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
96 modules_import = []
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
97 script_paths = [
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
98 str(Path('/').joinpath(C.BUILD_DIR, path))
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
99 for path in module_data["path"]
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
100 ]
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
101 import_type = module_data.get('import_type', "load")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
102 if import_type == 'module':
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
103 modules_import.append('javascript')
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
104 modules_import.append('proxy')
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
105 callback_function_name = f"on_{module_python_name}_loaded"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
106 declare_obj = "\n".join(f"{e} = proxy.JSProxy()" for e in to_export)
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
107 export_str = "\n ".join(f"{e}.js_module=module.{e}" for e in to_export)
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
108 load_js_libraries = (
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
109 f"\n{declare_obj}\n\n"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
110 f"def {callback_function_name}(module):\n"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
111 f" {export_str}\n"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
112 f" loaded.set_result(True)\n\n"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
113 f"javascript.import_modules({script_paths}, {callback_function_name})\n"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
114 )
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
115 elif import_type == 'script':
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
116 browser_imports.append("window")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
117 script_tags = [
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
118 f"<script src='{src}'></script>" for src in script_paths
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
119 ]
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
120 load_js_libraries = "\n".join(
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
121 f'document.head.insertAdjacentHTML("beforeend", "{tag}")'
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
122 for tag in script_tags
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
123 )
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
124 init_code.append('\n'.join(f'{e} = window.{e}' for e in to_export))
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
125 init_code.append("loaded.set_result(True)")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
126 elif import_type == "load":
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
127 browser_imports.append("load")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
128 browser_imports.append("window")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
129 load_calls = [f'load("{src}")' for src in script_paths]
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
130 load_js_libraries = "\n".join(load_calls)
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
131 init_code.append('\n'.join(f'{e} = window.{e}' for e in to_export))
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
132 init_code.append("loaded.set_result(True)")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
133 else:
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
134 raise ValueError("Invalid import type: {import_type!r}")
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
135
1570
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
136 extra_init = module_data.get("extra_init")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
137 if extra_init is not None:
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
138 if not isinstance(extra_init, str):
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
139 raise ValueError(f"Invalid extra_init: {extra_init!r}")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
140 init_code.append(
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
141 extra_init.format(
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
142 build_dir = C.BUILD_DIR
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
143 )
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
144 )
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
145
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
146 init_code_str = "\n".join(init_code)
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
147 imports = [f"from browser import {', '.join(browser_imports)}"]
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
148 for module in modules_import:
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
149 imports.append(f"import {module}")
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
150 imports_str = "\n".join(imports)
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
151
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
152 f.write(
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
153 "#!/usr/bin/env python3\n"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
154 f"{imports_str}\n\n"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
155 f"loaded = aio.Future()\n"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
156 f"{add_styles}\n"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
157 f"{load_js_libraries}\n"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
158 f"{init_code_str}"
038d4bfdd967 server (tasks/JS modules): add new way to generate modules + support of CSS files
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
159 )