Compile a SwiftPM package to WebAssembly

You can also use SwiftPM for managing packages in the same way as other platforms.

1. Create a package from template

$ swift package init --type executable --name Example 
Creating executable package: Example
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift

2. Build the Project into a WebAssembly binary

You need to pass --triple option, which indicates that you are building for the target.

$ swift build --triple wasm32-unknown-wasi

If you installed Swift SDK instead of the whole toolchain, you need to use the following command:

$ swift build --experimental-swift-sdk <SDK name>

3. Run the produced binary

Just as in the previous section, you can run the produced binary with WebAssembly runtimes like wasmtime.

$ wasmtime ./.build/wasm32-unknown-wasi/debug/Example.wasm
Hello, world!