|
| 1 | +import java.nio.file.Files |
| 2 | +import java.nio.file.StandardCopyOption.REPLACE_EXISTING |
| 3 | + |
1 | 4 | ThisBuild / versionScheme := Some("early-semver")
|
2 | 5 | // For all Sonatype accounts created on or after February 2021
|
3 | 6 | ThisBuild / sonatypeCredentialHost := "s01.oss.sonatype.org"
|
@@ -26,6 +29,8 @@ val sttp = "3.5.0"
|
26 | 29 |
|
27 | 30 | val consoleDisabledOptions = Seq("-Xfatal-warnings", "-Ywarn-unused", "-Ywarn-unused-import")
|
28 | 31 |
|
| 32 | +lazy val build = TaskKey[File]("build") |
| 33 | + |
29 | 34 | // Used only by the server
|
30 | 35 | lazy val baseServerSettings: Project => Project = {
|
31 | 36 | _.settings(
|
@@ -194,15 +199,84 @@ lazy val bundlerSettings: Project => Project =
|
194 | 199 | Compile / fullOptJS / webpackDevServerExtraArgs += "--mode=production"
|
195 | 200 | )
|
196 | 201 |
|
| 202 | +lazy val spraWebBuildInfoSettings: Project => Project = _.enablePlugins(BuildInfoPlugin) |
| 203 | + .settings( |
| 204 | + buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion), |
| 205 | + buildInfoKeys ++= { |
| 206 | + val apiUrl = sys.env.get("API_URL") |
| 207 | + val values = Seq( |
| 208 | + "apiUrl" -> apiUrl |
| 209 | + ) |
| 210 | + // Logging these values is useful to make sure that the necessary settings |
| 211 | + // are being overriden when packaging the app. |
| 212 | + sLog.value.info(s"BuildInfo settings:\n${values.mkString("\n")}") |
| 213 | + values.map(t => BuildInfoKey(t._1, t._2)) |
| 214 | + }, |
| 215 | + buildInfoPackage := "net.wiringbits", |
| 216 | + buildInfoUsePackageAsPath := true |
| 217 | + ) |
| 218 | + |
| 219 | +lazy val browserProject: Project => Project = |
| 220 | + _.settings( |
| 221 | + build := { |
| 222 | + val artifacts = (Compile / fullOptJS / webpack).value |
| 223 | + val artifactFolder = (Compile / fullOptJS / crossTarget).value |
| 224 | + val jsFolder = baseDirectory.value / "src" / "main" / "js" |
| 225 | + val distFolder = baseDirectory.value / "build" |
| 226 | + |
| 227 | + distFolder.mkdirs() |
| 228 | + artifacts.foreach { artifact => |
| 229 | + val target = artifact.data.relativeTo(artifactFolder) match { |
| 230 | + case None => distFolder / artifact.data.name |
| 231 | + case Some(relFile) => distFolder / relFile.toString |
| 232 | + } |
| 233 | + |
| 234 | + Files.copy(artifact.data.toPath, target.toPath, REPLACE_EXISTING) |
| 235 | + } |
| 236 | + |
| 237 | + // copy public resources |
| 238 | + Files |
| 239 | + .walk(jsFolder.toPath) |
| 240 | + .filter(x => !Files.isDirectory(x)) |
| 241 | + .forEach(source => { |
| 242 | + source.toFile.relativeTo(jsFolder).foreach { relativeSource => |
| 243 | + val dest = distFolder / relativeSource.toString |
| 244 | + dest.getParentFile.mkdirs() |
| 245 | + Files.copy(source, dest.toPath, REPLACE_EXISTING) |
| 246 | + } |
| 247 | + }) |
| 248 | + |
| 249 | + // link the proper js bundle |
| 250 | + val indexFrom = baseDirectory.value / "src/main/js/index.html" |
| 251 | + val indexTo = distFolder / "index.html" |
| 252 | + |
| 253 | + val indexPatchedContent = { |
| 254 | + import collection.JavaConverters._ |
| 255 | + Files |
| 256 | + .readAllLines(indexFrom.toPath, IO.utf8) |
| 257 | + .asScala |
| 258 | + .map(_.replaceAllLiterally("-fastopt", "-opt")) |
| 259 | + .mkString("\n") |
| 260 | + } |
| 261 | + |
| 262 | + Files.write(indexTo.toPath, indexPatchedContent.getBytes(IO.utf8)) |
| 263 | + distFolder |
| 264 | + } |
| 265 | + ) |
| 266 | + |
197 | 267 | lazy val spraWeb = (project in file("spra-web"))
|
198 |
| - .dependsOn(spraApi.js) |
199 |
| - .configure(bundlerSettings, baseLibSettings) |
| 268 | + .dependsOn(spraApi.js, spraPlayServer) |
| 269 | + .configure(bundlerSettings, baseLibSettings, browserProject, spraWebBuildInfoSettings) |
200 | 270 | .configure(_.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin))
|
201 | 271 | .settings(
|
202 | 272 | scalaVersion := "2.13.8",
|
203 | 273 | crossScalaVersions := Seq("2.13.8", "3.1.2"),
|
204 | 274 | name := "spra-web",
|
205 | 275 | Test / fork := false, // sjs needs this to run tests
|
| 276 | + scalaJSUseMainModuleInitializer := true, |
| 277 | + scalaJSLinkerConfig := scalaJSLinkerConfig.value.withSourceMap(false), |
| 278 | + webpackDevServerPort := 8081, |
| 279 | + webpackBundlingMode := BundlingMode.LibraryOnly(), |
206 | 280 | libraryDependencies ++= Seq(
|
207 | 281 | "org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0",
|
208 | 282 | "me.shadaj" %%% "slinky-core" % "0.7.3",
|
@@ -238,3 +312,5 @@ lazy val root = (project in file("."))
|
238 | 312 | publishLocal := {},
|
239 | 313 | publish / skip := true
|
240 | 314 | )
|
| 315 | + |
| 316 | +addCommandAlias("spra-admin", ";spraWeb/fastOptJS::startWebpackDevServer;~spraWeb/fastOptJS") |
0 commit comments