How to make scalatra compile task depend on scala.js packageJS task in sbt?
Question
I have a scalatra project within which I want to serve JavaScript that's been generated from a scala.js project.
I have a multi-project sbt build that allows me to build both these project successfully. That is, I can use the compile (and run) tasks in the scalatra project; and I can use the packageJS task in the scala.js project.
However, I'd very much like to be able to have the scalatra compile task depend on the scala.js packageJS task; so when the Scalatra project is compiled, the Javascript is automatically generated in the Scala.js project.
In my multi-project build.sbt file I've tried:
lazy val wwwjs = project // Scala.js project
lazy val www = project // Scalatra project
.dependsOn(wwwjs % "compile->packageJS")
But when compiling the Scalatra project this fails with "configuration not found in wwwjs#wwwjs_2.10;0.1: 'packageJS'. It was required from www#www_2.10;0.1 compile".
I'm fairly new to sbt (and Scalatra and Scala.js!) - can anyone enlighten me as to how to do this?
Thanks :)
Solution
What you want here is not a dependency between projects (which is what dependsOn
on a project does) but between tasks
.
I am not familiar with Scalatra, but for the sbt side it should be extremely similar to Play, and we have a successful template for Play projects with Scala.js here: https://github.com/vmunier/play-with-scalajs-example/blob/master/project/Build.scala
In particular, I derive the following basic adaptation from your build above:
import scala.scalajs.sbtplugin.ScalaJSPlugin._ // if Build.scala
import ScalaJSKeys._
lazy val wwwjs = project // Scala.js project
lazy val www = project // Scalatra project
.settings(
compile in Compile <<= (compile in Compile) dependsOn (fastOptJS in (wwwjs, Compile))
)
Alternatively, use fullOptJS
instead of fastOptJS
for the fully optimized version (but that takes several seconds to rebuild every time you make a change).