loro/crates/loro-wasm/scripts/build.ts

106 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-12-09 04:03:45 +00:00
import { resolve } from "https://deno.land/std@0.105.0/path/mod.ts";
2023-11-20 16:34:20 +00:00
import __ from "https://raw.githubusercontent.com/zxch3n/dirname/master/mod.ts";
2022-12-09 04:03:45 +00:00
const { __dirname } = __(import.meta);
2022-12-07 17:28:51 +00:00
// deno run -A build.ts debug
// deno run -A build.ts release
// deno run -A build.ts release web
// deno run -A build.ts release nodejs
2022-12-09 04:03:45 +00:00
let profile = "dev";
2022-12-18 13:05:04 +00:00
let profileDir = "debug";
if (Deno.args[0] == "release") {
2022-12-09 04:03:45 +00:00
profile = "release";
2022-12-18 13:05:04 +00:00
profileDir = "release";
2022-12-07 17:28:51 +00:00
}
const TARGETS = ["bundler", "nodejs"];
2022-12-07 18:25:13 +00:00
const startTime = performance.now();
2022-12-09 04:03:45 +00:00
const LoroWasmDir = resolve(__dirname, "..");
2022-12-07 17:28:51 +00:00
2022-12-09 04:03:45 +00:00
console.log(LoroWasmDir);
2022-12-07 17:28:51 +00:00
async function build() {
2022-12-07 18:25:13 +00:00
await cargoBuild();
2023-07-28 18:03:51 +00:00
const target = Deno.args[1];
if (target != null) {
if (!TARGETS.includes(target)) {
throw new Error(`Invalid target ${target}`);
2022-12-07 17:28:51 +00:00
}
2023-07-28 18:03:51 +00:00
buildTarget(target);
2022-12-07 17:28:51 +00:00
return;
}
await Promise.all(
TARGETS.map((target) => {
return buildTarget(target);
}),
);
2022-12-07 17:28:51 +00:00
2023-03-23 14:19:26 +00:00
if (profile !== "dev") {
await Promise.all(
TARGETS.map(async (target) => {
const cmd = `wasm-opt -O4 ./${target}/loro_wasm_bg.wasm -o ./${target}/loro_wasm_bg.wasm`;
console.log(">", cmd);
await Deno.run({ cmd: cmd.split(" "), cwd: LoroWasmDir }).status();
}),
);
2023-03-23 14:19:26 +00:00
}
2022-12-07 17:28:51 +00:00
console.log(
"✅",
"Build complete in",
(performance.now() - startTime) / 1000,
"s",
);
}
2022-12-07 18:25:13 +00:00
async function cargoBuild() {
const cmd = `cargo build --target wasm32-unknown-unknown --profile ${profile}`;
2022-12-12 05:44:49 +00:00
console.log(cmd);
2022-12-08 11:45:38 +00:00
const status = await Deno.run({
2022-12-12 05:44:49 +00:00
cmd: cmd.split(" "),
2022-12-09 04:03:45 +00:00
cwd: LoroWasmDir,
2022-12-07 18:25:13 +00:00
}).status();
2022-12-08 11:45:38 +00:00
if (!status.success) {
console.log(
"❌",
"Build failed in",
(performance.now() - startTime) / 1000,
"s",
);
Deno.exit(status.code);
}
2022-12-07 18:25:13 +00:00
}
2022-12-07 17:28:51 +00:00
async function buildTarget(target: string) {
console.log("🏗️ Building target", `[${target}]`);
2022-12-09 04:03:45 +00:00
const targetDirPath = resolve(LoroWasmDir, target);
try {
await Deno.remove(targetDirPath, { recursive: true });
console.log("Clear directory " + targetDirPath);
2023-07-28 18:03:51 +00:00
} catch (_e) {
//
}
2022-12-09 04:03:45 +00:00
2023-07-30 11:08:32 +00:00
// TODO: polyfill FinalizationRegistry
const cmd = `wasm-bindgen --weak-refs --target ${target} --out-dir ${target} ../../target/wasm32-unknown-unknown/${profileDir}/loro_wasm.wasm`;
2023-03-23 14:19:26 +00:00
console.log(">", cmd);
await Deno.run({ cmd: cmd.split(" "), cwd: LoroWasmDir }).status();
2022-12-09 04:03:45 +00:00
console.log();
2023-07-28 18:03:51 +00:00
if (target === "nodejs") {
console.log("🔨 Patching nodejs target");
const patch = await Deno.readTextFile(
resolve(__dirname, "./nodejs_patch.js"),
);
const wasm = await Deno.readTextFile(
resolve(targetDirPath, "loro_wasm.js"),
);
await Deno.writeTextFile(
resolve(targetDirPath, "loro_wasm.js"),
wasm + "\n" + patch,
);
}
2022-12-07 17:28:51 +00:00
}
build();