swiftwasm /JavaScriptKit
Swift framework to interact with JavaScript through WebAssembly.
★ 993⑂ 77👁 14⑃ 665● 162▤ 95
+0
Daily
993
stars
77
forks
14
watchers
665
prs
162
issues
95
releases
README
JavaScriptKit
Swift framework to interact with JavaScript through WebAssembly.
Quick Start
Check out the Hello World tutorial for a step-by-step guide to getting started.
Overview
JavaScriptKit provides a seamless way to interact with JavaScript from Swift code when compiled to WebAssembly. It allows Swift developers to:
- Access JavaScript objects and functions
- Create closures that can be called from JavaScript
- Convert between Swift and JavaScript data types
- Use JavaScript promises with Swift's async/await
- Work with multi-threading
swift
import JavaScriptKit
// Access global JavaScript objects
let document = JSObject.global.document
// Create and manipulate DOM elements
var div = document.createElement("div")
div.innerText = "Hello from Swift!"
_ = document.body.appendChild(div)
// Handle events with Swift closures
var button = document.createElement("button")
button.innerText = "Click me"
button.onclick = .object(JSClosure { _ in
JSObject.global.alert!("Button clicked!")
return .undefined
})
_ = document.body.appendChild(button)
Learn more: JavaScript Interop Cheat Sheet
BridgeJS Plugin
Note: BridgeJS is experimental. APIs may change in future releases.
BridgeJS provides easy interoperability between Swift and JavaScript/TypeScript. It enables:
- Export Swift to JavaScript - Expose Swift functions, classes, and types to JavaScript; the plugin also generates TypeScript definitions (.d.ts) for the exported API.
- Import JavaScript into Swift - Make JavaScript/TypeScript APIs (functions, classes, globals like document or console) callable from Swift with type-safe bindings. You can declare bindings with macros in Swift or generate them from a TypeScript declaration file (bridge-js.d.ts).
For architecture details, see the BridgeJS Plugin README. For package and build setup, see Setting up BridgeJS in the documentation.
Exporting Swift to JavaScript
Mark Swift code with @JS to make it callable from JavaScript:
swift
import JavaScriptKit
@JS class Greeter {
@JS var name: String
@JS init(name: String) {
self.name = name
}
@JS func greet() - String {
return "Hello, \(name)!"
}
}
JavaScript usage:
javascript
const greeter = new exports.Greeter("World");
console.log(greeter.greet()); // "Hello, World!"
Learn more: Exporting Swift to JavaScript
Importing JavaScript into Swift
Declare bindings in Swift with macros such as @JSFunction, @JSClass, @JSGetter, and @JSSetter:
swift
import JavaScriptKit
@JSClass struct Document {
@JSFunction func getElementById(_ id: String) throws(JSException) - HTMLElement
@JSFunction func createElement(_ tagName: String) throws(JSException) - HTMLElement
}
@JSGetter(from: .global) var document: Document
@JSClass struct HTMLElement {
@JSGetter var innerText: String
@JSSetter func setInnerText(_ newValue: String) throws(JSException)
@JSFunction func appendChild(_ child: HTMLElement) throws(JSException)
}
@JS func runTop issues
#737Support exporting structs as a reference9 comments · 2026-05-01#54Avoid copying and re-encoding for String every time6 comments · 2020-09-16#512BridgeJS: SwiftSyntax's BasicFormat usage in code generators takes too much time5 comments · 2026-01-22#116Add an implementation for `JSValueEncoder`5 comments · 2021-01-06#496[BridgeJS] Support Foundation.URL as parameter/return type4 comments · 2026-01-15#781[SwiftBuild] Preserve duplicate archive members when SwiftBuild expands static archives for linking3 comments · 2026-06-23#60Improve reference behavior3 comments · 2020-09-16#692bridge-js command fails on 6.3: `no such module 'SwiftBasicFormat'`2 comments · 2026-03-05
Latest release
0.63.0 0.63.0 2026-11-04What's Changed BridgeJS: Check thrown errors before lifting return values by @krodak in https://github.com/swiftwasm/JavaScriptKit/pull/824 BridgeJS: Diagnose async and throwing @JS struct initializers by @krodak in https://github.com/swiftwasm/JavaScriptKit/pull/825 BridgeJS: Fix JSValue reference ownership across the bridge by @krodak in https://github.com/swiftwasm/JavaScriptKit/pull/826 PackageToJS: Import only the UWASI features Swift needs by @krodak in https://github.com/swiftwasm/JavaScriptKit/pull/827 Full Changelog: https://github.com/swiftwasm/JavaScriptKit/compare/0.62