forked from ochrons/scalajs-spa-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.scala
145 lines (121 loc) · 5.5 KB
/
build.scala
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import com.lihaoyi.workbench.Plugin._
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
import sbt.Keys._
import sbt._
import spray.revolver.RevolverPlugin._
/**
* Application settings. Configure the build for your application here.
* You normally don't have to touch the actual build definition after this.
*/
object Settings {
/** The name of your application */
val name = "scalajs-spa"
/** The version of your application */
val version = "0.1.3"
/** Options for the scala compiler */
val scalacOptions = Seq(
"-Xlint",
"-unchecked",
"-deprecation",
"-feature"
)
/** Set some basic options when running the project with Revolver */
val jvmRuntimeOptions = Seq(
"-Xmx1G"
)
/** Declare global dependency versions here to avoid mismatches in multi part dependencies */
object versions {
val scala = "2.11.5"
val scalajsReact = "0.8.0"
}
/**
* These dependencies are shared between JS and JVM projects
* the special %%% function selects the correct version for each project
*/
val sharedDependencies = Def.setting(Seq(
"com.lihaoyi" %%% "autowire" % "0.2.4",
"com.lihaoyi" %%% "upickle" % "0.2.6"
))
/** Dependencies only used by the JVM project */
val jvmDependencies = Def.setting(Seq(
"io.spray" %% "spray-can" % "1.3.2",
"io.spray" %% "spray-routing" % "1.3.2",
"com.typesafe.akka" %% "akka-actor" % "2.3.6"
))
/** Dependencies only used by the JS project (note the use of %%% instead of %%) */
val scalajsDependencies = Def.setting(Seq(
"com.github.japgolly.scalajs-react" %%% "core" % versions.scalajsReact,
"com.github.japgolly.scalajs-react" %%% "extra" % versions.scalajsReact,
"com.lihaoyi" %%% "scalarx" % "0.2.7",
"com.lihaoyi" %%% "utest" % "0.3.0"
))
}
object ApplicationBuild extends Build {
// root project aggregating the JS and JVM projects
lazy val root = project.in(file(".")).
aggregate(js, jvm).
settings(
publish := {},
publishLocal := {}
)
val sharedSrcDir = "shared"
// a special crossProject for configuring a JS/JVM/shared structure
lazy val sharedProject = crossProject.in(file("."))
.settings(
name := Settings.name,
version := Settings.version,
scalaVersion := Settings.versions.scala,
scalacOptions ++= Settings.scalacOptions,
libraryDependencies ++= Settings.sharedDependencies.value
)
// set up settings specific to the JVM project
.jvmSettings(Revolver.settings: _*)
.jvmSettings(
libraryDependencies ++= Settings.jvmDependencies.value,
// copy resources from the "shared" project
unmanagedResourceDirectories in Compile += file(".") / sharedSrcDir / "src" / "main" / "resources",
unmanagedResourceDirectories in Test += file(".") / sharedSrcDir / "src" / "test" / "resources",
javaOptions in Revolver.reStart ++= Settings.jvmRuntimeOptions,
// configure a specific port for debugging, so you can easily debug multiple projects at the same time if necessary
Revolver.enableDebugging(port = 5111, suspend = false)
)
// set up settings specific to the JS project
.jsSettings(workbenchSettings: _*)
.jsSettings(
libraryDependencies ++= Settings.scalajsDependencies.value,
// copy resources from the "shared" project
unmanagedResourceDirectories in Compile += file(".") / sharedSrcDir / "src" / "main" / "resources",
unmanagedResourceDirectories in Test += file(".") / sharedSrcDir / "src" / "test" / "resources",
// use uTest framework for tests
testFrameworks += new TestFramework("utest.runner.Framework"),
// define where the JS-only application will be hosted by the Workbench plugin
localUrl := ("localhost", 13131),
refreshBrowsers <<= refreshBrowsers.triggeredBy(fastOptJS in Compile),
bootSnippet := "SPAMain().main();"
)
// 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(packageScalaJSLauncher, fastOptJS, fullOptJS) map { packageJSKey =>
crossTarget in(js, Compile, packageJSKey) := scalajsOutputDir.value
}
// instantiate the JS project for SBT with some additional settings
lazy val js: Project = sharedProject.js.settings(
fastOptJS in Compile := {
// make a copy of the produced JS-file (and source maps) under the js project as well,
// because the original goes under the jvm project
// NOTE: this is only done for fastOptJS, not for fullOptJS
val base = (fastOptJS in Compile).value
IO.copyFile(base.data, (classDirectory in Compile).value / "web" / "js" / base.data.getName)
IO.copyFile(base.data, (classDirectory in Compile).value / "web" / "js" / (base.data.getName + ".map"))
base
}
)
// 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",
// compile depends on running fastOptJS on the JS project
compile in Compile <<= (compile in Compile) dependsOn (fastOptJS in(js, Compile))
)
}