(wrapper_file, support_target, wasm_target)
| 2150 | |
| 2151 | |
| 2152 | def create_esm_wrapper(wrapper_file, support_target, wasm_target): |
| 2153 | js_exports = building.user_requested_exports.union(settings.EXPORTED_RUNTIME_METHODS) |
| 2154 | js_exports = ', '.join(sorted(js_exports)) |
| 2155 | |
| 2156 | wrapper = [] |
| 2157 | wrapper.append('// The wasm module must be imported here first before the support file') |
| 2158 | wrapper.append('// in order to avoid issues with circular dependencies.') |
| 2159 | wrapper.append(f"import * as unused from './{settings.WASM_BINARY_FILE}';") |
| 2160 | support_url = f'./{os.path.basename(support_target)}' |
| 2161 | if js_exports: |
| 2162 | wrapper.append(f"export {{ default, {js_exports} }} from '{support_url}';") |
| 2163 | else: |
| 2164 | wrapper.append(f"export {{ default }} from '{support_url}';") |
| 2165 | |
| 2166 | if settings.ENVIRONMENT_MAY_BE_NODE: |
| 2167 | wrapper.append(f''' |
| 2168 | // When run as the main module under node, create the module directly. This will |
| 2169 | // execute any startup code along with main (if it exists). |
| 2170 | import init from '{support_url}'; |
| 2171 | const isNode = {node_detection_code()}; |
| 2172 | if (isNode) {{ |
| 2173 | const url = await import('node:url'); |
| 2174 | const isMainModule = url.pathToFileURL(process.argv[1]).href === import.meta.url; |
| 2175 | if (isMainModule) await init(); |
| 2176 | }}''') |
| 2177 | |
| 2178 | write_file(wrapper_file, '\n'.join(wrapper) + '\n') |
| 2179 | |
| 2180 | # FIXME(sbc): This is a huge hack to rename the imports in the |
| 2181 | # wasm file. Find a better way to do this. |
| 2182 | wasm_dis = os.path.join(building.get_binaryen_bin(), 'wasm-dis') |
| 2183 | mod = shared.check_call([wasm_dis, wasm_target], stdout=PIPE).stdout |
| 2184 | mod = mod.replace('(import "env"', f'(import "{support_url}"') |
| 2185 | mod = mod.replace('(import "wasi_snapshot_preview1"', f'(import "{support_url}"') |
| 2186 | |
| 2187 | wasm_as = os.path.join(building.get_binaryen_bin(), 'wasm-as') |
| 2188 | cmd = [wasm_as, '--all-features', '-o', wasm_target, '-'] |
| 2189 | if settings.EMIT_NAME_SECTION: |
| 2190 | cmd.append('-g') |
| 2191 | shared.check_call(cmd, input=mod) |
| 2192 | |
| 2193 | |
| 2194 | def convert_line_endings_in_file(filename, to_eol): |
no test coverage detected