Scala-js 从 Scala 文件生成 js 文件。 sbt-web 文档将其称为源文件任务 https://github.com/sbt/sbt-web#writing-a-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
Edit
创建插件需要更多的工作。 Scala.js 还不是AutoPlugin http://www.scala-sbt.org/0.13/docs/Plugins.html,所以你可能会遇到一些依赖问题。
第一部分是将 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)