Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions plugin/integration/src/normal.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ object WebAppModuleTests extends TestSuite:
val html = os.read(siteDir / "index.html")

// 1. Must NOT reference the static unhashed name
assert(!html.contains("src=\"/main.js\""))
assert(!html.contains("src=\"./main.js\""))

// 2. Extract all <script src="..."> references from the HTML
val scriptSrcPattern = """src="/([^"]+\.js)"""".r
val scriptSrcPattern = """src="\./([^"]+\.js)"""".r
val scriptRefs = scriptSrcPattern.findAllMatchIn(html).map(_.group(1)).toList
if scriptRefs.isEmpty then
throw new java.lang.AssertionError(s"No <script src=...> found in index.html:\n$html")
Expand Down Expand Up @@ -180,7 +180,7 @@ object WebAppModuleTests extends TestSuite:
end if

// Extract all <script src="..."> references
val scriptSrcPattern = """src="/([^"]+\.js)"""".r
val scriptSrcPattern = """src="\./([^"]+\.js)"""".r
val scriptRefs = scriptSrcPattern.findAllMatchIn(html).map(_.group(1)).toList
if scriptRefs.isEmpty then
throw new java.lang.AssertionError(s"No <script src=...> found in publish index.html:\n$html")
Expand Down
35 changes: 2 additions & 33 deletions plugin/src/ScalaJsWebAppModule.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package io.github.quafadas.sjsls

import scalatags.Text.all.*

import mill.api.Task.Simple
import mill.scalajslib.*
import mill.scalajslib.api.Report
import mill.PathRef
import mill.Task

Expand All @@ -13,20 +8,7 @@ trait ScalaJsWebAppModule extends FileBasedContentHashScalaJSModule with ScalaJs
def publish = Task {
val report = fullLinkJS()
val minifiedDir = report.dest.path

val scriptTags = report.publicModules.map(m => script(src := s"/${m.jsFileName}", `type` := "module"))

val bodyHtml = body(
frag(scriptTags.toSeq*),
div(id := appRoot)
).render

val doc =
"<!doctype html>\n" +
html(
raw(indexHtmlHead()),
raw(bodyHtml)
).render
val doc = fullDocHtml(indexHtmlHead(), bodyHtmlFromReport(report))

os.write(Task.dest / "index.html", doc)
os.list(minifiedDir).foreach(f => os.copy(f, Task.dest / f.last, replaceExisting = true))
Expand Down Expand Up @@ -69,20 +51,7 @@ trait ScalaJsInMemWebAppModule extends InMemoryFastLinkHashScalaJSModule with Sc
def publish = Task {
val report = fullLinkJS()
val minifiedDir = report.dest.path

val scriptTags = report.publicModules.map(m => script(src := s"/${m.jsFileName}", `type` := "module"))

val bodyHtml = body(
frag(scriptTags.toSeq*),
div(id := appRoot)
).render

val doc =
"<!doctype html>\n" +
html(
raw(indexHtmlHead()),
raw(bodyHtml)
).render
val doc = fullDocHtml(indexHtmlHead(), bodyHtmlFromReport(report))

os.write(Task.dest / "index.html", doc)
os.list(minifiedDir).foreach(f => os.copy(f, Task.dest / f.last, replaceExisting = true))
Expand Down
32 changes: 16 additions & 16 deletions plugin/src/refresh_plugin.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package io.github.quafadas.sjsls

import scalatags.Text.all.*

import fs2.concurrent.Topic
Expand Down Expand Up @@ -45,19 +46,24 @@ trait ScalaJsRefreshModule extends ScalaJSConfigModule:

def titleString: String = "App"

def indexHtmlBody = Task {
val report = fastLinkJS()
val scriptTags = report
.publicModules
.map {
m =>
script(src := s"/${m.jsFileName}", `type` := "module")
}
def bodyHtmlFromReport(report: Report, basePath: String = "./", includeRefresh: Boolean = false): String =
val scriptTags = report.publicModules.map(m => script(src := s"$basePath${m.jsFileName}", `type` := "module"))
body(
frag(scriptTags.toSeq*),
div(id := appRoot),
raw(refreshScript)
if includeRefresh then raw(refreshScript) else frag()
).render
end bodyHtmlFromReport

def fullDocHtml(headHtml: String, bodyHtml: String): String =
"<!doctype html>\n" +
html(
raw(headHtml),
raw(bodyHtml)
).render

def indexHtmlBody = Task {
bodyHtmlFromReport(fastLinkJS(), includeRefresh = true)
}

def refreshScript: String =
Expand All @@ -79,13 +85,7 @@ trait ScalaJsRefreshModule extends ScalaJSConfigModule:
).render

def indexHtml = Task {
val doc =
"<!doctype html>\n" +
html(
raw(indexHtmlHead()),
raw(indexHtmlBody())
).render

val doc = fullDocHtml(indexHtmlHead(), indexHtmlBody())
os.write.over(Task.dest / "index.html", doc)
PathRef(Task.dest / "index.html")
}
Expand Down
Loading