Skip to content

Commit 09b928a

Browse files
Wasm landing page (#5781)
1 parent 2ab89b3 commit 09b928a

File tree

8 files changed

+124
-15
lines changed

8 files changed

+124
-15
lines changed

src/_data/side-nav.yml

+2
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@
237237
permalink: /web/deployment
238238
- title: Libraries & packages
239239
permalink: /web/libraries
240+
- title: Wasm compilation
241+
permalink: /web/wasm
240242
- title: Environment declarations
241243
permalink: /guides/environment-declarations
242244

src/content/interop/js-interop/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ check out the [Past JS interop][] page for a more in depth summary on past
3535
iterations.
3636

3737
[recently]: https://medium.com/dartlang/dart-3-3-325bf2bf6c13
38-
[Wasm]: {{site.flutter-docs}}/platform-integration/web/wasm
38+
[Wasm]: /web/wasm
3939
[`package:web`]: {{site.pub-pkg}}/web
4040
[`dart:html`]: {{site.dart-api}}/dart-html/dart-html-library.html
4141
[`dart:svg`]: {{site.dart-api}}/dart-svg/dart-svg-library.html

src/content/interop/js-interop/js-types.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Usage information about the core types in JS interop.
44
---
55

66
Dart values and JS values belong to separate language domains. When compiling to
7-
Wasm, they execute in separate *runtimes* as well. As such, you should treat JS
7+
[Wasm][], they execute in separate *runtimes* as well. As such, you should treat JS
88
values as foreign types. To provide Dart types for JS values,
99
[`dart:js_interop`] exposes a set of types prefixed with `JS` called "JS types".
1010
These types are used to distinguish between Dart values and JS values at
@@ -90,7 +90,7 @@ Generally, the conversion table looks like the following:
9090
</div>
9191

9292
:::warning
93-
Compiling to JavaScript vs Wasm can introduce inconsistencies in both
93+
Compiling to JavaScript vs [Wasm][] can introduce inconsistencies in both
9494
performance and semantics for conversions. Conversions may have different costs
9595
depending on the compiler, so prefer to only convert values if you need to.
9696

@@ -233,7 +233,7 @@ error if the value returned was JS `null` or `undefined` to ensure soundness.
233233

234234
:::warning
235235
There is a subtle inconsistency with regards to `undefined` between compiling to
236-
JS and Wasm. While compiling to JS *treats* `undefined` values as if they were
236+
JS and [Wasm][]. While compiling to JS *treats* `undefined` values as if they were
237237
Dart `null`, it doesn't actually *change* the value itself. If an interop member
238238
returns `undefined` and you pass that value back into JS, JS will see
239239
`undefined`, *not* `null`, when compiling to JS.
@@ -279,4 +279,5 @@ See [`toExternalReference`] and [`toDartObject`] to convert to and from an
279279
[`JSBoxedDartObject`]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-js_interop/JSBoxedDartObject-extension-type.html
280280
[`ExternalDartReference`]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-js_interop/ExternalDartReference-extension-type.html
281281
[`toExternalReference`]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-js_interop/ObjectToExternalDartReference/toExternalReference.html
282-
[`toDartObject`]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-js_interop/ExternalDartReferenceToObject/toDartObject.html
282+
[`toDartObject`]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-js_interop/ExternalDartReferenceToObject/toDartObject.html
283+
[Wasm]: /web/wasm

src/content/interop/js-interop/package-web.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ Do we have any other package migrations to show off here?
298298
{% endcomment -%}
299299

300300
[`package:web`]: {{site.pub-pkg}}/web
301-
[Wasm]: {{site.repo.dart.sdk}}/blob/main/pkg/dart2wasm/README.md
301+
[Wasm]: /web/wasm
302302
[html]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-html/dart-html-library.html
303303
[svg]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-svg/dart-svg-library.html
304304
[`dart:js_interop`]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-js_interop/dart-js_interop-library.html

src/content/interop/js-interop/past-js-interop.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Archive of past JS interop implementations.
44
---
55

66
:::warning
7-
None of these legacy interop libraries are supported when compiling to Wasm.
7+
None of these legacy interop libraries are supported when compiling to [Wasm][].
88
:::
99

1010
This page addresses previous iterations of JS interop for Dart that are
@@ -121,3 +121,4 @@ TODO: Link to `package:web` section
121121
[`dart:js_util`]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-js_util
122122
[`Function.toJS`]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-js_interop/FunctionToJSExportedDartFunction/toJS.html
123123
[`dart:js_interop_unsafe`]: {{site.dart-api}}/{{site.sdkInfo.channel}}/dart-js_interop_unsafe
124+
[Wasm]: /web/wasm

src/content/overview.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ Dart's compiler technology lets you run code in different ways:
184184
an ahead-of-time (AOT) compiler for producing machine code.
185185

186186
* **Web platform**: For apps targeting the web, Dart can compile for
187-
development or production purposes. Its web compiler translates Dart
188-
into JavaScript.
187+
development or production purposes. Its web compilers translate Dart
188+
into JavaScript or WebAssembly.
189189

190190
<img
191191
src="/assets/img/Dart-platforms.svg"
@@ -221,26 +221,31 @@ More information:
221221
* [Write command-line apps](/tutorials/server/cmdline)
222222
* [Write HTTP servers](/tutorials/server/httpserver)
223223

224-
#### Dart Web (JavaScript dev & prod) {:#web-platform}
224+
#### Dart Web (JavaScript dev & prod and WebAssembly) {:#web-platform}
225225

226226
Dart Web enables running Dart code on web platforms powered by
227227
JavaScript. With Dart Web, you compile Dart code to JavaScript code, which in
228228
turn runs in a browser—for example, [V8](https://v8.dev/) inside
229229
[Chrome](https://www.google.com/chrome/).
230+
Alternatively, Dart code can be compiled to WebAssembly.
230231

231-
Dart web contains two compilation modes:
232+
Dart web contains three compilation modes:
232233

233-
* An incremental development compiler enabling a fast developer cycle
234-
* An optimizing production compiler which compiles Dart code to fast,
235-
compact, deployable JavaScript. These efficiencies come from
236-
techniques such as dead-code elimination.
234+
* An incremental JavaScript development compiler enabling a fast developer
235+
cycle.
236+
* An optimizing JavaScript production compiler which compiles Dart code to fast,
237+
compact, deployable JavaScript. These efficiencies come from techniques such
238+
as dead-code elimination.
239+
* An optimizing WebAssembly (WasmGC) production compiler which compiles Dart
240+
code to super-fast, deployable WebAssembly GC code.
237241

238242
More information:
239243

240244
* [Build a web app with Dart](/web/get-started)
241245
* [`dart compile js`](/tools/dart-compile#js)
242246
* [`webdev` tool](/tools/webdev)
243247
* [Web deployment tips](/web/deployment)
248+
* [WebAssembly compilation](/web/wasm)
244249

245250
#### The Dart runtime {:#runtime}
246251

src/content/tools/dart-compile.md

+8
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ The following table shows the subcommands of `dart compile`.
117117
<br><em><a href="#js">Learn more.</a></em>
118118
</td>
119119
</tr>
120+
<tr>
121+
<td> <code>wasm</code> </td>
122+
<td> WebAssembly </td>
123+
<td> A portable, binary instruction format for a stack-based virtual machine.
124+
Currently under development.
125+
<br><em><a href="/web/wasm">Learn more.</a></em>
126+
</td>
127+
</tr>
120128
</table>
121129

122130

src/content/web/wasm.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: WebAssembly (Wasm) compilation
3+
description: Learn how to compile your Dart web app to WebAssembly.
4+
---
5+
6+
The Dart team is excited to add
7+
[WebAssembly](https://webassembly.org/) as a compilation target when building
8+
Dart and [Flutter][] applications for the web.
9+
10+
Development of WebAssembly support remains ongoing,
11+
which will potentially result in frequent changes.
12+
Revisit this page for the latest updates.
13+
14+
:::note
15+
**Support for Wasm is now in stable!**
16+
17+
WebAssembly support for Dart web is available on the Dart
18+
*stable* [channel](/get-dart#release-channels).
19+
:::
20+
21+
## WebAssembly support
22+
23+
The current version of Dart compilation to WebAssembly has a number of
24+
restrictions:
25+
26+
1. It targets WebAssembly with Garbage Collection ([WasmGC][]),
27+
so not all browsers are currently supported.
28+
The current list of browsers is available in the [Flutter documentation][Flutter].
29+
30+
1. The compiled Wasm output currently targets JavaScript environments
31+
(such as browsers), and thus currently doesn't support execution in standard
32+
Wasm run-times like wasmtime and wasmer. For details, see
33+
[issue #53884]({{site.repo.dart.sdk}}/issues/53884)
34+
35+
1. Only Dart's
36+
[next-gen JS interop mechanism](/interop/js-interop/)
37+
is supported when compiling to Wasm.
38+
39+
## Compiling your web app to Wasm {:#compiling-to-wasm}
40+
41+
We've landed support in the `dart` CLI for invoking the
42+
Wasm compiler in Dart and [Flutter][]:
43+
44+
```console
45+
$ dart help compile wasm
46+
Compile Dart to a WebAssembly/WasmGC module.
47+
48+
Usage: dart compile wasm [arguments] <dart entry point>
49+
-h, --help Print this usage information.
50+
-o, --output Write the output to <file name>.
51+
This can be an absolute or relative path.
52+
-v, --verbose Print debug output during compilation
53+
--enable-asserts Enable assert statements.
54+
-D, --define=<key=value> Define an environment declaration. To specify multiple declarations, use multiple
55+
options or use commas to separate key-value pairs.
56+
For example: dart compile wasm -Da=1,b=2 main.dart
57+
```
58+
59+
Wasm compilation is available in stable, but still in preview.
60+
While we continue optimizing tooling to improve developer experience,
61+
you can try compiling Dart to Wasm today
62+
by following the temporary steps outlined here:
63+
64+
1. Start with a web app: `dart create -t web mywebapp`
65+
66+
The template creates a small web app using [`package:web`][],
67+
which is necessary to run Wasm.
68+
Make sure your web apps are [migrated][] from `dart:html` to `package:web`.
69+
70+
1. Compile with Wasm: `mywebapp$ dart compile wasm web/main.dart`
71+
72+
1. Create a new directory for the built web app: `mkdir site`
73+
74+
1. Copy over Dart code: `cp web/main.* site/`
75+
76+
1. Copy over the web files: `cp web/index.html web/styles.css site/`
77+
78+
1. Create a JS bootstrap file to load the Wasm code:
79+
80+
Add a new file `site/main.dart.js` and fill it with the contents of
81+
this [`main.dart.js` sample](https://gist.github.com/mit-mit/0fcb1247a9444b0cadf611aa5fc6f32e).
82+
83+
1. Serve the output: `dart pub global run dhttpd` ([docs][dhttpd])
84+
85+
You can also try out this small example [here](https://github.com/mit-mit/sandbox).
86+
87+
[WasmGC]: https://developer.chrome.com/blog/wasmgc/
88+
[Flutter]: {{site.flutter}}/wasm
89+
[`package:web`]: {{site.pub-pkg}}/web
90+
[`dart:js_interop`]: {{site.dart.api}}/{{site.dart.sdk.channel}}/dart-js_interop
91+
[migrated]: /interop/js-interop/package-web/
92+
[dhttpd]: {{site.pub-pkg}}/dhttpd

0 commit comments

Comments
 (0)