You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/tutorial/getting-started/index.md
+8-6
Original file line number
Diff line number
Diff line change
@@ -5,18 +5,20 @@ title: Getting Started with Scala.js, Vite, Laminar and ScalablyTyped
5
5
6
6
This series of tutorials teaches you how to use Scala.js together with modern development tools.
7
7
8
+
1.[Scala.js and Vite](./scalajs-vite.html):
9
+
* Set up a hello world project ready for live reloading in the browser.
10
+
* Generate minimized production assets.
11
+
2.[Laminar and ScalablyTyped](./laminar-scalablytyped.html):
12
+
* Build UIs with Laminar using Function Reactive Programming (FRP), a hybrid model between imperative and functional programming particularly well suited for UI development in Scala.
13
+
* Integrate JavaScript libraries using ScalablyTyped.
14
+
8
15
If you have time, reading and applying them in order will give you more in-depth knowledge about the development environment.
9
16
10
17
If you are in a hurry, you can skip the ones you are not interested in.
11
-
Each tutorial starts with a link to repo that you can clone to get off the ground.
18
+
Each tutorial starts with a link to a repo that you can clone to get off the ground.
12
19
13
20
In any case, make sure that you have the Prerequisites listed below covered.
14
21
15
-
## Tutorials
16
-
17
-
1.[Scala.js and Vite](./scalajs-vite.html): set up a hello world project ready for live reloading in the browser.
18
-
2.[Laminar and ScalablyTyped](./laminar-scalablytyped.html): build UIs with Laminar, and integrate JavaScript libraries with ScalablyTyped.
19
-
20
22
## Prerequisites
21
23
22
24
In any case, make sure that you have the following tools installed first:
Copy file name to clipboardExpand all lines: doc/tutorial/getting-started/laminar-scalablytyped.md
+13-3
Original file line number
Diff line number
Diff line change
@@ -12,10 +12,15 @@ If you prefer to navigate the end result for this tutorial directly, checkout [t
12
12
13
13
## Prerequisites
14
14
15
-
Make sure to install [the prerequisites](./index.html) before continuing further.
15
+
Make sure to install [the prerequisites](./index.html#prerequisites) before continuing further.
16
16
17
17
## Introducing Laminar
18
18
19
+
[Laminar](https://laminar.dev/) is a Scala.js library to build UIs using Functional Reactive Programming (FRP).
20
+
FRP is a hybrid model between imperative and functional programming.
21
+
It is particularly well suited to developing UIs in Scala, as we can reason about relationships between immutable values while dealing with the changing nature of the UI.
22
+
We will elaborate on this point later.
23
+
19
24
To start off, we add a dependency on Laminar in our `build.sbt`:
20
25
21
26
{% highlight diff %}
@@ -35,7 +40,7 @@ sbt:livechart> ~fastLinkJS
35
40
[...]
36
41
{% endhighlight %}
37
42
38
-
If it was already running, stop it and `reload`it for changes in `build.sbt` to take effect.
43
+
If sbt was already running, run `reload` for the changes in `build.sbt` to take effect, then start the incremental compiler with `~fastLinkJS`.
39
44
40
45
Additionally, start Vite's development server if it wasn't already running:
41
46
@@ -44,7 +49,7 @@ $ npm run dev
44
49
[...]
45
50
{% endhighlight %}
46
51
47
-
We can now change the contents of `src/main/scala/livechart/LiveChart.scala` to use Laminar instead of raw DOM APIs.
52
+
We can now change the contents of `src/main/scala/livechart/LiveChart.scala` to use Laminar instead of vanilla DOM APIs.
48
53
49
54
At the top, we use the following import:
50
55
@@ -142,6 +147,11 @@ We then use it in two *bindings*:
142
147
We do not need to explicitly set the `innerHTML` attribute of the button.
143
148
That is taken care of by the `<--` binding.
144
149
150
+
Unlike frameworks based on a virtual DOM, Laminar bindings directly target the DOM element to update.
151
+
With a virtual DOM, when the value of `counter` changes, we would build an entirely new VDOM representation for the button (and perhaps its parents), and the framework would later diff it and identify which DOM `HTMLButtonElement` to update.
152
+
In Laminar, however, the `<--` binding remembers the precise instance of `HTMLButtonElement` to update, and directly modifies its text.
153
+
This is more efficient than going through the VDOM indirection.
154
+
145
155
Beside `:=`, the two binding arrows `<--` and `-->` are the only symbolic operators that Laminar defines.
146
156
`:=` is a static binding.
147
157
It can be seen as a `<--` with a time-immutable value on the right.
Copy file name to clipboardExpand all lines: doc/tutorial/getting-started/scalajs-vite.md
+43-9
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,8 @@ title: Getting Started with Scala.js and Vite
4
4
---
5
5
6
6
In this first tutorial, we learn how to get started with Scala.js and [Vite](https://vitejs.dev/).
7
-
We use Vite to provide live-reloading of the Scala.js application in the browser.
7
+
We use Vite to provide live-reloading of the Scala.js application in the browser for development.
8
+
We also configure it to build a minimal bundle for production.
8
9
9
10
Going through this tutorial will make sure you understand the basic building blocks.
10
11
If you prefer to skip this step and directly write Scala.js code, you may jump to [Getting Started with Scala.js and Laminar](./laminar-scalablytyped.html).
@@ -13,25 +14,25 @@ If you prefer to navigate the end result for this tutorial directly, checkout [t
13
14
14
15
## Prerequisites
15
16
16
-
Make sure to install [the prerequisites](./index.html) before continuing further.
17
+
Make sure to install [the prerequisites](./index.html#prerequisites) before continuing further.
17
18
18
19
## Vite template
19
20
20
21
We bootstrap our setup using the vanilla Vite template.
21
22
Navigate to a directory where you store projects, and run the command
22
23
23
24
{% highlight shell %}
24
-
$ npm create vite@latest
25
+
$ npm create vite@3.2.1
25
26
{% endhighlight %}
26
27
27
28
Choose a project name (we choose `livechart`).
28
29
Select the "Vanilla" framework and the "JavaScript" variant.
Note that the above is not idiomatic Scala, but rather a direct translation of the Vite template code into Scala.js.
199
200
We will see in the next tutorial how to use Laminar to write it more idiomatically.
200
201
202
+
For the most part, the Scala.js version uses straightforward Scala syntax corresponding to the original JavaScript code.
203
+
The definition of `javascriptLogo` deserves some explanation.
204
+
205
+
We translated it from the JavaScript import
206
+
207
+
{% highlight javascript %}
208
+
import javascriptLogo from "/javascript.svg"
209
+
{% endhighlight %}
210
+
211
+
which is actually a shorthand for
212
+
213
+
{% highlight javascript %}
214
+
import { default as javascriptLogo } from "/javascript.svg"
215
+
{% endhighlight %}
216
+
217
+
Many bundlers, Vite included, treat `import`s with asset files such as `.svg` as pseudo-modules whose `default` import is the *file path* to the corresponding asset in the processed bundle.
218
+
Further down, we use it as the value for the `src` attribute an `<img>` tag.
219
+
Read more about this mechanism [in the Vite documentation on static asset handling](https://vitejs.dev/guide/assets.html).
0 commit comments