How can scala-js integrate with sbt-web?

时间秒杀一切 提交于 2020-01-01 05:33:07

问题


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?


回答1:


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)



回答2:


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)


来源:https://stackoverflow.com/questions/26346605/how-can-scala-js-integrate-with-sbt-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!