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
If you had not stopped the `npm run dev` process with Vite, Vite will immediately pick up the changes and refresh the browser with our updated "Hello Scala.js!" message.
273
+
You may have to stop and restart the `npm run dev` process, so that Vite picks up the newly created configuration file.
274
+
Vite will refresh the browser with our updated "Hello Scala.js!" message.
255
275
256
276
## Live changes with Scala.js
257
277
@@ -288,129 +308,50 @@ This ensures that the development cycle remains as short as possible.
288
308
## Production build
289
309
290
310
The `fastLinkJS` task of sbt and the `npm run dev` task of Vite are optimized for incremental development.
291
-
For production, we want to perform more optimizations on the Scala.js side with `fullLinkJS`, and bundle minimized files with `npm run build`.
292
-
293
-
For that to work well, we need to conditionally wire the output of `fastLinkJS` (resp. `fullLinkJS`) to the input of Vite when it is in development mode (resp. production mode).
294
-
This requires some configuration in Vite.
295
-
296
-
We create the file `vite.config.js` at the root of our project, with the following content:
297
-
298
-
{% highlight javascript %}
299
-
import { spawnSync } from "child_process";
300
-
import { defineConfig } from "vite";
301
-
302
-
// Detect whether Vite runs in development or production mode
303
-
function isDev() {
304
-
return process.env.NODE_ENV !== "production";
305
-
}
306
-
307
-
// Utility to invoke a given sbt task and fetch its output
Since the built website uses an ECMAScript module, we need to serve it through an HTTP server to visualize it.
380
-
We use the npm application `http-server` for that purpose, as we can run it without any additional dependency:
333
+
We can use Vite's `preview` mode for that purpose, as we can run it without any additional dependency:
381
334
382
335
{% highlight shell %}
383
-
$ cd dist
384
-
$ npx http-server
385
-
Starting up http-server, serving ./
386
-
387
-
http-server version: 14.1.1
388
-
389
-
http-server settings:
390
-
CORS: disabled
391
-
Cache: 3600 seconds
392
-
Connection Timeout: 120 seconds
393
-
Directory Listings: visible
394
-
AutoIndex: visible
395
-
Serve GZIP Files: false
396
-
Serve Brotli Files: false
397
-
Default File Extension: none
398
-
399
-
Available on:
400
-
http://127.0.0.1:8080
401
-
Hit CTRL-C to stop the server
336
+
$ npm run preview
337
+
338
+
> livechart@0.0.0 preview
339
+
> vite preview
340
+
341
+
➜ Local: http://localhost:4173/
342
+
➜ Network: use --host to expose
402
343
{% endhighlight %}
403
344
404
345
Navigate to the mentioned URL to see your website.
405
346
406
347
## Conclusion
407
348
408
-
In this tutorial, we saw how to configure Scala.js with Vite from the ground up.
349
+
In this tutorial, we saw how to configure Scala.js with Vite from the ground up using `@scala-js/vite-plugin-scalajs`.
409
350
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/).
410
351
411
352
Our setup features the following properties:
412
353
413
354
* Development mode with live reloading: changing Scala source files automatically triggers recompilation and browser refresh.
414
-
* Production mode, wired to automatically take the `fullLinkJS` output of Scala.js, and producing a unique `.js` file.
355
+
* Production mode, wired to automatically take the fully optimized output of Scala.js, and producing a unique `.js` file.
415
356
416
357
In our [next tutorial about Laminar](./laminar.html), we will learn how to write UIs in idiomatic Scala code.
0 commit comments