Skip to content

Commit 068aac2

Browse files
committed
[base-3] Halve doc size by excluding the unused font-awesome glyphs
1 parent b917703 commit 068aac2

File tree

3 files changed

+126
-8
lines changed

3 files changed

+126
-8
lines changed

build.sbt

+3-3
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ publish / skip := true
266266

267267
enablePlugins(StageWebsitePlugin)
268268
webSnapshotVariants := Map(
269-
"2.12" -> (base.jvm(scala212Ver) / Compile / doc).value,
270-
"2.13" -> (base.jvm(scala213Ver) / Compile / doc).value,
271-
"3" -> (base.jvm(scala3Ver) / Compile / doc).value,
269+
"2.12" -> (base.jvm(scala212Ver) / Compile / packageDoc / mappings).value,
270+
"2.13" -> (base.jvm(scala213Ver) / Compile / packageDoc / mappings).value,
271+
"3" -> (base.jvm(scala3Ver) / Compile / packageDoc / mappings).value,
272272
)
273273
webStage / mappings += ((base.jvm(scala3Ver) / sourceDirectory).value / "docs" / "versions.json") -> "versions.json",

project/OptimizeScaladocPlugin.scala

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package name.rayrobdod.stringContextParserCombinator.build
2+
3+
import sbt._
4+
import sbt.Keys._
5+
6+
object OptimizeScaladocPlugin extends AutoPlugin {
7+
override def requires = sbt.plugins.JvmPlugin
8+
override def trigger = allRequirements
9+
10+
object autoImport {
11+
val optdocMakeReplacementFontawesomeCss = taskKey[(File, String)]("")
12+
val optdocMakeReplacementFontawesomeIcons = taskKey[Seq[(File, String)]]("")
13+
}
14+
import autoImport._
15+
16+
override val globalSettings = Seq(
17+
)
18+
19+
private def states: Seq[(String, Seq[String], String, String)] = Seq(
20+
("default", Seq("", ":disabled", ":focus"), "#6F6E77", "#A09FA6"),
21+
("hover", Seq(":hover", ":active"), "#1A1523", "#CCCCCC"),
22+
("selected", Seq(".selected"), "#1A1523", "#EDEDEF"),
23+
)
24+
25+
private def unscopedSettings = Seq(
26+
optdocMakeReplacementFontawesomeCss := {
27+
sbt.IO.createDirectory(target.value / "docopt")
28+
val outFile = target.value / "docopt" / "fontawesome.css"
29+
val clockLines = {
30+
val icon = "clock"
31+
for {
32+
theme <- Seq("light", "dark");
33+
themeClass = if (theme == "light") {""} else {s".theme-$theme "};
34+
line <- Seq(
35+
s"${themeClass}.fas.fa-${icon}::before {",
36+
s" content: url('../images/fontawesome/${icon}/${theme}/default.svg')",
37+
"}"
38+
)
39+
} yield line
40+
}
41+
val playLines = {
42+
val icon = "play"
43+
for {
44+
theme <- Seq("light", "dark");
45+
themeClass = if (theme == "light") {""} else {s".theme-$theme "};
46+
(state, stateClasses, _, _) <- states;
47+
stateClass <- stateClasses;
48+
line <- Seq(
49+
s"${themeClass}.icon-button.run-button${stateClass}::before {",
50+
s" content: url('../images/fontawesome/${icon}/${theme}/${state}.svg')",
51+
"}"
52+
)
53+
} yield line
54+
}
55+
56+
sbt.io.IO.writeLines(outFile, playLines ++ clockLines)
57+
outFile -> "styles/fontawesome.css"
58+
},
59+
optdocMakeReplacementFontawesomeIcons := {
60+
val outDir = target.value / "docopt" / "icons"
61+
sbt.IO.createDirectory(outDir)
62+
63+
for {
64+
icon <- Seq("play", "clock");
65+
theme <- Seq("light", "dark");
66+
(state, _, lightColor, darkColor) <- if (icon == "play") {states} else {states.take(1)}
67+
} yield {
68+
val color = if (theme == "light") {lightColor} else {darkColor}
69+
val lines = icon match {
70+
case "play" => Seq(
71+
s"""<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512" width="11.5" height="15.333" fill="${color}">""",
72+
"<!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.-->",
73+
"""<path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80L0 432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/>""",
74+
"</svg>",
75+
)
76+
case "clock" => Seq(
77+
s"""<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="13" height="13" fill="${color}">""",
78+
"<!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.-->",
79+
"""<path d="M256 0a256 256 0 1 1 0 512A256 256 0 1 1 256 0zM232 120l0 136c0 8 4 15.5 10.7 20l96 64c11 7.4 25.9 4.4 33.3-6.7s4.4-25.9-6.7-33.3L280 243.2 280 120c0-13.3-10.7-24-24-24s-24 10.7-24 24z"/>""",
80+
"</svg>",
81+
)
82+
}
83+
val outFile = outDir / s"$icon-$theme-$state.svg"
84+
sbt.io.IO.writeLines(outFile, lines)
85+
outFile -> s"images/fontawesome/${icon}/${theme}/${state}.svg"
86+
}
87+
},
88+
)
89+
90+
private def perScopeSettings(config:sbt.librarymanagement.Configuration) = Seq(
91+
config / packageDoc / mappings := {
92+
scalaBinaryVersion.value match {
93+
case "3" =>
94+
(config / packageDoc / mappings).value
95+
.filterNot({filePath =>
96+
import java.io.File.separator
97+
val path = filePath._2
98+
path.startsWith(s"images${separator}icon-buttons${separator}twitter${separator}") ||
99+
path.startsWith(s"images${separator}twitter-icon-") ||
100+
path.startsWith(s"images${separator}icon-buttons${separator}gitter${separator}") ||
101+
path.startsWith(s"images${separator}gitter-icon-") ||
102+
path.startsWith(s"styles${separator}fontawesome.css") ||
103+
path.startsWith(s"webfonts${separator}fa-") ||
104+
false
105+
})
106+
.:+(optdocMakeReplacementFontawesomeCss.value)
107+
.++(optdocMakeReplacementFontawesomeIcons.value)
108+
case _ =>
109+
(config / packageDoc / mappings).value
110+
}
111+
},
112+
)
113+
114+
override lazy val projectSettings = unscopedSettings ++ perScopeSettings(Compile) ++ perScopeSettings(Test)
115+
}

project/StageWebsitePlugin.scala

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ object StageWebsitePlugin extends AutoPlugin {
1616
object autoImport {
1717
val webStage = taskKey[Seq[File]]("Create a local directory with all the files laid out as they would be in the website")
1818
val webMakePagesIndex = taskKey[File]("Generates a root index page for the multi-versioned documentation site")
19-
val webSnapshotVariants = taskKey[Map[String, File]]("snapshot documentations to include in the multi-versioned documentation site")
20-
val webPublishedVariants = taskKey[Map[String, Map[String, File]]]("published documentations to include in the multi-versioned documentation site")
19+
val webSnapshotVariants = taskKey[Map[String, Seq[(File, String)]]]("snapshot documentations to include in the multi-versioned documentation site")
20+
val webPublishedVariants = taskKey[Map[String, Map[String, Seq[(File, String)]]]]("published documentations to include in the multi-versioned documentation site")
2121
}
2222
import autoImport._
2323

@@ -45,7 +45,7 @@ object StageWebsitePlugin extends AutoPlugin {
4545

4646
val unzipdir = cacheDir / "unzip" / scalaVersion / myVersion
4747
sbt.IO.unzip(docjar, unzipdir)
48-
scalaVersion -> unzipdir
48+
scalaVersion -> ((unzipdir ** AllPassFilter).pair(f => unzipdir.relativize(f).map(_.toString)))
4949
}).toMap
5050
}).toMap
5151
}.tag(sbt.Tags.Network)
@@ -112,8 +112,10 @@ object StageWebsitePlugin extends AutoPlugin {
112112
val variants = publishedVariants + snapshotVariants
113113

114114
variants.toSeq.flatMap({case (myVersion, myVersionVariants) =>
115-
myVersionVariants.toSeq.flatMap({case (scalaVersion, rootdir) =>
116-
(rootdir ** AllPassFilter).pair(f => rootdir.relativize(f).map(p => s"${myVersion}${webDirectorySuffixForScalaVersion(scalaVersion)}/${p.toString}"))
115+
myVersionVariants.toSeq.flatMap({case (scalaVersion, mappings) =>
116+
mappings.map({case (file, path) =>
117+
((file, s"${myVersion}${webDirectorySuffixForScalaVersion(scalaVersion)}/${path}"))
118+
})
117119
})
118120
})
119121
},
@@ -127,6 +129,7 @@ object StageWebsitePlugin extends AutoPlugin {
127129

128130
(webStage / mappings).value.map{case (srcFile, name) =>
129131
val tarFile = tarDir / name
132+
Files.createDirectories(tarFile.getParentFile.toPath)
130133
Files.copy(srcFile.toPath, tarFile.toPath, COPY_ATTRIBUTES, REPLACE_EXISTING)
131134
tarFile
132135
}

0 commit comments

Comments
 (0)