| 135 | }; |
| 136 | |
| 137 | export async function loadPlatformBinding(sys: OptimizerSystem) { |
| 138 | const sysEnv = getEnv(); |
| 139 | |
| 140 | // Try native build |
| 141 | if (sysEnv === 'node' || sysEnv === 'bun') { |
| 142 | // Node.js |
| 143 | const platform = (QWIK_BINDING_MAP as any)[process.platform]; |
| 144 | if (platform) { |
| 145 | const triples = platform[process.arch]; |
| 146 | if (triples) { |
| 147 | for (const triple of triples) { |
| 148 | // Node.js - Native Binding |
| 149 | try { |
| 150 | if (globalThis.IS_ESM) { |
| 151 | const module = await sys.dynamicImport('node:module'); |
| 152 | const mod = module.default.createRequire(import.meta.url)( |
| 153 | `../bindings/${triple.platformArchABI}` |
| 154 | ); |
| 155 | return mod; |
| 156 | } |
| 157 | const mod = await sys.dynamicImport(`../bindings/${triple.platformArchABI}`); |
| 158 | return mod; |
| 159 | } catch (e) { |
| 160 | console.warn( |
| 161 | `Unable to load native binding ${triple.platformArchABI}. Falling back to wasm build.`, |
| 162 | (e as Error)?.message |
| 163 | ); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (globalThis.IS_CJS) { |
| 171 | // CJS WASM |
| 172 | |
| 173 | if (sysEnv === 'node' || sysEnv === 'bun') { |
| 174 | // CJS WASM Node.js |
| 175 | const wasmPath = sys.path.join(__dirname, '..', 'bindings', 'qwik_wasm_bg.wasm'); |
| 176 | const mod = await sys.dynamicImport(`../bindings/qwik.wasm.cjs`); |
| 177 | const fs: typeof import('fs') = await sys.dynamicImport('node:fs'); |
| 178 | |
| 179 | return new Promise<Buffer>((resolve, reject) => { |
| 180 | fs.readFile(wasmPath, (err, buf) => { |
| 181 | if (err != null) { |
| 182 | reject(err); |
| 183 | } else { |
| 184 | resolve(buf); |
| 185 | } |
| 186 | }); |
| 187 | }) |
| 188 | .then((buf) => WebAssembly.compile(buf)) |
| 189 | .then((wasm) => mod.default(wasm)) |
| 190 | .then(() => mod); |
| 191 | } |
| 192 | |
| 193 | if (sysEnv === 'webworker' || sysEnv === 'browsermain') { |
| 194 | // CJS WASM Browser |