scala-js如何与sbt-web集成?

时间:2014-10-13 18:33:25

标签: scala scala.js sbt-web

我希望scala-jssbt-web一起使用,以便可以编译它以生成添加到资产管道的javascript资源(例如gzip,digest)。我知道lihaoyi的工作台项目,但我不相信这会影响资产管道。如何将这两个项目集成为sbt-web插件?

2 个答案:

答案 0 :(得分:3)

Scala-js从Scala文件生成js文件。 sbt-web文档将其称为Source file task

结果看起来像这样:

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

修改

创建插件需要多做一些工作。 Scala.js还不是AutoPlugin,因此您可能会遇到依赖项问题。

第一部分是您将Scala.js库添加为插件的依赖项。您可以使用以下代码执行此操作:

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

你的插件看起来像这样:

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
  )
}

然后在使用此插件的项目中,您可以执行以下操作:

lazy val root = project.in( file(".") ).enablePlugins(MyScalaJsPlugin)

答案 1 :(得分:1)

看看sbt-play-scalajs。它是一个额外的sbt插件,可以帮助与Play / sbt-web集成。

您的构建文件将如下所示(从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)