Skip to content

Commit 766d2e1

Browse files
authored
Update toolchain version, script, and README.md (swiftwasm#96)
The toolchain snapshot suggested in `README.md` is outdated. I think we should recommend using the script to install it automatically. Resolves swiftwasm#95. * Mention the `install-toolchain.sh` script in `README.md` * Update toolchain version, script, and `README.md` * Update example code in `README.md` * Update .swift-version * Update Swift version in README.md
2 parents 06a828e + 3254c81 commit 766d2e1

File tree

3 files changed

+85
-28
lines changed

3 files changed

+85
-28
lines changed

.swift-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
wasm-5.3-SNAPSHOT-2020-10-02-a
1+
wasm-5.3-SNAPSHOT-2020-10-20-a

README.md

+81-24
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,7 @@
44

55
Swift framework to interact with JavaScript through WebAssembly.
66

7-
## Requirements
8-
9-
This library only supports [`swiftwasm/swift`](https://github.com/swiftwasm/swift) distribution toolchain. Please install Swift for WebAssembly toolchain from [Release Page](https://github.com/swiftwasm/swift/releases)
10-
11-
The toolchains can be installed via [`swiftenv`](https://github.com/kylef/swiftenv) like official nightly toolchain.
12-
13-
e.g.
14-
15-
```sh
16-
17-
$ swiftenv install https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.3-SNAPSHOT-2020-08-10-a/swift-wasm-5.3-SNAPSHOT-2020-08-10-a-osx.tar.gz
18-
$ swift --version
19-
Swift version 5.3-dev (LLVM 09686f232a, Swift 5a196c7f13)
20-
Target: x86_64-apple-darwin19.6.0
21-
```
22-
23-
## Usage
7+
## Getting started
248

259
This JavaScript code
2610

@@ -48,13 +32,11 @@ Can be written in Swift using JavaScriptKit
4832
```swift
4933
import JavaScriptKit
5034

51-
let alert = JSObject.global.alert.function!
52-
let document = JSObject.global.document.object!
35+
let document = JSObject.global.document
5336

54-
let divElement = document.createElement!("div").object!
37+
let divElement = document.createElement("div")
5538
divElement.innerText = "Hello, world"
56-
let body = document.body.object!
57-
_ = body.appendChild!(divElement)
39+
_ = document.body.appendChild(divElement)
5840

5941
struct Owner: Codable {
6042
let name: String
@@ -68,7 +50,82 @@ struct Pet: Codable {
6850
let jsPet = JSObject.global.pet
6951
let swiftPet: Pet = try JSValueDecoder().decode(from: jsPet)
7052

71-
alert("Swift is running on browser!")
53+
JSObject.global.alert("Swift is running in the browser!")
54+
```
55+
56+
### Usage in a browser application
57+
58+
The easiest way to get started with JavaScriptKit in your browser app is with [the `carton`
59+
bundler](https://carton.dev).
60+
61+
As a part of these steps
62+
you'll install `carton` via [Homebrew](https://brew.sh/) on macOS (unfortunately you'll have to build
63+
it manually on Linux). Assuming you already have Homebrew installed, you can create a new app
64+
that uses JavaScriptKit by following these steps:
65+
66+
1. Install `carton`:
67+
68+
```
69+
brew install swiftwasm/tap/carton
70+
```
71+
72+
If you had `carton` installed before this, make sure you have version 0.6.1 or greater:
73+
74+
```
75+
carton --version
7276
```
7377

74-
Please see [Example](https://github.com/swiftwasm/JavaScriptKit/tree/main/Example) directory for more information
78+
2. Create a directory for your project and make it current:
79+
80+
```
81+
mkdir SwiftWasmApp && cd SwiftWasmApp
82+
```
83+
84+
3. Initialize the project from a template with `carton`:
85+
86+
```
87+
carton init --template basic
88+
```
89+
90+
4. Build the project and start the development server, `carton dev` can be kept running
91+
during development:
92+
93+
```
94+
carton dev
95+
```
96+
97+
5. Open [http://127.0.0.1:8080/](http://127.0.0.1:8080/) in your browser and a developer console
98+
within it. You'll see `Hello, world!` output in the console. You can edit the app source code in
99+
your favorite editor and save it, `carton` will immediately rebuild the app and reload all
100+
browser tabs that have the app open.
101+
102+
You can also build your project with webpack.js and a manually installed SwiftWasm toolchain. Please
103+
see the following sections and the [Example](https://github.com/swiftwasm/JavaScriptKit/tree/main/Example)
104+
directory for more information in this more advanced use case.
105+
106+
### Manual toolchain installation
107+
108+
This library only supports [`swiftwasm/swift`](https://github.com/swiftwasm/swift) distribution
109+
toolchain. The toolchain can be installed via [`swiftenv`](https://github.com/kylef/swiftenv), in
110+
the same way as the official Swift nightly toolchain.
111+
112+
You have to install the toolchain manually when working on the source code JavaScriptKit itself,
113+
especially if you change anything in the JavaScript runtime parts. This is because the runtime is
114+
embedded in `carton` and currently can't be replaced dynamically with the JavaScript code you've
115+
updated locally.
116+
117+
Just pass a toolchain archive URL for [the latest SwiftWasm 5.3
118+
snapshot](https://github.com/swiftwasm/swift/releases) appropriate for your platform:
119+
120+
```sh
121+
$ swiftenv install https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.3-SNAPSHOT-2020-10-20-a/swift-wasm-5.3-SNAPSHOT-2020-10-20-a-macos_x86_64.pkg
122+
```
123+
124+
You can also use the `install-toolchain.sh` helper script that uses a hardcoded toolchain snapshot:
125+
126+
```sh
127+
$ ./scripts/install-toolchain.sh
128+
$ swift --version
129+
Swift version 5.3 (swiftlang-5.3.0)
130+
Target: x86_64-apple-darwin19.6.0
131+
```

scripts/install-toolchain.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ fi
1818

1919
case $(uname -s) in
2020
Darwin)
21-
toolchain_download="$swift_tag-osx.tar.gz"
21+
toolchain_download="$swift_tag-macos_x86_64.pkg"
2222
;;
2323
Linux)
2424
if [ $(grep RELEASE /etc/lsb-release) == "DISTRIB_RELEASE=18.04" ]; then
25-
toolchain_download="$swift_tag-ubuntu18.04.tar.gz"
25+
toolchain_download="$swift_tag-ubuntu18.04_x86_64.tar.gz"
2626
elif [ $(grep RELEASE /etc/lsb-release) == "DISTRIB_RELEASE=20.04" ]; then
27-
toolchain_download="$swift_tag-ubuntu20.04.tar.gz"
27+
toolchain_download="$swift_tag-ubuntu20.04_x86_64.tar.gz"
2828
else
2929
echo "Unknown Ubuntu version"
3030
exit 1

0 commit comments

Comments
 (0)