forked from QwikDev/qwik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinding-wasm.ts
71 lines (60 loc) · 1.76 KB
/
binding-wasm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { BuildConfig, copyFile, emptyDir, ensureDir } from './util';
import spawn from 'cross-spawn';
import { join } from 'node:path';
import { rollup } from 'rollup';
export async function buildWasmBinding(config: BuildConfig) {
const srcWasmDir = join(config.srcDir, `wasm`);
const tmpBuildDir = join(config.tmpDir, `wasm-out`);
ensureDir(config.distPkgDir);
ensureDir(config.distBindingsDir);
emptyDir(tmpBuildDir);
async function buildForTarget(env = {}) {
const cmd = `wasm-pack`;
const args = [`build`, '--target', 'web', `--out-dir`, tmpBuildDir, srcWasmDir];
if (!config.dev) {
args.push(`--release`);
}
await new Promise((resolve, reject) => {
const child = spawn(cmd, args, {
stdio: 'inherit',
shell: true,
env: {
...process.env,
...env,
},
});
child.on('error', reject);
child.on('close', (code) => {
if (code === 0) {
resolve(child.stdout);
} else {
reject(`wasm-pack exited with code ${code}`);
}
});
});
return join(tmpBuildDir, 'qwik_wasm.js');
}
const wasmJsBuildPath = await buildForTarget({
CARGO_PROFILE_RELEASE_LTO: true,
CARGO_PROFILE_RELEASE_PANIC: 'abort',
CARGO_PROFILE_RELEASE_OPT_LEVEL: 'z',
});
const build = await rollup({
input: wasmJsBuildPath,
});
await build.write({
format: 'es',
file: join(config.distBindingsDir, 'qwik.wasm.mjs'),
exports: 'named',
});
await build.write({
format: 'cjs',
file: join(config.distBindingsDir, 'qwik.wasm.cjs'),
exports: 'named',
});
await copyFile(
join(tmpBuildDir, 'qwik_wasm_bg.wasm'),
join(config.distBindingsDir, 'qwik_wasm_bg.wasm')
);
console.log('🐻 wasm binding');
}