semptic
semptic

Reputation: 654

How to add the output path of JavaScripts from scala.js to Play?

I'm trying to get play (exactly sbt-web) to access the javascript files created from scala.js.

With unmanagedResourceDirectories in Assets += I'm able to set a paht for the sbt-web to automatically copy to the right asset directory. The problem is that I won't hardcode the path to the javascript files.

My Build.scala looks like this:

import play.Play._
import com.typesafe.sbt.web.Import.Assets
import scala.scalajs.sbtplugin.ScalaJSPlugin._
import scala.scalajs.sbtplugin.ScalaJSPlugin.ScalaJSKeys._

object Build extends sbt.Build {
    override def rootProject = Some(jvm) 

    lazy val js = project
                    .settings(scalaJSSettings: _*)
                    .settings(
                       scalaVersion := "2.11.2"
                    )

    lazy val jvm = project
                     .settings(play.PlayScala)
                     .settins(
                       scalaVersion := "2.11.2",
                       // This is the interesting line
                       unmanagedResourceDirectories in Assets += (target in js).value / "scala-2.11",
                       compile in Compile <<= (compile in Compile) dependsOn (fastOptJS in (js, Compile))
                     )
                     .aggregate(js)
}

Is there a way to get the output path of the javascript from scala.js?

(target in js).value / "scala-2.11" works, but there is the hardcoded scala-2.11 and the classes directory in the assets output.

Another Problem is that the files now are under web/public/main/ instead of web/public/main/js.

Upvotes: 4

Views: 1626

Answers (3)

Cause Chung
Cause Chung

Reputation: 1505

I had been working on the solution for almost 3 days. Much of it was about how to integrate js output into play auto reloading. What I suggest:

1.Adding target path to play assets is not very appropriate. Since, well, just not neaty and for play's frigerprint control. etc.

2.Copy file to assets folder. Good at first. However when I added triggeredBy or dependsOn, trying to let play auto reload and do the copy. Problem happened: copy would always be invoked multiple times, with stun of the server, no matter how I excluded assets from wathSources.

3.Change js output path. I finnally returned to this approach. Some people metion to alter artifactPath's value, nevertheless, I was unable to work this around.

What I did below:

//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 jsOutputSettings = Seq(fastOptJS, fullOptJS, packageJSDependencies, packageScalaJSLauncher, packageMinifiedJSDependencies) map { packageJSKey =>
  crossTarget in(Compile, packageJSKey) := scalajsOutputDir.value
}

I used sbt's inspect to find packageMinifiedJSDependencies.

Hope I can help someone who meets this later.

Upvotes: 2

gzm0
gzm0

Reputation: 14842

Have a look at sbt-play-scalajs. It is an additional sbt plugin that helps integration with Play / sbt-web.

Your build file will look something like this (copied from README):

import sbt.Project.projectToRef

lazy val jsProjects = Seq(js)

lazy val jvm = project.settings(
  scalaJSProjects := jsProjects,
  pipelineStages := Seq(scalaJSProd)).
  enablePlugins(PlayScala).
  aggregate(jsProjects.map(projectToRef): _*)

lazy val js = project.enablePlugins(ScalaJSPlugin, ScalaJSPlay)

Upvotes: 0

sjrd
sjrd

Reputation: 22095

Instead of (target in js).value / "scala-2.11", you can use (crossTarget in js).value. It will append the right subdirectory, depending on your Scala version.

The fully qualified name of the file generated by fastOptJS in (js, Compile) is artifactPath in (js, Compile, fastOptJS), as is customary in sbt.

You can also change the destination file of fastOptJS by modifying artifactPath instead. This could help you with your last problem. This technique can be used to send the generated file directly to the managed resources of the jvm project.

Upvotes: 5

Related Questions