Crossbuilding Scala.js 1.0 and 0.6

Post image

As you may know, Scala.js 1.0.0 just went final. For end-products, the upgrade is rather simple, just bump the plugin version and fix potential compile issues. If you’re maintaining a library, cross-building for 0.6.x and 1.0.x is still a bit tricky, but possible…

How to parametrize the plugin version?

First of all, one needs to find a way to locad different versions of the SBT plugin. This is possible using the following snippet:

project/plugins.sbt

val scalaJSVersion = Option(System.getenv("SCALAJS_VERSION")).getOrElse("1.0.1")

addSbtPlugin("org.scala-js" % "sbt-scalajs" % scalaJSVersion)

That’s basically it. Now one can release for different Scala.js versions using shell commands like the following:

SCALAJS_VERSION=0.6.32 sbt +publish
SCALAJS_VERSION=1.0.1 sbt +publish
sbt +publish # picks default version

As usual, the above will publish for all crossScalaVersions, but keep in mind, that Scala.js 1.0.x only supports Scala >= 2.12.

Enabling the JSDom

Scala.js 1.0.x has externalized the JSDom support to its own package, which in turn is not compatible with sjs 0.6.x. One approach to solve this, is adding the following to your project/plugins.sbt:

libraryDependencies ++= {
  if(scalaJSVersion.startsWith("1.")) {
    Seq("org.scala-js" %% "scalajs-env-jsdom-nodejs" % "1.0.0" )
  } else {
    Nil
  }
}

Now, the JSDom from Node.js can be used with both versions:

build.sbt

jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv

Version-based dependencies

There are some libs, which are completely different between the versions. One example is scalajs-tools which was splitted into different ones (e.g. scalajs-logging) for 1.0.x. The following SBT code snippet enables version-based dependencies:

build.sbt

libraryDependencies ++= Seq(
    "org.scala-js" %%% "scalajs-dom" % "1.0.0",
    "co.fs2" %%% "fs2-core" % "2.3.0"
  ) ++ {
    if(scalaJSVersion.startsWith("1.")) Seq(
      "org.scala-js" %%% "scalajs-logging" % scalaJSVersion
    ) else Seq(
      "org.scala-js" %%% "scalajs-tools" % scalaJSVersion
    )
  }

You May Also Like

Scala Compiler Tuning

Scala Compiler Tuning

As my Scala projects go on, I want to share some compiler configuration and tricks with you, which I use on many projects. Some tiny configuration options can greatly improve your code and warn you about things, you would probably never discover.

Java Libs in Scala - A bit more Functional

Java Libs in Scala - A bit more Functional

Every Java library can be used in Scala, which is, for me, one of the good parts of the JVM world. But Java libs are mostly object-oriented and not functional, therefore full of side effects and somtimes “ugly” to use in Scala. But there are some approaches how to make Java libs (or their interfaces) more functional, so they can almost be used like a Scala lib.