vmunier / sbt-web-scalajs

SBT plugin to use Scala.js along with any sbt-web server.
Apache License 2.0
194 stars 30 forks source link
sbt scala scalajs

sbt-web-scalajs

License Maven Central Gitter

sbt-web-scalajs is a SBT plugin which allows you to use Scala.js along with any sbt-web server. It uses the sbt-web and scala-js plugins.

Setup

Specify the sbt version in project/build.properties (you can find the latest version here):

sbt.version=1.9.7

Add the sbt-web-scalajs and Scala.js plugins to project/plugins.sbt:

addSbtPlugin("com.vmunier" % "sbt-web-scalajs" % "1.3.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.14.0")

Lastly, put the following configuration in build.sbt:

lazy val server = project.settings(
  scalaJSProjects := Seq(client),
  Assets / pipelineStages := Seq(scalaJSPipeline)
).enablePlugins(SbtWeb)

lazy val client = project.enablePlugins(ScalaJSPlugin, ScalaJSWeb)

Note: make sure you use the Assets scope.

Examples

To see the plugin in action, you can run sbt new with one of these Giter8 templates:

Releases

Have a look at the releases to find out about the new features, bug fixes and how to upgrade when breaking changes were introduced.

Selecting fastLinkJS or fullLinkJS

sbt-web-scalajs looks up the scalaJSStage setting from the Scala.js projects to know whether to run fastLinkJS or fullLinkJS.

How it works

There are two plugins: WebScalaJS and ScalaJSWeb.

Settings and Tasks

Defined in WebScalaJS:

Defined in ScalaJSWeb:

We can extend jsMappings to add the output of other Scala.js tasks. When using the sbt-jsdependencies plugin, we can update jsMappings in build.sbt as follows:

import com.typesafe.sbt.web.PathMapping

val client = project.settings(
  Compile / fastLinkJS / jsMappings += toPathMapping((Compile / packageJSDependencies).value),
  Compile / fullLinkJS / jsMappings += toPathMapping((Compile / packageMinifiedJSDependencies).value),
  ...
).enablePlugins(ScalaJSPlugin, ScalaJSWeb, JSDependenciesPlugin)

def toPathMapping(f: File): PathMapping = f -> f.getName

Source Maps

The plugin copies the Scala files to the sbt-web assets, so that they can be served to the browser and used for Source Maps.

By default, Source Maps are enabled in both fastLinkJS and fullLinkJS. However, Source Maps can easily be disabled in fullLinkJS by adding the following line to the Scala.js project settings:

Compile / fullLinkJS / scalaJSLinkerConfig ~= (_.withSourceMap(false))

When Source Maps are disabled, the .map files and the Scala files are not copied and do not exist in the sbt-web assets.

Note that Source Maps only get requested by the browser when the DevTools is open, so it does not hinder the performance of your website.

Scala.js continuous compilation

The plugin also watches files from the Scala.js projects. Redefine compile to trigger scalaJSPipeline when using compile, ~compile, ~run:

Compile / compile := ((Compile / compile) dependsOn scalaJSPipeline).value

Publish a new version of the plugin

New versions are automatically published to Sonatype when creating a git tag, thanks to sbt-ci-release.