Quick Start: Hello World
This tutorial walks you through creating a simple web application using JavaScriptKit. You’ll learn how to set up a Swift package, add JavaScriptKit as a dependency, write code to manipulate the DOM, and build and run your web application.
JavaScriptKit allows you to interact with JavaScript APIs directly from Swift code when targeting WebAssembly, making it easy to build web applications using Swift.
Prerequisites
Visit the installation guide to install the Swift SDK for WebAssembly before starting this tutorial. This tutorial assumes you have the Swift SDK for WebAssembly installed. Please check your Swift installation.
Check your Swift toolchain version. If you see different
Console
hello-world-0-1-swift-version.txt$ swift --version Apple Swift version 6.0.3 (swift-6.0.3-RELEASE) or Swift version 6.0.3 (swift-6.0.3-RELEASE) $ swift sdk list 6.0.3-RELEASE-wasm32-unknown-wasi
Select a Swift SDK for WebAssembly version that matches the version of the Swift toolchain you have installed.
The following sections of this tutorial assume you have set the
SWIFT_SDK_ID
environment variable.Console
hello-world-0-2-select-sdk.txt$ swift --version Apple Swift version 6.0.3 (swift-6.0.3-RELEASE) or Swift version 6.0.3 (swift-6.0.3-RELEASE) $ swift sdk list 6.0.3-RELEASE-wasm32-unknown-wasi $ export SWIFT_SDK_ID=6.0.3-RELEASE-wasm32-unknown-wasi
Set up your project
Let’s start by creating a new Swift package and configuring it to use JavaScriptKit.
Create a new Swift package by running the following command in your terminal: This creates a new Swift executable package named “Hello” with a basic folder structure.
Console
hello-world-1-1-init-package.txt$ swift package init --name Hello --type executable Creating executable package: Hello Creating Package.swift Creating .gitignore Creating Sources/ Creating Sources/main.swift
Add JavaScriptKit as a dependency using the Swift Package Manager: This command adds the JavaScriptKit GitHub repository as a dependency to your package.
Console
hello-world-1-2-add-dependency.txt$ swift package init --name Hello --type executable Creating executable package: Hello Creating Package.swift Creating .gitignore Creating Sources/ Creating Sources/main.swift $ swift package add-dependency https://github.com/swiftwasm/JavaScriptKit.git --branch main Updating package manifest at Package.swift... done.
Add JavaScriptKit as a target dependency: This command adds JavaScriptKit as a target dependency to your package.
Console
hello-world-1-3-add-target-dependency.txt$ swift package init --name Hello --type executable Creating executable package: Hello Creating Package.swift Creating .gitignore Creating Sources/ Creating Sources/main.swift $ swift package add-dependency https://github.com/swiftwasm/JavaScriptKit.git --branch main Updating package manifest at Package.swift... done. $ swift package add-target-dependency --package JavaScriptKit JavaScriptKit Hello Updating package manifest at Package.swift... done.
Write your web application
Now let’s write some Swift code that manipulates the DOM to create a simple web page.
Create or modify the main.swift file in your Sources/Hello directory: This code creates a new div element, sets its text content to “Hello from Swift!”, and appends it to the document body.
main.swift
hello-world-2-1-main-swift.swiftimport JavaScriptKit let document = JSObject.global.document var div = document.createElement("div") div.innerText = "Hello from Swift!" document.body.appendChild(div)
Create an index.html file in the root of your project to load your WebAssembly application: This HTML file includes a script that loads and runs your compiled WebAssembly code.
index.html
hello-world-2-2-index-html.html<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Swift Web App</title> <script type="module"> import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js"; init(); </script> </head> <body> <h1>My Swift Web App</h1> </body> </html>
Build and run your application
Let’s build your application and run it in a web browser.
Build your application with the Swift WebAssembly toolchain: This command compiles your Swift code to WebAssembly and generates the necessary JavaScript bindings.
Console
hello-world-3-1-build.txt$ swift package --swift-sdk $SWIFT_SDK_ID js --use-cdn [37/37] Linking Hello.wasm Build of product 'Hello' complete! (5.16s) Packaging... ... Packaging finished
Start a local web server to serve your application: This starts a simple HTTP server that serves files from your current directory.
Console
hello-world-3-2-server.txt$ swift package --swift-sdk $SWIFT_SDK_ID js --use-cdn [37/37] Linking Hello.wasm Build of product 'Hello' complete! (5.16s) Packaging... ... Packaging finished $ npx serve
Open your application in a web browser: Your browser should open and display a page with “Hello from Swift!” as text added by your Swift code.
Console
hello-world-3-3-open.txt$ swift package --swift-sdk $SWIFT_SDK_ID js --use-cdn [37/37] Linking Hello.wasm Build of product 'Hello' complete! (5.16s) Packaging... ... Packaging finished $ npx serve $ open http://localhost:3000