diff options
author | Jakob Odersky <jakob@inpher.io> | 2019-11-25 23:23:59 -0500 |
---|---|---|
committer | Jakob Odersky <jakob@inpher.io> | 2019-11-26 13:40:36 -0500 |
commit | 2f298c65846b2f62b9b40cd23f11971b301829f3 (patch) | |
tree | d415576639ebd35004441c61e9a7aa5c8b06ab00 | |
parent | c458a1bd7d173419232ee6e49f3a06ddb4bb7c39 (diff) | |
download | scala-tutorial-2f298c65846b2f62b9b40cd23f11971b301829f3.tar.gz scala-tutorial-2f298c65846b2f62b9b40cd23f11971b301829f3.tar.bz2 scala-tutorial-2f298c65846b2f62b9b40cd23f11971b301829f3.zip |
Create basic ScalaJS project and serve it from the server
-rw-r--r-- | build.sc | 25 | ||||
-rw-r--r-- | server/src/Templates.scala | 14 | ||||
-rw-r--r-- | webapp/src/Main.scala | 13 |
3 files changed, 49 insertions, 3 deletions
@@ -1,4 +1,4 @@ -import mill._, scalalib._ +import mill._, scalalib._, scalajslib._ object server extends ScalaModule { def scalaVersion = "2.13.1" @@ -10,4 +10,27 @@ object server extends ScalaModule { ivy"org.xerial:sqlite-jdbc:3.28.0" // actual database driver, note the single ':' ) + // This includes the resulting javascript file so that it can be served + // as a classpath resource and is packaged in the final jar. + def localClasspath = T { + super.localClasspath() ++ List(webapp.asAsset()) + } + +} + +object webapp extends ScalaJSModule { + def scalaVersion = "2.13.1" + def scalaJSVersion = "0.6.31" // https://www.scala-js.org/ + + def ivyDeps = Agg( + ivy"org.scala-js::scalajs-dom::0.9.7" // http://scala-js.github.io/scala-js-dom/ + ) + + // friendlier name than the default 'out.js' + def asAsset = T { + val out = T.ctx().dest / "assets" / "app.js" + os.copy(fastOpt().path, out, createFolders = true) + PathRef(T.ctx().dest) + } + } diff --git a/server/src/Templates.scala b/server/src/Templates.scala index a512c49..3abb09e 100644 --- a/server/src/Templates.scala +++ b/server/src/Templates.scala @@ -50,10 +50,20 @@ object Templates { meta( name := "viewport", content := "width=device-width, initial-scale=1, shrink-to-fit=no" - ) + ), + script(`type` := "text/javascript", src := "/assets/app.js") ), body( - conversation(messages) + conversation(messages), + script(`type` := "text/javascript")( + raw( + """|document.addEventListener("DOMContentLoaded", function(event) { + | console.info("Starting ScalaJS application...") + | Main.func() // run ScalaJS application + |}) + |""".stripMargin + ) + ) ) ) diff --git a/webapp/src/Main.scala b/webapp/src/Main.scala new file mode 100644 index 0000000..5aa2f2b --- /dev/null +++ b/webapp/src/Main.scala @@ -0,0 +1,13 @@ + +import org.scalajs.dom +import scalajs.js.annotation + +@annotation.JSExportTopLevel("Main") +object Main { + + @annotation.JSExport + def func() = { + dom.console.log("hello from ScalaJS!") + } + +} |