summaryrefslogtreecommitdiff
path: root/bellsprout/scripts/treeshake.clj
blob: 96c4009782ccf07443ffceeb866cc836fb9329a4 (plain)
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
#!/usr/bin/env bb
(ns treeshake)

(require '[babashka.tasks :refer [shell]])
(require '[babashka.fs :as fs])
(require '[cheshire.core :as json])

(defn treeshake [content original target]
  (let [shake (partial shell {:out :string}
                       "npx" "purgecss"
                       "--css" original
                       "--content")
        out (apply shake content)
        css (-> out :out json/parse-string first (get "css"))]
    (spit target css)))

(def original-css "resources/public/css/pico.min.css")
(def target-css "resources/public/css/pico.purged.css")
(def html-file "resources/public/index.html")
(def js-file "src/bellsprout/core.cljs")

(defn treeshake-pico []
  (treeshake [html-file js-file] original-css target-css)
  (if (fs/exists? target-css)
    (let [size #(.length (java.io.File. %))
          old-size (size original-css)
          new-size (size target-css)]
      (println "Tree shaken. Purged CSS file size:" new-size "Bytes"
               (str "(" (- new-size old-size) " Bytes)")))
    (println "Something went wrong.")))

(defn -main []
  (treeshake-pico))