Specify the sbt version in
project/build.properties, which needs to be 0.13.16 or higher (or sbt 1.x):
If you want to use Scala.js 1.x, add the following plugins to
addSbtPlugin("com.vmunier" % "sbt-web-scalajs" % "1.0.8") addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.0.0-M3")
Otherwise, if you prefer using Scala.js 0.6.x, add the following plugins to
addSbtPlugin("com.vmunier" % "sbt-web-scalajs" % "1.0.8-0.6") addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.23")
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
Selecting fastOptJS or fullOptJS
sbt-web-scalajs maintains a list of dev commands, which includes
show scalaJSPipeline::devCommands to see the full list).
When one of the dev commands is executed in SBT, e.g.
sbt run, sbt-web-scalajs considers to be in development mode and will call Scala.js fastOptJS.
For all other commands, which are not listed in the
devCommands setting, e.g.
sbt universal:packageBin, sbt-web-scalajs considers to be in production mode and will call Scala.js fullOptJS.
It is possible to control when fastOptJS or fullOptJS is selected, either by extending the
devCommands setting or by overriding the
You may want to instruct sbt-web-scalajs to execute fastOptJS when the tests are run, in which case you can add
devCommands in scalaJSPipeline ++= Seq("test", "testOnly") to your server's build settings.
You can also explicitly control when fastOptJS or fullOptJS is executed. For example, you may want sbt-web-scalajs to always execute fastOptJS, except when a
SCALAJS_PROD environment variable is defined, in which case add
isDevMode in scalaJSPipeline := !sys.env.get("SCALAJS_PROD").isDefined to your server's build settings. Simply start SBT with
sbt and fastOptJS will be executed for any command; similarly start SBT with
SCALAJS_PROD=true sbt and fullOptJS will be executed for any command.
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
scalaJSLinkerConfig in fullOptJS ~= (_.withSourceMap(true)) in the Scala.js projects.
Scala.js continuous compilation
The plugin also watches files from the Scala.js projects.
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
Publish a new verion of the plugin
For Scala.js 0.6.x:
$ sbt ^publish
For Scala.js 1.x:
$ SCALAJS_VERSION=1.0.0-M3 sbt ^publish