Contributing#
How to build, test, deploy and release SwiftBrowser itself. If you want to run your own app in the browser instead, start with docs/getting-started.md.
Local development#
You need Swift 6.4.0, its Swift SDK for WebAssembly, and Node 22. On Linux, swiftly installs the toolchain:
swiftly install 6.4.0 --use
swift sdk install \
https://download.swift.org/swift-6.4.0-release/wasm-sdk/swift-6.4.0-RELEASE/swift-6.4.0-RELEASE_wasm.artifactbundle.tar.gz \
--checksum f07b7be3c586d92d7a07051fc6d303b87ebea67eadc40640ba59d5a8b79aa86d
Then:
swift test # unit tests for the shim and reconciler (native Linux)
npm ci && npm run build:packages # the CLI, which builds the examples (and the web renderer's Node side)
scripts/install-binaryen.sh # once: wasm-opt, which shrinks the modules by about half
scripts/build-wasm.sh # builds every Examples/<App> through the CLI into Web/public/<App>.wasm
scripts/snapshot-test.sh # runs them headless (events + virtual clock, en_US) and diffs the op streams (--update to accept)
cd Web && npm run dev # serves the apps in an iPhone frame at http://localhost:5173
Each example's __snapshots__/events.txt is the script the snapshot replays,
one event per line, naming elements by what they show rather than by id:
tap:"Add", text:"Name":Paul, select:"Sort":"Rating", back. A selector
that matches nothing or more than one element fails the run
(docs/ops-protocol.md).
The dev server shows Counter by default and any other example at
?app=Todos. It watches Sources/ and Examples/, rebuilds the Wasm module
on save, and reloads the page with the previous @State values restored
where the view tree still matches (see the state snapshot section of
docs/ops-protocol.md).
scripts/build-wasm.sh Todos # one app only
Do not build the package on macOS: the shim module is named SwiftUI and
collides with the system framework. On a Mac, open the example in Xcode as
plain SwiftUI, or let CI's ios-source-compat job type-check it for you.
Native/ is a separate Swift package (SwiftBrowser Native:
the ops drawn by real SwiftUI for JavaScript apps) that does build on a Mac,
and whose protocol and tree targets build and test on Linux:
cd Native && swift test # the protocol and tree, against every example's op snapshot
cd Native/Examples/Todos && npm run build # the example's ios/app.js, then open Todos.xcodeproj
Release modules go through Binaryen's wasm-opt -Oz --strip-debug when
scripts/install-binaryen.sh has been run (CI always does): a small example
drops from 35 MB to 17.4 MB (4.5 MB with brotli). The dev server's hot
rebuilds skip the step to stay fast and keep function names in stack traces.
Deploying the examples#
The deploy job of .github/workflows/ci.yml
publishes the web renderer with every example to Cloudflare Pages, so the apps
can be opened in a real mobile browser. It takes the modules the examples
matrix built, after the optimize matrix ran wasm-opt on them, so it
compiles no Swift. Apple's Backyard Birds
joins them: the examples matrix also builds it, unchanged from its
repository at a pinned commit (SB_APP_PATH in scripts/build-wasm.sh), and
uploads the site the CLI built for it beside the module; the deploy gives that
site the optimized module and merges it into the examples' (SB_SITES,
Web/plugins/included-sites.ts), so the landing page lists it with the
examples. Pushes to main deploy the production site;
every pull request gets a preview deployment on a branch alias and a comment
with the links. The deployed site has a landing page at / that lists the
apps with QR codes, and runs each app at /app/?app=<Name>. On a phone the
app page drops the fake iPhone frame and uses the real screen and safe areas;
in Safari, Share → Add to Home Screen runs it full screen.
The job needs two repository secrets (Settings → Secrets and variables → Actions); without them it still builds the site, and skips the deploy:
| Secret | Value |
|---|---|
CLOUDFLARE_API_TOKEN |
an API token created at dash.cloudflare.com → My Profile → API Tokens with the Cloudflare Pages: Edit permission |
CLOUDFLARE_ACCOUNT_ID |
the account id from the dashboard URL (dash.cloudflare.com/<account id>) |
The Pages project is named swiftbrowser (CLOUDFLARE_PAGES_PROJECT in the
workflow); the workflow creates it on the first run if it does not exist. The site is
then at https://swiftbrowser.pages.dev, and previews at
https://<branch>.swiftbrowser.pages.dev.
Deploying Ice Cubes#
.github/workflows/icecubes.yml builds
Ice Cubes, unmodified, with the CLI
and deploys it to the Cloudflare Pages project swiftbrowser-icecubes, so it
can be opened at https://swiftbrowser-icecubes.pages.dev (a site built from
one app is that app: it fills the window, with the regular-width layout in a
desktop browser).
- When it runs. Pushes to
maindeploy, and so can a manual run, which takes an Ice Cubes commit, tag or branch. Pull requests that change the workflow build without deploying. The build is long (the app's packages, thenwasm-opton one thread), so it is not part of CI. - Credentials. It uses the same secrets as the examples' deploy.
- TelemetryDeck. The app's TelemetryDeck signals belong to its author, so
the build blocks
nom.telemetrydeck.com(--block-host): those requests fail as if offline, and the app works on. - Images. To load images from servers that do not allow the page's
origin, deploy the CORS proxy in
packages/cors-proxyand set the repository variableICECUBES_CORS_PROXYto its URL. - The module's size. Ice Cubes' module is about 31 MB after
wasm-opt, over Pages' 25 MiB-per-file limit. The site build therefore publishes it as parts with a manifest, and the page joins them (Web/plugins/swift-wasm.ts).
Deploying the docs#
The documentation site in DocsSite/ is built from the
Markdown in this repository (the root README, docs/, the package READMEs and
this file) by .github/workflows/docs.yml,
which deploys it to the Cloudflare Pages project swiftbrowser-docs
(https://swiftbrowser-docs.pages.dev) with the same two secrets. Edit the
Markdown, not the site; cd DocsSite && npm install && npm run dev previews
it at http://localhost:5174.
Releasing to npm#
.github/workflows/publish.yml publishes the
packages (@swiftbrowser/cli, @swiftbrowser/web, @swiftbrowser/xcodeproj,
@swiftbrowser/swift, @swiftbrowser/kit, @swiftbrowser/wasm-tools,
@swiftbrowser/wasm-compiler, @swiftbrowser/react, @swiftbrowser/native,
@swiftbrowser/code, @swiftbrowser/shader, @swiftbrowser/reality,
@swiftbrowser/raster (its Rust rasterizer built for WebAssembly by the
run), and the Swift SDK for WebAssembly's packages
@swiftbrowser/wasm-sdk, -foundation, -icu, -icu-data, -testing and
-cxx) together,
at one version, with npm provenance while the
repository is public (npm rejects provenance from a private one, so a private
repository publishes without it). Start it
from Actions → Publish to npm → Run workflow with the version to release
(0.2.0, or patch / minor / major). The run bumps every manifest and the
lockfile (npm run set-version <version> does the same locally), runs the
typecheck and unit tests, builds, commits Release vX.Y.Z to the branch it
ran on, tags it, publishes the packages in dependency order and creates the
GitHub release with generated notes. Before publishing it builds the kit
with its macros as WebAssembly (swiftbrowser kit --wasm-macros, then
packages/kit/pack-kit.mjs into packages/kit); the macros serve every
host, a native toolchain running them through @swiftbrowser/wasm-compiler's
launcher, so there are no per-host packages to build. It also fills the
SDK packages from the Swift SDK it installs (packages/wasm-sdk/pack-sdk.mjs,
the split in packages/cli/src/sdkPackages.ts). Tick dry run to do all of that except
the push and the publish.
The slow parts run side by side: the typecheck and unit tests, the kit, and
SwiftBrowser Native's module for WebAssembly are jobs of their own, and the
packages go to the registry in parallel, the CLI last since it pins the
others. The kit and the module are cached by what they are built from
(scripts/release-cache-key.sh: the Swift sources, the scripts that build
them, the Swift version and the SDK, but not the version), so a release that
changes no Swift skips both builds, about twenty minutes. Pushing a vX.Y.Z tag by hand also publishes, if
the manifests already carry that version.
@swiftbrowser/dsp is not published, and a release cannot publish it by
accident: its manifest is "private": true, which npm refuses, and
scripts/release-packages.mjs stops the run before anything is built if the
list names it, if a package in the list depends on it (or on any package of
this repository the release does not publish), or if it is no longer
private. Publishing it one day is a change to that script's HELD_BACK, its
manifest and the workflow's PACKAGES, after what
its README lists ("Before this package is
published").
Publishing authenticates in one of two ways:
| Option | Setup |
|---|---|
NPM_TOKEN secret |
Settings → Secrets and variables → Actions: a granular access token with read and write access to the @swiftbrowser packages, allowed to bypass 2FA |
| Trusted publishing | On npmjs.com, each package's Settings → Trusted Publisher: this repository, workflow publish.yml. No secret; the workflow's OIDC token is enough |
@swiftbrowser/wasm-tools carries the Swift compiler and linker built as
WebAssembly, too big for the repository: the job downloads them from
Cloudflare R2 (packages/wasm-tools/fetch.mjs), so the CLOUDFLARE_API_TOKEN
secret needs R2 read access as well as its Pages permission, next to
CLOUDFLARE_ACCOUNT_ID. Trusted publishing is configured per package on
npmjs.com, which needs the package to exist, so its first release goes out
with NPM_TOKEN, which then needs write access to the whole @swiftbrowser
scope rather than to a list of packages.
A release that failed half-way is resumed by running the workflow again with the same version: the job publishes from the commit the existing tag points at, packages already on the registry are skipped and an existing release is reused.
Continuous integration#
.github/workflows/ci.yml runs these jobs on every
push and pull request:
swift-tests: native Linux build andswift test.native:native.yml, called from CI and runnable on its own for a branch (Actions → Native → Run workflow): the SwiftBrowser Native package's protocol and tree targets built and tested on Linux (cd Native && swift test), and on macOS the whole package for the iOS simulator, its tests, and the Counter example's Xcode project with its bundle built. To try a change to an optional native package or an example app against Apple's SDKs without the rest of it, runnative-quick.ymlby hand (Actions → Native quick build): one package underNative/Packagesand one app underNative/Examples(SwiftBrowserReality and the Showcase by default), built past their errors, everyerror:line gathered in the run's summary.hello-wasm: buildsHelloWasmfor Wasm and runs it in wasmtime, a canary for the SDK install.shim: compiles the SwiftBrowser package for Wasm once, as the CLI's kit (swiftbrowser kit: every module and the archive of its objects, see packages/cli/README.md). The kit is cached by its inputs: a run whose inputs were built before skips the job, and any other run starts the kit's SwiftPM scratch path from the latest one (main's, on a pull request), so SwiftPM recompiles only the modules whose sources changed.examples: a matrix with one runner per example (the list is in the workflow; thewasmjob fails when anExamples/directory is missing from it), each restoring theshimjob's kit, building its module through the CLI unoptimized and uploading it. A module built from the same inputs is restored instead of built. Apple's Backyard Birds is one more runner, built from its own repository, which also uploads its site.optimize: one runner per example runswasm-opton its module (scripts/optimize-wasm.mjs, the CLI's own step) for the deploy, beside the tests, which run on the unoptimized modules. An optimized module is cached by the module it came from. Main only: a pull request's preview deploys the unoptimized modules.wasm: downloads the unoptimized modules, runs every example's headless snapshot under Node's WASI, type-checks and unit-tests the workspaces, builds the web renderer and runs the Playwright tests against the real modules in Chromium. Needs no Swift.deploy: builds the site from the optimized modules and the sites of the apps from other repositories, and publishes it to Cloudflare Pages (see "Deploying the examples").cli: the CLI against Hacking with SwiftUI's Xcode projects, andcheckon an app fresh frominit. Its WebAssembly builds use theshimjob's kit; for the nativechecks each shard starts from its scratch path of main's latest run (saved on main only). A pull request runs thechecksshard; the two headless shards run on main.ios-source-compat: type-checks every example against Apple's SwiftUI with the iOS simulator SDK on macOS, and builds an app fresh frominitwithxcodebuild, the only place Xcode builds a project here.changes: the files a pull request touches, which decide whether the macOS jobs run for it (a macOS minute bills as ten Linux ones):ios-source-compatforExamples/orpackages/cli/, and native.yml's macOS and iOS Simulator jobs forNative/,packages/native/,packages/react/or the protocol. On main every job runs.
.github/workflows/ios-fidelity.yml
compares the browser with iOS (see "Comparing with iOS").
.github/workflows/docs.yml runs when Markdown
or DocsSite/ changes: it type-checks, tests and builds the docs site, and
deploys it (see "Deploying the docs").
Comparing with iOS#
Examples/Fidelity is a set of reference screens
(navigation, lists, forms, controls, text styles, shapes, layout, a sheet,
an alert, search), each opened directly with the launch argument
-screen <name> (Examples/Fidelity/screens.txt lists them). The
iOS fidelity workflow screenshots every
screen in the iOS Simulator on macOS (scripts/ios-screenshots.sh: the app
compiled with swiftc by the runner's newest Xcode, on an iPhone 16 with that
Xcode's iOS and the status bar at 9:41) and in the
browser's iPhone frame at the same size (scripts/fidelity-screens.mjs,
?arg=-screen&arg=<name>), each in light and dark appearance
(<name>.png, <name>-dark.png), then puts each pair side by side with the
pixels that differ. It only runs by hand, from Actions → iOS fidelity → Run
workflow with the branch to compare (the macOS runner is costly, so pull requests
do not trigger it). The fidelity-comparison
artifact has the images and an index.html to page through them, and the
run's summary the share of differing pixels per screen and appearance. Fonts differ by
design (SF Pro is not on the page), so the numbers are for watching
regressions and progress, not for reaching zero.
The same workflow screenshots SwiftBrowser Native's
Fidelity example (Native/Examples/Fidelity, the React Fidelity app drawn by
real SwiftUI) on the same simulator with scripts/native-screenshots.sh, and
compares it with the Swift app the same way (the fidelity-native-comparison
artifact, --label native). Both sides are SwiftUI with SF Pro there, so
its numbers can reach zero; every differing pixel is a mapping bug.
Locally, on a Mac: scripts/ios-screenshots.sh writes .build/ios-screens;
with the site running (scripts/build-wasm.sh Fidelity, then serve
.build/examples/Fidelity), node scripts/fidelity-screens.mjs --base <url> --ios .build/ios-screens --out .build/fidelity takes the browser's and
compares.
Comparing with the in-memory renderer#
scripts/raster-compare.mjs compares the browser with
@swiftbrowser/raster the same way, on the
React Fidelity screens (packages/react/demo/fidelity): with the React demo
running (cd packages/react && npm run dev) and the module built
(npm run build:wasm --workspace packages/raster, a Rust toolchain),
node scripts/raster-compare.mjs --out .build/raster-fidelity screenshots
each screen in Playwright's Chromium, draws the same React tree's ops with
the renderer in Node, and writes the side-by-side images and a summary with
the share of differing pixels (the numbers are in
docs/raster.md). Both sides draw with Inter, so these can
reach zero: a differing pixel is a painting rule the renderer gets wrong.
Layout#
Sources/SwiftUI/ the shim: API/ (public SwiftUI surface) and Runtime/ (state, reconciler, ops)
Sources/HelloWasm/ smallest possible Swift-on-Wasm program
Examples/Counter/ the first app and its headless snapshot
Examples/Todos/ navigation, lists, input and @Observable in one app
Examples/Gallery/ layout, animation, sheets and typography screens
Examples/Settings/ tabs, forms, pickers, path navigation, tasks and GeometryReader
Examples/FoodTruck/ Apple's Food Truck sample, trimmed: Foundation, asset catalogs, ObservableObject
Sources/FoundationBridge/ what apps need from Foundation that corelibs lacks on WASI: Timer, UserDefaults, Bundle, URLSession, AsyncImage, Locale
Sources/CoreImage/ Core Image's built-in filters and QR code generator, as recipes the page draws
Examples/HackingWithSwift/ five Hacking with SwiftUI projects: gestures, swipe actions, edit mode, alerts, controls
Examples/CupcakeCorner/ Hacking with SwiftUI project 10: AsyncImage and URLSession over the browser's fetch
Examples/PhotoPicker/ PhotosUI's PhotosPicker over the browser's file chooser
Examples/Filters/ Core Image: Instafilter's filters on a photo, Hot Prospects' QR code
Examples/HotProspects/ Hacking with SwiftUI project 16 (no Me tab): the real CodeScanner over capture sessions, UserNotifications
Web/ Vite + TypeScript renderer and device frame
packages/react/ @swiftbrowser/react: SwiftUI's components for React, rendered by Web/ (a react-reconciler host that emits the ops)
Native/ SwiftBrowser Native: the ops drawn by real SwiftUI for JavaScript apps in JavaScriptCore (a separate Swift package; docs/native.md)
Native/Examples/ Counter, Todos and Fidelity as @swiftbrowser/react apps with Xcode projects (generate-projects.mjs writes them from the CLI's native template)
packages/native/ @swiftbrowser/native: run(), the web globals over the native bridge, the app.js bundler, and the Native/ Swift package (synced in at pack time)
packages/raster/ @swiftbrowser/raster: the in-memory renderer, the ops drawn to a PNG by Web/src/layout and a Rust rasterizer (crate/) compiled to WebAssembly, a session that drives an app, and swiftbrowser-shot (cli/: a React app in QuickJS, driven by a script) (docs/raster.md)
DocsSite/ the documentation site, built from the repository's Markdown
Tests/SwiftUITests/ swift-testing suite for the shim
scripts/ build-wasm.sh, snapshot-test.sh, run-wasi.mjs, set-version.mjs (release versions)
docs/ getting started, compatibility, architecture, the ops protocol and the history