-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinline_css.clj
51 lines (40 loc) · 1.53 KB
/
inline_css.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(ns inline-css (:require [clojure.java.io :as io]
[clojure.string :as str]))
; TODO: This is a hack, using an html parser and then find all the css links and inline them
(defn read-file [file]
(with-open [reader (io/reader file)]
(slurp reader)))
(defn get-html [file]
(with-open [reader (io/reader file)]
(slurp reader)))
(defn fix-escape-chars [s]
; Tailwind uses \: for modifiers, when passed throught str the \ is removed
; so we need to replace it with \\\\: to make sure it is passed through
(str/replace s #"\:" "\\\\:"))
(defn remove-comments [s]
(str/replace s #"/\*[\s\S]*?\*/" ""))
(defn replace-css [a html]
(str/replace html #"<link rel=\"stylesheet\" href=\"/styles.css\"></link>" (str "<style>" a "</style>")))
(defn is-used-var? [name s]
(str/includes? s (str "var(" name ")")))
(defn remove-unused-vars [s]
(let [vars (re-seq #"(--[^\)\:\;\s]*)" s)
unused-vars (filter #(not (is-used-var? (second %) s)) vars)]
(reduce (fn [val [_ name]]
(str/replace val (re-pattern (str name ":[^\\;\\}]*;?")) ""))
s unused-vars)))
(def css (-> "dist/styles.css"
(read-file)
(remove-unused-vars)
(remove-comments)
(fix-escape-chars)))
(defn run-replace [file css]
(->> file
(get-html)
(replace-css css)
(spit file)))
(run-replace "dist/index.html" css)
(run-replace "dist/groups.html" css)
(run-replace "dist/routes.html" css)
(run-replace "dist/about.html" css)
(println "DONE: Inlining css")