$ node -v
v18.16.0
$ yarn -v
1.22.19
Scaffold a Vite project:
$ yarn create vite scalafui
success Installed "[email protected]" with binaries:
- create-vite
- cva
Select a framework: › Vanilla
Select a variant: › JavaScript
Move the generated files to the top directory:
$ mv -f scalafui/{.,}* .
$ rm -rf scalafui
Test:
$ yarn
$ yarn run dev
- Add Scala.js specific entries to
.gitignore
- Add project settings and build.sbt
project/build.properties
project/plugins.sbt
build.sbt
- Add Scala.js source files
src/main/scala/scalafui/FunctionalUI.scala
src/main/scala/scalafui/Main.scala
- Add npm dependencies (React-related) in
package.json
- Add
vite.config.js
- Connect the Scala.js output to Vite
- Change the content of
main.js
toimport '@linkOutputDir/main.js'
- Delete the files imported in the old
main.js
style.css
javascript.svg
counter.js
public/vite.svg
- Change the content of
- Test the app
$ yarn
sbt> ~fastLinkJS
$ yarn run dev
With Vite v4.2.2, sourcemap errors will be output in the console like below when launching a dev server.
Sourcemap for "/path/to/scalafui/target/scala-2.13/scalafui-fastopt/scalafui.Main.js" points to missing source files
According to the debug log output by DEBUG="vite:sourcemap" yarn dev
, Vite doesn't seem to properly handle absolute source URLs (starting with file:
, https:
) in *.map files. It treats sources as relative paths from the source directory unconditionally like:
vite:sourcemap /path/to/scalafui/target/scala-2.13/scalafui-fastopt/https:/raw.githubusercontent.com/scala-js/scala-js/v1.13.1/library-aux/src/main/scala/scala/runtime/Statics.scala
- Getting Started with Scala.js and Vite - Scala.js
- scala-js/vite-plugin-scalajs: Vite plugin for integration of Scala.js
-
Add a dev dependency in
package.json
"@scala-js/vite-plugin-scalajs": "^1.0.0"
$ yarn
-
Update
vite.config.js
to:import { defineConfig } from "vite"; import scalaJSPlugin from "@scala-js/vite-plugin-scalajs"; export default defineConfig({ plugins: [scalaJSPlugin()], });
-
Update
main.js
to:import 'scalajs:main.js'
-
Update
build.sbt
- Delete
fastLinkOutputDir
,fullLinkOutputDir
tasks
- Delete