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

Rei*_*cer 5 scala scala.js sbt-web

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

EEC*_*LOR 3

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
Run Code Online (Sandbox Code Playgroud)

编辑

创建插件需要更多的工作。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
)
Run Code Online (Sandbox Code Playgroud)

你的插件看起来像这样:

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
  )
}
Run Code Online (Sandbox Code Playgroud)

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

lazy val root = project.in( file(".") ).enablePlugins(MyScalaJsPlugin)
Run Code Online (Sandbox Code Playgroud)