From 85d91e10b42a27d61e0a51d4e80edaf801e19a30 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Thu, 23 Mar 2023 22:19:26 +0800 Subject: [PATCH] chore: speedup wasm build --- crates/loro-wasm/scripts/build.ts | 37 ++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/crates/loro-wasm/scripts/build.ts b/crates/loro-wasm/scripts/build.ts index 0904d6b0..c40449cd 100644 --- a/crates/loro-wasm/scripts/build.ts +++ b/crates/loro-wasm/scripts/build.ts @@ -32,6 +32,26 @@ async function build() { return buildTarget(target); })); + if (profile !== "dev") { + let first = true; + let firstTarget = ""; + for (const target of TARGETS) { + if (first) { + const cmd = + `wasm-opt -O4 ${target}/loro_wasm_bg.wasm -o ${target}/loro_wasm_bg.wasm`; + console.log(">", cmd); + firstTarget = target; + await Deno.run({ cmd: cmd.split(" "), cwd: LoroWasmDir }).status(); + first = false; + } else { + const cmd = + `cp ${firstTarget}/loro_wasm_bg.wasm ${target}/loro_wasm_bg.wasm`; + console.log(">", cmd); + await Deno.run({ cmd: cmd.split(" "), cwd: LoroWasmDir }).status(); + } + } + } + console.log( "✅", "Build complete in", @@ -67,20 +87,11 @@ async function buildTarget(target: string) { console.log("Clear directory " + targetDirPath); } catch (e) {} - for (const cmd of genCommands(target)) { - console.log(">", cmd); - await Deno.run({ cmd: cmd.split(" "), cwd: LoroWasmDir }).status(); - } + const cmd = + `wasm-bindgen --weak-refs --target ${target} --out-dir ${target} ../../target/wasm32-unknown-unknown/${profileDir}/loro_wasm.wasm`; + console.log(">", cmd); + await Deno.run({ cmd: cmd.split(" "), cwd: LoroWasmDir }).status(); console.log(); } -function genCommands(target: string): string[] { - return [ - `wasm-bindgen --weak-refs --target ${target} --out-dir ${target} ../../target/wasm32-unknown-unknown/${profileDir}/loro_wasm.wasm`, - ...(profile == "dev" ? [] : [ - `wasm-opt -O4 ${target}/loro_wasm_bg.wasm -o ${target}/loro_wasm_bg.wasm`, - ]), - ]; -} - build();