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

69 lines
1.6 KiB
TypeScript
Raw Normal View History

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
let profile = "dev"
if (Deno.args[0] == "release") {
profile = "release"
2022-12-07 17:28:51 +00:00
}
const TARGETS = ["bundler", "web", "nodejs"];
2022-12-07 18:25:13 +00:00
const startTime = performance.now();
2022-12-07 17:28:51 +00:00
async function build() {
2022-12-07 18:25:13 +00:00
await cargoBuild();
2022-12-07 17:28:51 +00:00
if (Deno.args[1] != null) {
if (!TARGETS.includes(Deno.args[1])) {
throw new Error(`Invalid target ${Deno.args[1]}`);
}
buildTarget(Deno.args[1]);
2022-12-07 17:28:51 +00:00
return;
}
2022-12-08 12:01:06 +00:00
await Promise.all(TARGETS.map(target => {
return buildTarget(target);
}));
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() {
2022-12-08 11:45:38 +00:00
const status = await Deno.run({
cmd: `cargo build --target wasm32-unknown-unknown --profile release`
2022-12-07 18:25:13 +00:00
.split(" "),
}).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}]`);
for (const cmd of genCommands(target)) {
console.log(">", cmd);
await Deno.run({ cmd: cmd.split(" ") }).status();
}
console.log()
}
function genCommands(target: string): string[] {
return [
`rm -rf ./${target}`,
`wasm-bindgen --weak-refs --target ${target} --out-dir ${target} ../../target/wasm32-unknown-unknown/release/loro_wasm.wasm`,
2022-12-08 12:01:06 +00:00
...(profile == "dev" ? [] : [`wasm-opt -O4 ${target}/loro_wasm_bg.wasm -o ${target}/loro_wasm_bg.wasm`]),
2022-12-07 17:28:51 +00:00
];
}
2022-12-07 17:28:51 +00:00
build();