在 SBT 中使用 scala.js 仅编译(而不是覆盖运行) [英] Using scala.js to compile only (and not override run) in SBT

查看:49
本文介绍了在 SBT 中使用 scala.js 仅编译(而不是覆盖运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 scalajs 仅将一些 scala 源编译为 javascript,而不修改有关 sbt 环境的任何其他内容,我不希望它覆盖运行"sbt 命令的默认行为.

I'm trying to use scalajs to just compile some scala sources to javascript and not modify anything else about the sbt environment, I don't want it to override the default behavior of the "run" sbt command.

目前我有一个 build.sbt 看起来像:

Currently I've got a build.sbt that looks like:

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,但问题是这实际上破坏了现有的运行"命令,我想使用与默认 sbt 相同的发现来运行普通的旧 scala.我的项目很简单,所以我不想走多项目路线(就像 play-with-scalajs-example).我想我可能需要删除 scalaJSSettings,但是我不知道如何访问 fastOptJS 目标,因此我可以在这样做后将其附加为依赖项进行编译.

Which compiles both the javascript and the scala just fine, but the problem is that this actually breaks an existing "run" command which I want to run plain old scala using the same discovery as the default sbt. My project is simple so I don't want to go down the multi-project route (as with the play-with-scalajs-example). I guess I probably need to remove the scalaJSSettings but then I don't know how to access the fastOptJS target so I can attach it as a dependency to compile after doing so.

推荐答案

不得这样做.一旦您将 scalaJSSettings 放入项目中,所有源代码都将使用 Scala.js 编译器插件进行编译.

You must not do this. As soon as you put the scalaJSSettings in a project, all the sources will be compiled with the Scala.js compiler plugin.

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

This will indeed produce .class files, however, they contain things the basic Scala compiler does not emit and can therefore lead to binary incompatibility issues or unexpected behavior (see this post).

相反,使用多项目构建:

Instead, use a multi-project build:

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 命令的问题.

This obviously also resolves the issue with the run command.

这篇关于在 SBT 中使用 scala.js 仅编译(而不是覆盖运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆