使用scala.js在SBT中仅编译(而不是覆盖运行)

时间:2014-12-16 13:38:36

标签: sbt scala.js

我试图使用scalajs将一些scala源代码编译为javascript而不修改有关sbt环境的任何其他内容,我不希望它覆盖" run&的默认行为#34; sbt命令。

目前我的build.sbt看起来像是:

import ScalaJSKeys._

scalaJSSettings

name := "foo"

organization := "com.example"

scalaVersion := "2.11.4"

compile <<= (compile in Compile) dependsOn (fastOptJS in Compile)

crossTarget in (fastOptJS in Compile) := ((classDirectory in Compile).value / "public" / "js")

libraryDependencies ++= {
val sprayVersion = "1.3.2"
val akkaVersion = "2.3.7"
Seq(
    "io.spray"            %%  "spray-can"     % sprayVersion,
    "io.spray"            %%  "spray-routing" % sprayVersion,
    "io.spray"            %%  "spray-servlet" % sprayVersion,
    "io.spray"            %%  "spray-testkit" % sprayVersion  % "test",
    "com.typesafe.akka"   %%  "akka-actor"    % akkaVersion,
    "com.typesafe.akka"   %%  "akka-testkit"  % akkaVersion   % "test",
    "org.specs2"          %%  "specs2-core"   % "2.3.11" % "test",
    "javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided",
    "org.scala-lang.modules.scalajs" %%% "scalajs-jquery" % "0.6"
)}

编译javascript和scala都很好,但问题是这实际上打破了现有的&#34;运行&#34;我希望使用与默认sbt相同的发现来运行普通旧scala的命令。我的项目很简单,所以我不想沿着多项目路线走下去(就像play-with-scalajs-example)。我想我可能需要删除scalaJSSettings,但后来我不知道如何访问fastOptJS目标,所以我可以将它作为依赖项附加到编译之后。

1 个答案:

答案 0 :(得分:3)

必须不这样做。只要将scalaJSSettings放在项目中,就会使用Scala.js编译器插件编译所有源代码。

这确实会生成.class文件,但是,它们包含基本Scala编译器不会发出的内容,因此可能导致二进制不兼容问题或意外行为(请参阅this post)。

相反,使用多项目构建:

import ScalaJSKeys._

organization := "com.example"
scalaVersion := "2.11.4"

val sprayVersion = "1.3.2"
val akkaVersion = "2.3.7"

lazy val foo = project.
  settings(
    name := "foo",
    compile <<= (compile in Compile) dependsOn (fastOptJS in Compile in bar),
    crossTarget in (fastOptJS in Compile in bar) :=
      ((classDirectory in Compile).value / "public" / "js"),
    libraryDependencies ++= Seq(
      "io.spray"            %%  "spray-can"     % sprayVersion,
      "io.spray"            %%  "spray-routing" % sprayVersion,
      "io.spray"            %%  "spray-servlet" % sprayVersion,
      "io.spray"            %%  "spray-testkit" % sprayVersion  % "test",
      "com.typesafe.akka"   %%  "akka-actor"    % akkaVersion,
      "com.typesafe.akka"   %%  "akka-testkit"  % akkaVersion   % "test",
      "org.specs2"          %%  "specs2-core"   % "2.3.11" % "test",
      "javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided"
    )
  )


lazy val bar = project.
  settings(scalaJSSettings: _*).
  settings(
    name := "bar",
    libraryDependencies += "org.scala-lang.modules.scalajs" %%% "scalajs-jquery" % "0.6",
  )

这显然也解决了run命令的问题。