Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can scala-js integrate with sbt-web?

I would like to use scala-js with sbt-web in such a way that it can be compiled to produce javascript assets that are added to the asset pipeline (e.g. gzip, digest). I am aware of lihaoyi's workbench project but I do not believe this affects the asset pipeline. How can these two projects be integrated as an sbt-web plugin?

like image 809
Reid Spencer Avatar asked Sep 30 '22 15:09

Reid Spencer


2 Answers

Scala-js produces js files from Scala files. The sbt-web documentation calls this a Source file task.

The result would look something like this:

val compileWithScalaJs = taskKey[Seq[File]]("Compiles with Scala js")

compileWithScalaJs := {
  // call the correct compilation function / task on the Scala.js project
  // copy the resulting javascript files to webTarget.value / "scalajs-plugin"
  // return a list of those files
}

sourceGenerators in Assets <+= compileWithScalaJs

Edit

To create a plugin involves a bit more work. Scala.js is not yet an AutoPlugin, so you might have some problems with dependencies.

The first part is that you add the Scala.js library as a dependency to your plugin. You can do that by using code like this:

libraryDependencies += Defaults.sbtPluginExtra(
  "org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.5.5", 
  (sbtBinaryVersion in update).value, 
  (scalaBinaryVersion in update).value
)

Your plugin would look something like this:

object MyScalaJsPlugin extends AutoPlugin {
  /* 
    Other settings like autoImport and requires (for the sbt web dependency), 
    see the link above for more information
  */

  def projectSettings = scalaJSSettings ++ Seq(
    // here you add the sourceGenerators in Assets implementation
    // these settings are scoped to the project, which allows you access 
    // to directories in the project as well
  )
}

Then in a project that uses this plugin you can do:

lazy val root = project.in( file(".") ).enablePlugins(MyScalaJsPlugin)
like image 184
EECOLOR Avatar answered Oct 08 '22 01:10

EECOLOR


Have a look at sbt-play-scalajs. It is an additional sbt plugin that helps integration with Play / sbt-web.

Your build file will look something like this (copied from README):

import sbt.Project.projectToRef

lazy val jsProjects = Seq(js)

lazy val jvm = project.settings(
  scalaJSProjects := jsProjects,
  pipelineStages := Seq(scalaJSProd)).
  enablePlugins(PlayScala).
  aggregate(jsProjects.map(projectToRef): _*)

lazy val js = project.enablePlugins(ScalaJSPlugin, ScalaJSPlay)
like image 32
gzm0 Avatar answered Oct 07 '22 23:10

gzm0