diff --git a/.gitignore b/.gitignore index c9cc7f2..8df43eb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ .scala-build secrets.env out + +node_modules diff --git a/build.mill b/build.mill index 47e738f..25afce8 100644 --- a/build.mill +++ b/build.mill @@ -4,6 +4,10 @@ package build import mill.*, scalalib.*, javascriptlib.* import mill.api.Task.Simple +import java.net.URI +import java.nio.file.* +import java.nio.file.attribute.BasicFileAttributes +import java.util trait ScalaCommon extends ScalaModule: def scalaVersion = "3.7.2" @@ -15,10 +19,6 @@ trait ScalaCommon extends ScalaModule: "-Wvalue-discard" ) -object js extends TypeScriptModule: - def npmDeps = Seq("morphdom@2.7.7") - def mainFileName = "index.ts" - object core extends ScalaCommon: // Replace with ujson, core shouldn't depend on ZIO def mvnDeps = Seq(mvn"dev.zio::zio-json:0.7.44") @@ -44,9 +44,30 @@ object core extends ScalaCommon: object test extends ScalaTests with scalalib.TestModule.Utest: def utestVersion = "0.9.0" + object js extends TypeScriptModule: + def npmDeps = Seq("morphdom@2.7.7") + def mainFileName = "index.ts" + object zio extends ScalaCommon: def mvnDeps = Seq(mvn"dev.zio::zio-http:3.4.0") def moduleDeps = Seq(core) object example extends ScalaCommon: def moduleDeps = Seq(zio) + + def scaliveBundle = Task { + os.copy( + from = example.js.bundle().path, + to = Task.dest / "public" / "app.js", + createFolders = true + ) + PathRef(Task.dest) + } + + def resources = Task { + super.resources() ++ Seq(scaliveBundle()) + } + + object js extends TypeScriptModule: + def mainFileName = "app.ts" + def moduleDeps = Seq(core.js) diff --git a/js/src/index.ts b/core/js/src/index.ts similarity index 84% rename from js/src/index.ts rename to core/js/src/index.ts index 1698bf8..588fb52 100644 --- a/js/src/index.ts +++ b/core/js/src/index.ts @@ -8,4 +8,4 @@ el2.className = 'bar'; morphdom(el1, el2); -console.log("ok"); +export const scalive = "scalive" diff --git a/core/src/scalive/Scalive.scala b/core/src/scalive/Scalive.scala index 7b1312e..59584a6 100644 --- a/core/src/scalive/Scalive.scala +++ b/core/src/scalive/Scalive.scala @@ -3,6 +3,9 @@ import scalive.defs.complex.ComplexHtmlKeys import scalive.defs.tags.HtmlTags package object scalive extends HtmlTags with HtmlAttrs with ComplexHtmlKeys: + + lazy val defer = htmlAttr("defer", codecs.BooleanAsOnOffStringCodec) + implicit def stringToMod(v: String): Mod = Mod.Content.Text(v) implicit def htmlElementToMod(el: HtmlElement): Mod = Mod.Content.Tag(el) implicit def dynStringToMod(d: Dyn[String]): Mod = Mod.Content.DynText(d) diff --git a/example/js/src/app.js b/example/js/src/app.js new file mode 100644 index 0000000..46a2147 --- /dev/null +++ b/example/js/src/app.js @@ -0,0 +1,3 @@ +import { scalive } from "core/js/index" + +console.log(scalive) diff --git a/example/src/Example.scala b/example/src/Example.scala index 8112ad5..a7d9a66 100644 --- a/example/src/Example.scala +++ b/example/src/Example.scala @@ -17,4 +17,6 @@ object Example extends ZIOAppDefault: ) ) - override val run = Server.serve(liveRouter.routes).provide(Server.default) + val routes = liveRouter.routes @@ Middleware.serveResources(Path.empty / "static", "public") + + override val run = Server.serve(routes).provide(Server.default) diff --git a/example/src/RootLayout.scala b/example/src/RootLayout.scala index a4903bb..456f8af 100644 --- a/example/src/RootLayout.scala +++ b/example/src/RootLayout.scala @@ -5,7 +5,8 @@ object RootLayout: htmlRootTag( lang := "en", headTag( - metaTag(charset := "utf-8") + metaTag(charset := "utf-8"), + scriptTag(defer := true, typ := "text/javascript", src := "/static/app.js") ), bodyTag( content