如何在JVM交叉项目中加载javascript

时间:2016-05-18 17:56:14

标签: scala.js

我遇到了一些困难,想知道是否有人可以帮助我。我有以下Build.scala,我试图从JVM项目访问编译javascript。

lazy val webProject = CrossProject(base = file("./main/web"), crossType = CrossType.Full, jvmId = "api-gateway", jsId = "web-js")
    .settings(
      name := "web",
      unmanagedSourceDirectories in Compile += baseDirectory.value / "shared" / "main" / "scala",
      libraryDependencies ++= Dependencies.Client.sharedDeps.value)
    .jvmSettings(
      persistLauncher := true,
      persistLauncher in Test := false,
      libraryDependencies ++= Dependencies.Client.jvmDeps.value)
    .jsSettings(libraryDependencies ++= Dependencies.Client.jsDeps.value)

  lazy val webJS = webProject.js.enablePlugins(ScalaJSPlugin)

  lazy val webJVM = webProject.jvm
    .settings((resources in Compile) += (fastOptJS in(webJS, Compile)).value.data)
    .dependsOn(dominos)

生成编译javascript

[info] Fast optimizing /.../main/web/js/target/scala-2.11/web-fastopt.js

当我尝试通过运行get server访问编译javascript时,找不到它。

object Main extends App {

  implicit val system = ActorSystem("my-system")
  implicit val materializer = ActorMaterializer()
  implicit val executionContext = system.dispatcher

  val routes = pathEndOrSingleSlash(getFromResource("web-fastopt.js"))

  Http().bindAndHandle(routes, "localhost", 8080)
}

这行不是假设在运行JVM的资源文件夹时添加javascript吗?

(resources in Compile) += (fastOptJS in(webJS, Compile)).value.data

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

似乎这样似乎因某种原因对我不起作用

(resources in Compile) += (fastOptJS in(webJS, Compile)).value.data

相反,我结束了移动fastOptJS文件

lazy val webJVM = webProject.jvm
    .settings(Seq(fastOptJS, fullOptJS, packageJSDependencies)
      .map(pkg ⇒ crossTarget in(webJS, Compile, pkg) := scalaJSOutput.value))

我还需要添加

getFromResourceDirectory("")

到Akka Http路线。