comparison libervia/server/tasks/implicit/task_js_modules.py @ 1259:0b269d4a46a3

task (js_modules): handle `extra_init`: if `extra_init` is found in module metadata's `brython_map`, it is added at the end of the generated module, and `{build_dir}` is replaced by actual build dir if present.
author Goffi <goffi@goffi.org>
date Sun, 03 May 2020 19:32:20 +0200
parents 780dbc2f4853
children 10748aa888a9
comparison
equal deleted inserted replaced
1258:92ff09cdd6dd 1259:0b269d4a46a3
56 raise ValueError( 56 raise ValueError(
57 f'module data for {module_name} must have a "path" key') 57 f'module data for {module_name} must have a "path" key')
58 module_data['path'] = Path('node_modules') / js_path.strip(' /') 58 module_data['path'] = Path('node_modules') / js_path.strip(' /')
59 export = module_data.get('export') or [module_name] 59 export = module_data.get('export') or [module_name]
60 export_objects = '\n'.join(f'{e} = window.{e}' for e in export) 60 export_objects = '\n'.join(f'{e} = window.{e}' for e in export)
61 extra_kwargs = {"build_dir": C.BUILD_DIR}
61 62
62 with module_path.open('w') as f: 63 with module_path.open('w') as f:
63 f.write(f"""\ 64 f.write(f"""\
64 #!/usr/bin/env python3 65 #!/usr/bin/env python3
65 from browser import window, load 66 from browser import window, load
66 {module_data.get('extra_import', '')} 67 {module_data.get('extra_import', '')}
67 68
68 load("{Path('/').joinpath(C.BUILD_DIR, module_data['path'])}") 69 load("{Path('/').joinpath(C.BUILD_DIR, module_data['path'])}")
69 {export_objects} 70 {export_objects}
70 {module_data.get('extra_init', '')} 71 {module_data.get('extra_init', '').format(**extra_kwargs)}
71 """) 72 """)