Specify the sbt version in
project/build.properties, which needs to be 0.13.16 or higher (or sbt 1.x):
Add the sbt plugin to the
project/plugins.sbt file along with Scala.js:
addSbtPlugin("com.vmunier" % "sbt-web-scalajs" % "1.0.7") addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.22")
Lastly, put the following configuration in
lazy val server = project.settings( scalaJSProjects := Seq(client), pipelineStages in Assets := Seq(scalaJSPipeline) ).enablePlugins(SbtWeb) lazy val client = project.enablePlugins(ScalaJSPlugin, ScalaJSWeb)
Note: make sure you use the
To see the plugin in action, you can run
sbt new with one of these Giter8 templates:
- Play with Scala.js:
sbt new vmunier/play-scalajs.g8
- Akka HTTP with Scala.js:
sbt new vmunier/akka-http-scalajs.g8
How it works
There are two plugins:
WebScalaJSis automatically added to your SbtWeb project.
ScalaJSWebshould be manually added to the Scala.js projects that you want to connect the source mapping to your SbtWeb project.
- Scala.js projects are collected in the
scalaJSProjectssetting key of the SbtWeb project. The plugin does nothing if
scalaJSProjectsis not specified or is empty.
- When compilation or testing takes place, then the
WebScalaJSplugin runs all required tasks on
scalaJSProjectsprojects, copies the output to SbtWeb assets and takes care about source maps.
Settings and Tasks
scalaJSProjectssetting lists the Scala.js projects whose output is used by the server.
scalaJSDevtask runs all tasks for development, including Scala.js
fastOptJStask and source maps.
scalaJSProdtask runs all tasks for production, including Scala.js
fullOptJStask and source maps.
isDevModeis true, runs
isDevModetask returns true if the sbt command run by the user exists in the
devCommandssetting. Some users may want to override
isDevModeto read the dev/prod mode from a configuration file or from an environment variable.
devCommandssetting contains the name of the commands used during development, which includes
re-start. It can be extended/overridden to contain different dev commands. For example, adding
devCommands in scalaJSPipeline ++= Seq("test", "testOnly")to your build would make
scalaJSProdwhen running tests.
The plugin copies the Scala files to the SbtWeb assets, so that they can be served to the browser and used for Source Maps.
Source Map and Scala files do not exist in production by default to prevent your users from seeing the source files. But it can easily be enabled in production too by setting
(emitSourceMaps in fullOptJS) := true in the Scala.js projects.
Scala.js continuous compilation
The plugin also watches files from the Scala.js projects. Redefine
compile to trigger
scalaJSPipeline when using
compile in Compile := ((compile in Compile) dependsOn scalaJSPipeline.map(f => f(Seq.empty))).value
As we only care about triggering
scalaJSPipeline dependencies here, the line can be shortened to:
compile in Compile := ((compile in Compile) dependsOn scalaJSPipeline).value