Skip to content

Commit

Permalink
chore: Replace worker-build with it's individual steps (#2)
Browse files Browse the repository at this point in the history
This allows me to modify the JS side and gives more control of what and
how things get built.
  • Loading branch information
AllexVeldman authored Jun 11, 2024
1 parent f5441b4 commit 4677335
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ jobs:
deploy:
runs-on: ubuntu-latest
name: Deploy
timeout-minutes: 10
environment:
name: pyoci
url: https://pyoci.allexveldman.com

steps:
- uses: extractions/setup-just@v2
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ jobs:
test:
runs-on: ubuntu-latest
name: Test
timeout-minutes: 10

steps:
- uses: extractions/setup-just@v2
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
Expand All @@ -20,6 +22,8 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Build
run: just build

- name: Test
run: |
cargo test
run: cargo test
46 changes: 46 additions & 0 deletions js/cf_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Entrypoint for the cloudflare worker runtime.

import * as imports from "./pyoci.js";
import wasmModule from "./pyoci_bg.wasm";
import { WorkerEntrypoint } from "cloudflare:workers";

// Run the worker's initialization function.
imports.start?.();

export { wasmModule };

class Entrypoint extends WorkerEntrypoint {
async fetch(request) {
return await imports.fetch(request, this.env, this.ctx)
}

// async queue(batch) {
// return await imports.queue(batch, this.env, this.ctx)
// }
//
// async scheduled(event) {
// return await imports.scheduled(event, this.env, this.ctx)
// }
}

const EXCLUDE_EXPORT = [
"IntoUnderlyingByteSource",
"IntoUnderlyingSink",
"IntoUnderlyingSource",
"MinifyConfig",
"PolishConfig",
"R2Range",
"RequestRedirect",
"fetch",
"queue",
"scheduled",
"getMemory"
];

Object.keys(imports).map(k => {
if (!(EXCLUDE_EXPORT.includes(k) | k.startsWith("__"))) {
Entrypoint.prototype[k] = imports[k];
}
})

export default Entrypoint;
7 changes: 7 additions & 0 deletions js/pyoci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as imports from "./pyoci_bg.js";

import wkmod from "./pyoci_bg.wasm";
const instance = new WebAssembly.Instance(wkmod, {"./pyoci_bg.js": imports});
imports.__wbg_set_wasm(instance.exports);

export * from "./pyoci_bg.js";
12 changes: 10 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ cf-worker *args:
NO_MINIFY=1 npx wrangler dev --port 8090 --local-upstream localhost:8090 {{args}}

[group("dev")]
build:
echo "todo"; exit 1
install-build-dependencies:
cargo install wasm-pack

[group("dev")]
build *args: install-build-dependencies
rm -rf ./build/
wasm-pack build --no-typescript --target bundler --out-dir "build" --out-name "pyoci" {{args}}
cp -f ./js/pyoci.js ./build/
cp ./js/cf_worker.js ./build/
cd ./build && npx esbuild --external:./pyoci_bg.wasm --external:cloudflare:sockets --external:cloudflare:workers --format=esm --bundle ./cf_worker.js --outfile=cf_worker.mjs --minify

[group("curl")]
local-publish:
Expand Down
24 changes: 5 additions & 19 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
name = "pyoci"
main = "build/worker/shim.mjs"
main = "build/cf_worker.mjs"
compatibility_date = "2023-12-01"

# disable *.workers.dev
workers_dev = false
route = { pattern = "pyoci.allexveldman.nl", custom_domain = true }

# what worker-build does:
# - install wasm-pack
# - `wasm-pack build --no-typescript --target bundler --out-dir "build" --out-name "index" {args}`
# - install esbuild
# - create ./build/worker
# - move build/index_bg.js to ./build/worker/index_bg.js
# - move build/index_bg.wasm to ./build/worker/index.wasm
# - move build/snippets/ to ./build/worker/snippets/
# https://rustwasm.github.io/wasm-bindgen/reference/js-snippets.html
# - patch index_bg.js instantiate WASM module instead of importing it
# https://developers.cloudflare.com/workers/languages/rust/#javascript-plumbing-wasm-bindgen
# - include glue.js and shim.js (hard-coded in worker-build)
# - `esbuild --external:./index.wasm --externale:cloudflare:sockets --external:cloudflare:workers --format=esm --bundle ./shim.js --outfile=shim.mjs` --minify
# - remove ./build/worker/snippets/, ./build/worker/index_bg.js, shim.js, and glue.js
# these are part of the esbuild bundle
# Use env NO_MINIFY=1 to disable minification
[build]
command = "cargo install -q worker-build && worker-build --release"
# command = "cargo install -q worker-build && worker-build --release"
command = "just build --release"

[env.dev]
build = { command = "cargo install -q worker-build && worker-build --dev" }
# build = { command = "cargo install -q worker-build && worker-build --dev" }
build = { command = "just build --dev" }

0 comments on commit 4677335

Please sign in to comment.