Release Build & Cross-Compilation
ปัญหา deployment ของ Node.js
หัวข้อที่มีชื่อว่า “ปัญหา deployment ของ Node.js”เมื่อ deploy Node.js application คุณต้องส่ง Node เองไปด้วย — runtime, node_modules และ dependency ทั้งหมด ผลที่ได้มักมีขนาดหลายร้อย MB การ deploy มักหมายความว่าต้อง bundle ด้วย esbuild/webpack, เขียน Dockerfile ที่ติดตั้ง Node หรือใช้ PaaS ที่รู้จักการรัน Node
Rust compile เป็น binary ที่ self-contained เดียว ไม่มี runtime ที่ต้องส่ง, ไม่มี node_modules และไม่มี interpreter binary รันบน OS โดยตรง
# TypeScript / Node.js deployment# ต้องส่ง runtime + packages ทั้งหมดdist/ index.js # app ที่ bundle แล้วnode_modules/ # 300+ MB of dependencies# ... plus ต้องติดตั้ง Node บน server
# หรือใช้ Docker:FROM node:20-alpineCOPY package*.json ./RUN npm ci --only=productionCOPY dist/ ./dist/CMD ["node", "dist/index.js"]# Rust deployment# Build ครั้งเดียว ส่ง binarycargo build --release# → target/release/my-app (single static binary, ~5 MB)
# Copy binary ไปที่ server — แค่นั้น# ไม่มี runtime, ไม่มี package manager, ไม่มี node_modulesscp target/release/my-app user@server:/usr/local/bin/Debug vs release build
หัวข้อที่มีชื่อว่า “Debug vs release build”cargo build # debug build: compile เร็ว, ไม่ optimise, มี debug infocargo build --release # release build: compile ช้า, optimise เต็มที่ (LLVM -O3)cargo run --release # compile ใน release mode และรันDebug build ใช้ระหว่าง development — compile ใน second Release build อาจใช้เวลาหลายนาทีสำหรับ project ใหญ่แต่สร้าง binary ที่เร็วกว่าที่ runtime บ่อยถึง 10–100 เท่า
Cross-compilation
หัวข้อที่มีชื่อว่า “Cross-compilation”Rust รองรับ cross-compilation โดยตรง target triple ระบุ OS + architecture + ABI combination
# เพิ่ม cross-compilation targetrustup target add x86_64-unknown-linux-musl # Linux, static link (musl libc)rustup target add aarch64-unknown-linux-gnu # Linux, ARM64 (Raspberry Pi, AWS Graviton)rustup target add x86_64-pc-windows-gnu # Windows จาก macOS/Linux
# Compile สำหรับ target อื่นcargo build --release --target x86_64-unknown-linux-musl# → target/x86_64-unknown-linux-musl/release/my-appStatic binary ด้วย musl
หัวข้อที่มีชื่อว่า “Static binary ด้วย musl”target x86_64-unknown-linux-musl link กับ musl libc แทน glibc สร้าง static binary เต็มรูปแบบที่รันบน Linux distribution ใดก็ได้ — รวมถึง Alpine Linux container — โดยไม่มี shared library dependency
# Setup ครั้งเดียวrustup target add x86_64-unknown-linux-musl
# บน macOS ต้องมี cross-linker; ง่ายสุดผ่าน cargo-zigbuild:cargo install cargo-zigbuildcargo zigbuild --release --target x86_64-unknown-linux-musl
# ตรวจสอบ: binary ไม่มี dynamic dependencyldd target/x86_64-unknown-linux-musl/release/my-app# → not a dynamic executableOptimisation profile ใน Cargo.toml
หัวข้อที่มีชื่อว่า “Optimisation profile ใน Cargo.toml”[profile.release]opt-level = 3 # LLVM optimisation สูงสุด (default)lto = true # link-time optimisation: binary เล็กและเร็วกว่าcodegen-units = 1 # codegen unit เดียว: compile ช้ากว่า แต่ optimise ดีกว่าstrip = true # ตัด debug symbol ออกจาก binarypanic = "abort" # abort เมื่อ panic แทน unwind (binary เล็กกว่า)เหล่านี้คือ
bash/tomlconfiguration ไม่มี browser Playground สำหรับ cross-compilation — รัน command เหล่านี้ใน terminal หลังติดตั้ง Rust ผ่าน rustup.rs