Reputation: 34423
I have a Scala.js project with an index.html
file. Currently I use a structure like in Scala.js example, with two different index.html
files for fullOptJS
and fastOptJS
builds. However I would like to DRY this somehow.
I think it should be possible to generate both index.html
and index-fastopt.html
from a common source template using a simple Scala code doing the substitution of the variable parts. Perhaps one could achieve this using Generating sources and resources, but I am unsure how.
Upvotes: 0
Views: 610
Reputation: 1115
I did a checkout of the scala.js example project and modified it a little bit.
First, edit index.html
and replace the following lines:
<script type="text/javascript" src="./target/scala-2.11/example-opt.js"></script>
<script type="text/javascript" src="./target/scala-2.11/example-launcher.js"></script>
with:
<script type="text/javascript" src="{{example.js}}"></script>
<script type="text/javascript" src="example-launcher.js"></script>
Next, add the following lines in build.sbt:
def generateIndexTask(suffix: String) = Def.task {
val source = baseDirectory.value / "index.html"
val target = (crossTarget in Compile).value / "index.html"
val log = streams.value.log
IO.writeLines(target,
IO.readLines(source).map {
line => line.replace("{{example.js}}", s"example-$suffix.js")
}
)
log.info(s"generate with suffix: $suffix")
}
(fastOptJS in Compile) <<= (fastOptJS in Compile).dependsOn(generateIndexTask("fastopt"))
(fullOptJS in Compile) <<= (fullOptJS in Compile).dependsOn(generateIndexTask("opt"))
The generated index.html will be located at the sample place as the generate .js files: ./target/scala-2.11
You should probably adjust the file paths to your own use.
For my own projects, I use another structure with a sbt-web project at the top level to manage all assets and the scala-js code in a sub-project that exports it's code as a webjar. It's more flexible.
Upvotes: 2