@@ -22,17 +22,17 @@ We bootstrap our setup using the vanilla Vite template.
22
22
Navigate to a directory where you store projects, and run the command
23
23
24
24
{% highlight shell %}
25
- $ npm create vite@4.1 .0
25
+ $ npm create vite@6.5 .0
26
26
{% endhighlight %}
27
27
28
28
Choose a project name (we choose ` livechart ` ).
29
29
Select the "Vanilla" framework and the "JavaScript" variant.
30
30
Our output gives:
31
31
32
32
{% highlight shell %}
33
- $ npm create vite@4.1 .0
33
+ $ npm create vite@6.5 .0
34
34
Need to install the following packages:
35
- create-vite@4.1 .0
35
+ create-vite@6.5 .0
36
36
Ok to proceed? (y)
37
37
✔ Project name: … livechart
38
38
✔ Select a framework: › Vanilla
@@ -55,11 +55,11 @@ $ npm install
55
55
[ ...]
56
56
$ npm run dev
57
57
58
- VITE v4.1.4 ready in 156 ms
58
+ VITE v6.3.5 ready in 156 ms
59
59
60
60
➜ Local: http://localhost:5173/
61
61
➜ Network: use --host to expose
62
- ➜ press h to show help
62
+ ➜ press h + enter to show help
63
63
{% endhighlight %}
64
64
65
65
Open the provided URL to see the running JavaScript-based hello world.
@@ -69,12 +69,12 @@ Open the provided URL to see the running JavaScript-based hello world.
69
69
In the generated folder, we find the following relevant files:
70
70
71
71
* ` index.html ` : the main web page; it contains a ` <script type=module src="/main.js"> ` referencing the main JavaScript entry point.
72
- * ` main.js ` : the main JavaScript entry point; it sets up some DOM elements, and sets up a counter for a button.
73
- * ` counter.js ` : it implements a counter functionality for a button.
72
+ * ` /src/ main.js` : the main JavaScript entry point; it sets up some DOM elements, and sets up a counter for a button.
73
+ * ` /src/ counter.js` : it implements a counter functionality for a button.
74
74
* ` package.json ` : the config file for ` npm ` , the JavaScript package manager and build orchestrator.
75
75
76
76
Remarkably, there is no ` vite.config.js ` file, which would be the configuration for Vite itself.
77
- Vite gives a decent experience out of the box, without any configuration.
77
+ Vite gives a decent experience out of the box, without any configuration, but we will need one soon enough .
78
78
79
79
### Live changes
80
80
@@ -99,12 +99,12 @@ Observe that the page automatically and instantaneously refreshes to show the ch
99
99
We use [ sbt] ( https://www.scala-sbt.org/ ) as a build tool for Scala and Scala.js.
100
100
We set it up as follows.
101
101
102
- In the subdirectory ` livechart/ project/` , we add two files: ` build.properties ` and ` plugins.sbt ` .
102
+ Create the subdirectory ` project/ ` , and add two files: ` build.properties ` and ` plugins.sbt ` .
103
103
104
104
* ` project/build.properties ` : set the version of sbt
105
105
106
106
{% highlight plaintext %}
107
- sbt.version=1.10.0
107
+ sbt.version=1.11.3
108
108
{% endhighlight %}
109
109
110
110
* ` project/plugins.sbt ` : declare sbt plugins; in this case, only sbt-scalajs
@@ -113,7 +113,7 @@ sbt.version=1.10.0
113
113
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "{{ site.versions.scalaJS }}")
114
114
{% endhighlight %}
115
115
116
- At the root of our ` livechart/ ` project, we add one file: ` build.sbt ` .
116
+ At the root of our project, we add one file: ` build.sbt ` .
117
117
118
118
* ` build.sbt ` : the main sbt build
119
119
@@ -123,7 +123,7 @@ import org.scalajs.linker.interface.ModuleSplitStyle
123
123
lazy val livechart = project.in(file("."))
124
124
.enablePlugins(ScalaJSPlugin) // Enable the Scala.js plugin in this project
125
125
.settings(
126
- scalaVersion := "3.3.3 ",
126
+ scalaVersion := "3.7.1 ",
127
127
128
128
// Tell Scala.js that this is an application with a main method
129
129
scalaJSUseMainModuleInitializer := true,
@@ -158,8 +158,8 @@ import scala.scalajs.js.annotation.*
158
158
159
159
import org.scalajs.dom
160
160
161
- // import javascriptLogo from "/javascript.svg"
162
- @js .native @JSImport ("/javascript.svg", JSImport.Default)
161
+ // import javascriptLogo from "/src/ javascript.svg"
162
+ @js .native @JSImport ("/src/ javascript.svg", JSImport.Default)
163
163
val javascriptLogo: String = js.native
164
164
165
165
@main
@@ -206,13 +206,13 @@ The definition of `javascriptLogo` deserves some explanation.
206
206
We translated it from the JavaScript import
207
207
208
208
{% highlight javascript %}
209
- import javascriptLogo from "/javascript.svg"
209
+ import javascriptLogo from "/src/ javascript.svg"
210
210
{% endhighlight %}
211
211
212
212
which is actually a shorthand for
213
213
214
214
{% highlight javascript %}
215
- import { default as javascriptLogo } from "/javascript.svg"
215
+ import { default as javascriptLogo } from "/src/ javascript.svg"
216
216
{% endhighlight %}
217
217
218
218
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.
@@ -222,12 +222,12 @@ Read more about this mechanism [in the Vite documentation on static asset handli
222
222
The translation in Scala.js reads as
223
223
224
224
{% highlight scala %}
225
- @js .native @JSImport ("/javascript.svg", JSImport.Default)
225
+ @js .native @JSImport ("/src/ javascript.svg", JSImport.Default)
226
226
val javascriptLogo: String = js.native
227
227
{% endhighlight %}
228
228
229
229
The ` @js.native ` annotation tells Scala.js that ` javascriptLogo ` is provided externally by JavaScript.
230
- The ` @JSImport("/javascript.svg", JSImport.Default) ` is the translation of the ` default ` import from the ` /javascript.svg ` pseudo-module.
230
+ The ` @JSImport("/src/ javascript.svg", JSImport.Default) ` is the translation of the ` default ` import from the ` /src /javascript.svg` pseudo-module.
231
231
Since it represents a file path, we declare ` javascriptLogo ` as a ` String ` .
232
232
233
233
The ` = js.native ` is a Scala.js idiosyncrasy: we need a concrete value to satisfy the Scala typechecker.
@@ -285,7 +285,7 @@ Let us change the message to
285
285
286
286
{% highlight diff %}
287
287
<a href =" https://developer.mozilla.org/en-US/docs/Web/JavaScript " target =" _blank " >
288
- <img src =" /javascript.svg " class =" logo vanilla " alt =" JavaScript logo " />
288
+ <img src =" /src/ javascript.svg " class =" logo vanilla " alt =" JavaScript logo " />
289
289
</a >
290
290
- <h1>Hello Scala.js!</h1>
291
291
+ <h1>Hello Scala.js and Vite!</h1>
@@ -319,7 +319,7 @@ $ npm run build
319
319
320
320
> vite build
321
321
322
- vite v4.1.4 building for production...
322
+ vite v6.3.5 building for production...
323
323
[ info] welcome to sbt 1.8.0 (Temurin Java 1.8.0_362)
324
324
[ ...]
325
325
[ info] Full optimizing .../livechart/target/scala-3.2.2/livechart-opt
0 commit comments