5

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?

Reid Spencer
  • 2,616
  • 25
  • 33
  • [Typesafe and Scala.js have talked about it](https://groups.google.com/forum/#!msg/scala-js/apbxL1KHiTo/nPi14uYTDmgJ), [I asked if there are any updates](https://groups.google.com/d/msg/scala-js/apbxL1KHiTo/jcHI-phoNU0J). – EECOLOR Oct 14 '14 at 21:16
  • @EECOLOR - I guess my question is about how to do the integration, not whether Typesafe and Scala.js are talking about it. However, please update here if you get a reply. Thx. – Reid Spencer Oct 22 '14 at 18:54
  • The reply was: `It seems to have fallen out of their immediate priorities, I'm afraid.` – EECOLOR Oct 22 '14 at 20:55
  • That's about what I figured or else it would already be done. What I'm looking for is some sort of hint about how to do the integration between the two. According to one video I watched on sbt-web it is relatively easy. I don't mind doing the work, I just can't find any resources on WHAT to do. – Reid Spencer Oct 23 '14 at 02:45

2 Answers2

3

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)
EECOLOR
  • 11,034
  • 3
  • 38
  • 72
  • This part is relatively straight forward and I can easily add such a "source file task" to a given project. That's not the same thing as creating an sbt-web plugin that does all this for you. Not stated here, or in the documentation, are how to integrate the various compiler options and turn this into an sbt-web plugin that it can incorporate automatically including using the default options when "SbtWeb" plugin is loaded. – Reid Spencer Oct 23 '14 at 15:06
  • Ahh, I didn't get that from your question. I have added what I know about sbt plugins and what I would be needed to the answer. – EECOLOR Oct 23 '14 at 18:30
  • Good point. I updated the question to indicate I was looking for an sbt-web plugin as the solution. – Reid Spencer Oct 23 '14 at 21:47
  • Thank you for the additional information on the "AutoPlugin" -- I guess that was the missing link. I likely have enough information to proceed now. – Reid Spencer Oct 23 '14 at 21:48
  • 1
    Note that Scala.js will become an AutoPlugin in 0.6.0. See https://github.com/scala-js/scala-js/issues/1025 – sjrd Oct 26 '14 at 18:21
  • Yes, I was about to start work on making it an AutoPlugin but just started using 0.6.0-M2 instead. Seems to work! – Reid Spencer Dec 17 '14 at 19:53
1

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)
gzm0
  • 14,247
  • 1
  • 33
  • 57