Skip to content

Instantly share code, notes, and snippets.

@megri
Created January 31, 2025 23:37
Show Gist options
  • Save megri/52c1b68f44e57e13a762d16450751b96 to your computer and use it in GitHub Desktop.
Save megri/52c1b68f44e57e13a762d16450751b96 to your computer and use it in GitHub Desktop.
Mill Tailwind self-managed integration
trait TailwindModule extends ScalaJSModule {
def tailwindVersion: Target[String] = Task("v3.4.14")
/** Tailwindcss download url. By default autodetected from props os.name and os.arch
*/
def tailwindBinUrl = Task {
def nameMatch(prefix: String) =
sys.props("os.name").toLowerCase.startsWith(prefix.toLowerCase)
val platform =
if (nameMatch("mac os x")) "macos"
else if (nameMatch("linux")) "linux"
else if (nameMatch("windows")) "windows"
val arch = sys.props("os.arch") match {
case "aarch64" => "arm64"
case "amd64" | "x64" => "x64"
case "arm" => "armv7"
}
val version = tailwindVersion()
s"https://github.com/tailwindlabs/tailwindcss/releases/download/$version/tailwindcss-$platform-$arch"
}
def tailwindBin: Target[Path] = Task {
val binPath = T.dest / "tailwindcss"
os.write(
target = binPath,
data = requests.get.stream(tailwindBinUrl()),
perms = os.PermSet(Integer.parseInt("755", 8))
)
binPath
}
def webSrcDir: Target[Path] = Task.workspace / "web-src"
def devDistDir: Target[Path] = Task.workspace / "dev-dist"
def tailwindInputCss: Target[PathRef] = Task.Source {
webSrcDir() / "tailwind.css"
}
def tailwindOutputName: Target[String] = "styles.css"
def indexHtmlPath: Target[PathRef] = Task.Source {
webSrcDir() / "index.html"
}
def tailwindOutput: Target[Path] = Task {
val jsFilePath = os.list(fastLinkJS().dest.path).find(_.ext == "js").get
val outPath = T.dest / "compiled.css"
// format: off
os.proc(
tailwindBin(),
"--input", tailwindInputCss(),
"--output", outPath,
"--content", s"$jsFilePath,${indexHtmlPath().path}",
).call(cwd = Task.workspace)
// format: on
outPath
}
def devBuild(): Command[PathRef] = Task.Command {
val targetDir = devDistDir()
os.copy.over(webSrcDir() / "img", targetDir / "img")
os.copy.into(indexHtmlPath().path, targetDir, replaceExisting = true)
os.copy.over(tailwindOutput(), targetDir / tailwindOutputName())
os.list(fastLinkJS().dest.path).foreach(path => os.copy.into(path, targetDir, replaceExisting = true))
PathRef(T.dest)
}
}
// usage: mill frontend.devBuild
object frontend extends TailwindModule {
// change "plugin" settings as desired here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment