blob: c8960c89a1ab4002df3c33e6d3a7187a90709b23 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package scala.scalajs.js.resource
import sbt._
import Keys._
import scala.scalajs.sbtplugin.ScalaJSPlugin.ScalaJSKeys._
import org.apache.commons.codec.binary.Base64
object Plugin extends sbt.Plugin {
val resourceSettings = Seq(
watchSources := {
watchSources.value ++ (resources in Compile).value ++ Path.allSubpaths((sourceDirectory in Compile).value / "js").map(_._1).toSeq
},
packageJS := {
val bundledJSResults: Set[sbt.File] = FileFunction.cached(
cacheDirectory.value,
FilesInfo.lastModified,
FilesInfo.exists
){(inFiles: Set[File]) =>
val pathMap = Path.relativeTo((resources in Compile).value)
val bundle = crossTarget.value / "resources.js"
val fileLines = for(file <- inFiles) yield {
val b64 = Base64.encodeBase64String(IO.readBytes(file))
" \"" + pathMap(file).get + "\": \"" + b64 + "\""
}
IO.write(bundle, "\nScalaJS.resources = {\n" + fileLines.mkString(",\n") + "\n}" )
Set(bundle)
}(
for{
(resourceRoot: File) <- (resources in Compile).value.toSet
(file, path) <- Path.allSubpaths(resourceRoot)
} yield file
)
val copiedJSFiles = FileFunction.cached(
cacheDirectory.value,
FilesInfo.lastModified,
FilesInfo.exists
){(inFiles: Set[File]) =>
val pathMap = Path.relativeTo((sourceDirectory in Compile).value / "js")
IO.copy(inFiles.map{f => f -> crossTarget.value / pathMap(f).get})
}(
for{
(file, path) <- Path.allSubpaths((sourceDirectory in Compile).value / "js").toSet
} yield file
)
val normalResults: Seq[sbt.File] = (packageJS in Compile).value
normalResults ++ bundledJSResults ++ copiedJSFiles
}
)
}
|