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
Finally, we write the following content in the file `src/main/scala/livechart/LiveChart.scala`:
138
+
Mill will expect to find a subdirectory called livechart at `livechart/livechart` because that is what we named the `AppScalaJsModule` in our `build.mill`. And finally, we write the following content in the file `livechart/livechart/src/LiveChart.scala`:
152
139
153
140
{% highlight scala %}
154
141
package livechart
@@ -233,33 +220,35 @@ Since it represents a file path, we declare `javascriptLogo` as a `String`.
233
220
The `= js.native` is a Scala.js idiosyncrasy: we need a concrete value to satisfy the Scala typechecker.
234
221
In an ideal world, it would not be required.
235
222
236
-
We can now build the Scala.js project by opening a new console, and entering sbt:
223
+
We can now build the Scala.js project by opening a new console, and running the following Mill command:
237
224
238
225
{% highlight shell %}
239
-
$ sbt
226
+
$ mill -w livechart.fastLinkJS
240
227
[...]
241
-
sbt:livechart> ~fastLinkJS
228
+
Watching for changes to 12 paths and 11 other values... (Enter to re-run, Ctrl-C to exit)
242
229
{% endhighlight %}
243
230
244
231
The `fastLinkJS` task produces the `.js` outputs from the Scala.js command.
245
-
The `~` prefix instructs sbt to re-run that task every time a source file changes.
232
+
The `-w` command line parameter instructs Mill to re-run that task every time a source file changes.
246
233
247
234
There is one thing left to change: replace the hand-written JavaScript code with our Scala.js application.
248
-
We use the `@scala-js/vite-plugin-scalajs` plugin to link Vite and Scala.js with minimal configuration.
235
+
We use the `vite-plugin-scalajs-mill` plugin to link Vite and Scala.js with minimal configuration.
and instruct Vite to use it with the following configuration in a new file `vite.config.js`:
256
243
257
244
{% highlight javascript %}
258
245
import { defineConfig } from "vite";
259
-
import scalaJSPlugin from "@scala-js/vite-plugin-scalajs";
246
+
import scalaJSMillPlugin from "vite-plugin-scalajs-mill";
260
247
261
248
export default defineConfig({
262
-
plugins: [scalaJSPlugin()],
249
+
plugins: [scalaJSMillPlugin({
250
+
moduleNames: "livechart"
251
+
})],
263
252
});
264
253
{% endhighlight %}
265
254
@@ -270,7 +259,7 @@ import './style.css'
270
259
import 'scalajs:main.js'
271
260
{% endhighlight %}
272
261
273
-
When we `import` a URI starting with `scalajs:`, `vite-plugin-scalajs` resolves it to point to the output directory of Scala.js' `fastLinkJS` task.
262
+
When we `import` a URI starting with `scalajs:`, `vite-plugin-scalajs-mill` resolves it to point to the output directory of Scala.js' `fastLinkJS` task.
274
263
275
264
You may have to stop and restart the `npm run dev` process, so that Vite picks up the newly created configuration file.
276
265
Vite will refresh the browser with our updated "Hello Scala.js!" message.
@@ -298,7 +287,7 @@ Once we save, we notice that the browser refreshes with the updated message.
298
287
299
288
There are two things happening behind the scenes:
300
289
301
-
1. The `~fastLinkJS` task in sbt notices that a `.scala` file has changed, and therefore rebuilds the `.js` output.
290
+
1. The `~fastLinkJS` task in Mill notices that a `.scala` file has changed, and therefore rebuilds the `.js` output.
302
291
1. The `npm run dev` process with Vite notices that a `.js` file imported from `/main.js` has changed, and triggers a refresh with the updated files.
303
292
304
293
All these steps are *incremental*.
@@ -309,11 +298,12 @@ This ensures that the development cycle remains as short as possible.
309
298
310
299
## Production build
311
300
312
-
The `fastLinkJS` task of sbt and the `npm run dev` task of Vite are optimized for incremental development.
301
+
The `fastLinkJS` task of Mill and the `npm run dev` task of Vite are optimized for incremental development.
313
302
For production, we want to perform more optimizations on the Scala.js side and bundle minimized files with `npm run build`.
314
303
We stop Vite with `Ctrl+C` and launch the following instead:
315
304
316
305
{% highlight shell %}
306
+
$ mill livechart.fullLinkJS
317
307
$ npm run build
318
308
319
309
> livechart@0.0.0 build
@@ -348,7 +338,7 @@ Navigate to the mentioned URL to see your website.
348
338
349
339
## Conclusion
350
340
351
-
In this tutorial, we saw how to configure Scala.js with Vite from the ground up using `@scala-js/vite-plugin-scalajs`.
341
+
In this tutorial, we saw how to configure Scala.js with Vite from the ground up using `vite-plugin-scalajs-mill`.
352
342
We used sbt as our build tool, but the same effect can be achieved with any other Scala build tool, such as [Mill](https://com-lihaoyi.github.io/mill/) or [scala-cli](https://scala-cli.virtuslab.org/).
0 commit comments