Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed May 24, 2024
1 parent 695aa19 commit 773abba
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
21 changes: 19 additions & 2 deletions project/src/htmlGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 44 additions & 15 deletions project/src/live.server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <script __REPLACE_WITH_MODULE_HEADERS__\\> in the header tag/>"
)
.validate(
"index.html must be a file, with a .html extension, and must contain the exact script tag <script __REPLACE_WITH_MODULE_HEADERS__/> template cannot be blank"
) {
path =>
os.isFile(os.Path(path)) match
case false => false
case true =>
val f = os.Path(path)
f.ext match
case "html" =>
os.read.lines(f).exists(_.contains("<script __REPLACE_WITH_MODULE_HEADERS__/>"))
case _ => false
end match

}
.map {
path =>
os.read(os.Path(path))
}
.orNone

val millModuleNameOpt: Opts[Option[String]] = Opts
.option[String](
"mill-module-name",
Expand All @@ -323,7 +350,8 @@ object LiveServer
buildToolOpt,
openBrowserAtOpt,
extraBuildArgsOpt,
millModuleNameOpt
millModuleNameOpt,
indexHtmlTemplateOpt
).mapN {
(
baseDir,
Expand All @@ -336,7 +364,8 @@ object LiveServer
buildTool,
openBrowserAt,
extraBuildArgs,
millModuleName
millModuleName,
indexHtmlTemplate
) =>

scribe
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ CI builds a container image which is ready to roll.
To run this from a shell, try something like this:

```sh
cs launch io.github.quafadas::live-server-scala-cli-js:0.0.8 -- --project-dir /Users/simon/Code/viteless --port 3000 --build-tool scala-cli --out-dir /Users/simon/Code/viteless/out --browse-on-open-at /
cs launch io.github.quafadas:live-server-scala-cli-js_3:0.0.9 -- --project-dir /Users/simon/Code/viteless --port 3000 --build-tool scala-cli --out-dir /Users/simon/Code/viteless/out --browse-on-open-at /
```

0 comments on commit 773abba

Please sign in to comment.