• Stars
    star
    317
  • Rank 132,216 (Top 3 %)
  • Language
    Swift
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ“¦ Watcher, bundler, and test runner for your SwiftWasm apps

carton πŸ“¦

Watcher, bundler, and test runner for your SwiftWasm apps

The main goal of carton is to provide a smooth zero-config experience when developing for WebAssembly. It currently supports these features with separate commands:

  • Creating basic package boilerplate for apps built with SwiftWasm with carton init.
  • Watching the app for source code changes and reloading it in your browser with carton dev.
  • Running your XCTest suite in the full JavaScript/DOM environment with carton test.
  • Optimizing and packaging the app for distribution with carton bundle.
  • Managing SwiftWasm toolchain and SDK installations with carton sdk.

Motivation

The main motivation for carton came after having enough struggles with webpack.js, trying to make its config file work, looking for appropriate plugins. At some point the maintainers became convinced that the required use of webpack in SwiftWasm projects could limit the wider adoption of SwiftWasm itself. Hopefully, with carton you can avoid using webpack altogether. carton also simplifies a few other things in your SwiftWasm development workflow such as toolchain and SDK installations.

Getting started

Requirements

  • macOS 11 and Xcode 13.2.1 or later. macOS 10.15 may work, but is untested.
  • Swift 5.5 or later and Ubuntu 18.04 or 20.04 for Linux users.

Installation

On macOS carton can be installed with Homebrew. Make sure you have Homebrew installed and then run:

brew install swiftwasm/tap/carton

Note
If you can't install the latest carton via brew upgrade swiftwasm/tap/carton, please try rm -rf $(brew --prefix)/Library/Taps/swiftwasm/homebrew-tap/ && brew tap swiftwasm/tap and retry again. The master branch was renamed to main, so you need to update your local tap repo.

carton is also available as a Docker image for Linux. You can pull it with this command:

docker pull ghcr.io/swiftwasm/carton:latest

If Docker images are not suitable for you, you'll have to build carton from sources on Ubuntu. Clone the repository and run ./install_ubuntu_deps.sh in the root directory of the clone. After that completes successfully, run swift build -c release, the carton binary will be located in the .build/release directory after that. Unfortunately, other Linux distributions are currently not supported.

Version compatibility

carton previously embedded runtime parts of the JavaScriptKit library. This runtime allows Swift and JavaScript code to interoperate in Node.js and browser environments. Because of how JavaScriptKit runtime was embedded, older versions of JavaScriptKit were incompatible with different versions of carton and vice versa. This incompatibility between different versions was resolved starting with JavaScriptKit 0.15 and carton 0.15. All version combinations of carton and JavaScriptKit higher than those are compatible with each other.

You still have to keep in mind that older versions of SwiftWasm may be incompatible with newer carton. You can follow the compatibility matrix if you need to use older verions:

carton version SwiftWasm version JavaScriptKit version Tokamak version
0.15+ 5.6 0.15+ 0.10.1+
0.14 5.6 0.14 0.10
0.13 5.5 0.13 0.9.1
0.12.2 5.5 0.12 0.9.1
0.12.0 5.5 0.11 0.9.0
0.11.0 5.4 0.10.1 0.8.0

Usage

The carton init command initializes a new SwiftWasm project for you (similarly to swift package init) with multiple templates available at your choice. carton init --template tokamak creates a new Tokamak project, while carton init --template basic (equivalent to carton init) creates an empty SwiftWasm project with no dependencies. Also, carton init list-templates provides a complete list of templates (with only basic and tokamak available currently).

The carton dev command builds your project with the SwiftWasm toolchain and starts an HTTP server that hosts your WebAssembly executable and a corresponding JavaScript entrypoint that loads it. The app, reachable at http://127.0.0.1:8080/, will automatically open in your default web browser. The port that the development server uses can also be controlled with the --port option (or -p for short). You can edit the app source code in your favorite editor and save it, carton will immediately rebuild the app and reload all browser tabs that have the app open. You can also pass a --verbose flag to keep the build process output available, otherwise stale output is cleaned up from your terminal screen by default. If you have a custom index.html page you'd like to use when serving, pass a path to it with a --custom-index-page option.

The carton test command runs your test suite in wasmer, node or using your default browser. You can switch between these with the --environment option, passing either: wasmer, node or defaultBrowser. Code that depends on JavaScriptKit should pass either --environment node or --environment defaultBrowser options, depending on whether it needs Web APIs to work. Otherwise the test run will not succeed, since JavaScript environment is not available with --environment wasmer. If you want to run your test suite on CI or without GUI but on browser, you can pass --headless flag. It enables WebDriver-based headless browser testing. Note that you need to install a WebDriver executable in PATH before running tests. You can use the command with a prebuilt test bundle binary instead of building it in carton by passing --prebuilt-test-bundle-path <your binary path>.

The carton sdk command and its subcommands allow you to manage installed SwiftWasm toolchains, but is rarely needed, as carton dev installs the recommended version of SwiftWasm automatically. carton sdk versions lists all installed versions, and carton sdk local prints the version specified for the current project in the .swift-version file. You can however install SwiftWasm separately if needed, either by passing an archive URL to carton sdk install directly, or just specifying the snapshot version, like carton sdk install wasm-5.3-SNAPSHOT-2020-09-25-a.

carton dev can also detect existing installations of swiftenv, so if you already have SwiftWasm installed via swiftenv, you don't have to do anything on top of that to start using carton.

The carton bundle command builds your project using the release configuration (although you can pass the --debug flag to it to change that), and copies all required assets to the Bundle directory. You can then use a static file hosting (e.g. GitHub Pages) or any other server with support for static files to deploy your application. All resulting bundle files except index.html are named by their content hashes to enable cache busting. As with carton dev, a custom index.html page can be provided through the --custom-index-page option. You can also pass --debug-info flag to preserve names and DWARF sections in the resulting .wasm file, as these are stripped in the release configuration by default. By default, carton bundle will run wasm-opt on the resulting .wasm binary in order to reduce its file size. That behaviour can be disabled (in order to speed up the build) by appending the --wasm-optimizations none option.

The carton package command proxies its subcommands to swift package invocations on the currently-installed toolchain. This may be useful in situations where you'd like to generate an Xcode project file for your app with something like carton package generate-xcodeproj. It would be equivalent to swift package generate-xcodeproj, but invoked with the SwiftWasm toolchain instead of the toolchain supplied by Xcode.

All commands that delegate to swift build and swiftc (namely, dev, test, and bundle) can be passed -Xswiftc arguments, which is equivalent to -Xswiftc in swift build. All -Xswiftc arguments are propagated to swiftc itself unmodified.

All of these commands and subcommands can be passed a --help flag that prints usage info and information about all available options.

How does it work?

carton bundles a WASI polyfill, which is currently required to run any SwiftWasm code, and the JavaScriptKit runtime for convenience. carton also embeds an HTTP server for previewing your SwiftWasm app directly in a browser. The development version of the polyfill establishes a helper WebSocket connection to the server, so that it can reload development browser tabs when rebuilt binary is available. This brings the development experience closer to Xcode live previews, which you may have previously used when developing SwiftUI apps.

carton does not require any config files for these basic development scenarios, while some configuration may be supported in the future, for example for complex asset pipelines if needed. The only requirement is that your Package.swift contains at least a single executable product, which then will be compiled for WebAssembly and served when you start carton dev in the directory where Package.swift is located.

carton is built with Vapor, SwiftNIO, swift-tools-support-core, and supports both macOS and Linux. (Many thanks to everyone supporting and maintaining those projects!)

Running carton dev with the release configuration

By default carton dev will compile in the debug configuration. Add the --release flag to compile in the release configuration.

Contributing

Sponsorship

If this tool saved you any amount of time or money, please consider sponsoring the SwiftWasm organization. Or you can sponsor some of our maintainers directly on their personal sponsorship pages: @carson-katri, @kateinoigakukun, and @MaxDesiatov. While some of the sponsorship tiers give you priority support or even consulting time, any amount is appreciated and helps in maintaining the project.

Become a gold or platinum sponsor and contact maintainers to add your logo on our README on Github with a link to your site.

Coding Style

This project uses SwiftFormat and SwiftLint to enforce formatting and coding style. We encourage you to run SwiftFormat within a local clone of the repository in whatever way works best for you either manually or automatically via an Xcode extension, build phase or git pre-commit hook etc.

To guarantee that these tools run before you commit your changes on macOS, you're encouraged to run this once to set up the pre-commit hook:

brew bundle # installs SwiftLint, SwiftFormat and pre-commit
pre-commit install # installs pre-commit hook to run checks before you commit

Refer to the pre-commit documentation page for more details and installation instructions for other platforms.

SwiftFormat and SwiftLint also run on CI for every PR and thus a CI build can fail with incosistent formatting or style. We require CI builds to pass for all PRs before merging.

Code of Conduct

This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to [email protected].

More Repositories

1

JavaScriptKit

Swift framework to interact with JavaScript through WebAssembly.
Swift
583
star
2

awesome-swiftwasm

A community-driven curated list of SwiftWasm projects and content
256
star
3

WasmKit

WebAssembly Runtime written in Swift
Swift
173
star
4

swiftwasm.github.io

Website for the SwiftWasm project
JavaScript
79
star
5

WebAPIKit

Access the DOM and other Web APIs from Swift! (Very much a WIP)
Swift
60
star
6

swiftwasm-pad

swiftwasm-pad is a online playground to help developers learn about Swift on Web.
Swift
54
star
7

swiftwasm-book

The SwiftWasm book to get you started
44
star
8

WasmTransformer

A Swift package for reading, writing, and transformation of WebAssembly binaries
Swift
34
star
9

wamr-swift

Swift WebAssembly runtime powered by WAMR
Swift
31
star
10

uwasi

Micro modularized WASI runtime for JavaScript
TypeScript
30
star
11

OpenCombineJS

OpenCombine helpers for JavaScriptKit/WebAssembly APIs
Swift
30
star
12

swiftwasm-compile-service

Web service for compling Swift to Wasm, used by Try It Now on https://swiftwasm.org
JavaScript
23
star
13

WebWorkerKit

A way of running Swift Distributed Actors in their own worker "thread"
Swift
21
star
14

swiftwasm-action

GitHub Action with SwiftWasm toolchain and SDK preinstalled
Dockerfile
16
star
15

swift-web-github-example

Demo project of SwiftWasm app using webpack
Swift
14
star
16

browser-basic-template

Basic template for browser apps built with SwiftWasm
Swift
14
star
17

Gravity

Binary code size profiler for WebAssembly
Swift
13
star
18

WebFoundation

Web-based implementation of missing SwiftWasm Foundation APIs
Swift
13
star
19

swiftwasm-sdk

This repository is no longer in use, please refer to the main repo https://github.com/swiftwasm/swift
Shell
13
star
20

blog.swiftwasm.org

SwiftWasm organization blog
Swift
12
star
21

swiftwasm-package-sdk

This repository is no longer in use, please refer to the main repo https://github.com/swiftwasm/swift
Shell
8
star
22

swiftwasm-docker

Docker images for SwiftWasm
Dockerfile
8
star
23

swift-webpack-plugin

webpack plugin for Swift
JavaScript
6
star
24

size-tracker

Utility for tracking size of `.wasm` binaries produced by latest SwiftWasm snapshots
HTML
6
star
25

setup-swiftwasm

An action to download a prebuilt SwiftWasm toolchain and add it to the PATH
JavaScript
5
star
26

homebrew-tap

🍺 Homebrew Tap for SwiftWasm-related formulae
Ruby
3
star
27

swiftwasm-wasi-stubs

Stubs for getting things to link with the WASI sdk
C
3
star
28

swift-webpack-template

JavaScript
2
star
29

swift-community-ci-playbook

2
star
30

wasi-sdk-build

Build of wasi-sdk for SwiftWasm
Makefile
2
star