CraigglesO/workers-zig
Write Cloudflare Workers in Zig via WebAssembly
Workers Zig is a light weight Zig bindings for the Cloudflare Workers environment via WebAssembly.
Why Zig?
5.8Kb
of WASM code and 6.8Kb
javascript code.Be sure to read the Documentation for guidance on usage.
env
Follow the instructions to install Zig
Release used: 0.10.0-dev.3838+77f31ebbb
Follow the steps provided by the skeleton project
# in one go
git clone --recurse-submodules -j8 [email protected]:CraigglesO/worker-zig-template.git
# OR
# clone
git clone [email protected]:CraigglesO/worker-zig-template.git
# enter
cd worker-zig-template
# Pull in the submodule
git submodule update --init --recursive
# NPM
npm install --save workers-zig
# Yarn
yarn add workers-zig
# PNPM
pnpm add workers-zig
# BUN
bun add workers-zig
git submodule add https://github.com/CraigglesO/workers-zig
const std = @import("std");
const Builder = std.build.Builder;
pub fn build(b: *Builder) void {
b.is_release = true;
b.cache_root = "cache";
b.global_cache_root = "cache";
b.use_stage1 = true;
const wasm_build = b.addSharedLibrary("zig", "lib/main.zig", .unversioned);
wasm_build.setOutputDir("dist");
wasm_build.setTarget(std.zig.CrossTarget {
.cpu_arch = .wasm32,
.os_tag = .freestanding,
});
wasm_build.build_mode = std.builtin.Mode.ReleaseSmall;
wasm_build.strip = true;
wasm_build.linkage = std.build.LibExeObjStep.Linkage.dynamic;
wasm_build.addPackagePath("workers-zig", "workers-zig/lib/main.zig");
wasm_build.install();
}
name = "zig-worker-template"
main = "dist/worker.mjs"
compatibility_date = "2022-07-29"
usage_model = "bundled" # or unbound
account_id = ""
[build]
command = "zig build && npm run esbuild"
watch_dir = [
"src",
"lib"
]
[[build.upload.rules]]
type = "CompiledWasm"
globs = ["**/*.wasm"]
[[build.upload.rules]]
type = "ESModule"
globs = ["**/*.mjs"]