Scala.js compilation destination

我怕爱的太早我们不能终老 提交于 2019-12-03 11:51:02

问题


I'm working on a Scala.js cross project where the jvm folder represents my server application and jsrepresents my scala.js code.

Whenever i compile my scala.js code via sbt crossJS/fastOptJS the compiled JS ends up in ./js/target/scala-2.11/web-fastopt.js.

I need to have this compiled JS file accessible in the resources of the server project in the jvm folder, so i can server it through my web application. I think i have to do something with artifactPath but i can't seem to get any results from my experiments thus far.


回答1:


You can simply set the artifactPath of the fastOptJS task (or the fullOptJS task) to the (managed) resources directory of your JVM project:

// In the JS project's settings
artifactPath in fastOptJS :=
  (resourceManaged in jvm in Compile).value /
    ((moduleName in fastOptJS).value + "-fastopt.js"))

This will put it in the directory, if the you run the fastOptJS task. However, it will not be included in sbt's resources task and it will not automatically be triggered, if you launch your server. Therefore:

// In the JVM project's settings
resources in Compile += (fastOptJS in js).value.data

A couple of notes:

  • The first step is only necessary, if your web-server does only serve specific directories. Otherwise the second one is enough, as this adds the file to the resources already; where it lies is secondary.
  • Setting the crossTarget, as in @ochrons' answer will also output all the .class and .sjsir files in the resource directory.
  • Have a look at Vincent Munier's sbt-play-scalajs for out-of-the-box sbt-web / Scala.js integration (it follows a slightly different approach: It copies the file from the js project, rather than directly placing it in the JVM project. Useful if you have multiple JVM projects).



回答2:


You can configure the Scala.js SBT plugin to output the JavaScript file in folder of your choosing. For example like this:

// configure a specific directory for scalajs output
val scalajsOutputDir = Def.settingKey[File]("directory for javascript files output by scalajs")

// make all JS builds use the output dir defined later
lazy val js2jvmSettings = Seq(fastOptJS, fullOptJS, packageJSDependencies) map { packageJSKey =>
  crossTarget in(js, Compile, packageJSKey) := scalajsOutputDir.value
}
// instantiate the JVM project for SBT with some additional settings
lazy val jvm: Project = sharedProject.jvm.settings(js2jvmSettings: _*).settings(
  // scala.js output is directed under "web/js" dir in the jvm project
  scalajsOutputDir := (classDirectory in Compile).value / "web" / "js",

This will also store -jsdeps.js and .js.map files in the same folder, in case you want to use those in your web app.

For a more complete example, check out this tutorial which addresses many other issues of creating a more complex Scala.js application.




回答3:


Or simply:

artifactPath in (Compile, fastOptJS) := (artifactPath in (Compile, fullOptJS)).value

Compare to gzm0's solution you are not re



来源:https://stackoverflow.com/questions/29371654/scala-js-compilation-destination

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!