我想在sbt-web中使用scala-js,这样就可以编译它来生成添加到资产管道中的javascript资产(例如gzip、摘要)。我知道李好易的工作台项目,但我不认为这会影响资产管道。如何将这两个项目集成为sbt web插件?
Scala js从Scala文件中生成js文件。sbt web文档将此任务称为源文件任务。
结果看起来像这样:
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)
看看sbt-play-scalajs。这是一个额外的sbt插件,有助于与Play/sbt-web集成。
您的构建文件将如下所示(从自述文件中复制):
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)