diff --git a/project/src/htmlGen.scala b/project/src/htmlGen.scala index 3eeb8a6..9041149 100644 --- a/project/src/htmlGen.scala +++ b/project/src/htmlGen.scala @@ -5,10 +5,27 @@ import fs2.io.file.Path /* * create an html template with that has a head, which includes script tags, that have modulepreload enabled */ + +// def generateHtml(modules: Seq[(Path, String)]) = (template: String => String) => +// template(makeHeader(modules, true).render) + +def injectModulePreloads(modules: Seq[(Path, String)], template: String) = + val modulesStrings = + for + m <- modules + if m._1.toString.endsWith(".js") + yield link(rel := "modulepreload", href := s"${m._1}?hash=${m._2}").render + + // template(makeHeader(modules, true).render) + modulesStrings +end injectModulePreloads + def makeHeader(modules: Seq[(Path, String)], withStyles: Boolean) = val scripts = - for (m <- modules) - yield if m._1.toString.endsWith(".js") then link(rel := "modulepreload", href := s"${m._1}?hash=${m._2}") + for + m <- modules + if m._1.toString.endsWith(".js") + yield link(rel := "modulepreload", href := s"${m._1}?hash=${m._2}") val lessStyle: Seq[Modifier] = if withStyles then diff --git a/project/src/live.server.scala b/project/src/live.server.scala index 80ea6a7..1ecd431 100644 --- a/project/src/live.server.scala +++ b/project/src/live.server.scala @@ -172,23 +172,23 @@ object LiveServer ) ) + val makeIndex = ref.get.flatMap(mp => logger.trace(mp.toString())) >> + Ok( + (ref + .get + .map(_.toSeq.map((path, hash) => (fs2.io.file.Path(path), hash))) + .map(mods => makeHeader(mods, stylesPath.isDefined))) + ) + val overrides = HttpRoutes.of[IO] { case GET -> Root => logger.trace("GET /") >> - ref.get.flatMap(mp => logger.trace(mp.toString())) >> - Ok( - (ref - .get - .map(_.toSeq.map((path, hash) => (fs2.io.file.Path(path), hash))) - .map(mods => makeHeader(mods, stylesPath.isDefined))) - ) + makeIndex + case GET -> Root / "index.html" => - Ok( - (ref - .get - .map(_.toSeq.map((path, hash) => (fs2.io.file.Path(path), hash))) - .map(mods => makeHeader(mods, stylesPath.isDefined))) - ) + logger.trace("GET /index.html") >> + makeIndex + case GET -> Root / "all" => ref .get @@ -299,6 +299,33 @@ object LiveServer ) .orEmpty + val indexHtmlTemplateOpt: Opts[Option[String]] = Opts + .option[String]( + "path-to-index-html-template", + "a path to a file which contains the index.html template you want to use. \n" + + "The file _MUST_ have the EXACT string