Skip to content

Commit 23e422e

Browse files
Move hashing css file step from shadow-cljs hook to build (#3)
1 parent 8e1ae36 commit 23e422e

File tree

3 files changed

+45
-52
lines changed

3 files changed

+45
-52
lines changed

build.clj

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
(ns build
2-
(:require [clojure.tools.build.api :as b]))
2+
(:require [clojure.tools.build.api :as b]
3+
[clojure.java.io :as io]
4+
[clojure.string :as str])
5+
(:import [java.security MessageDigest]
6+
[java.math BigInteger]
7+
[java.nio.file Paths]))
38

49
(def ^:private TARGET-DIR "target")
510
(def ^:private CLASS-DIR (format "%s/classes" TARGET-DIR))
@@ -25,10 +30,48 @@
2530
:basis @basis
2631
:main main-ns}))
2732

33+
(defn- md5
34+
[^String s]
35+
(->> (.getBytes s)
36+
(.digest (MessageDigest/getInstance "MD5"))
37+
(BigInteger. 1)
38+
(format "%032x")))
39+
40+
41+
(defn- join-path
42+
[p & ps]
43+
(str (.normalize (Paths/get p (into-array String ps)))))
44+
45+
46+
(defn- file-name-with-hash
47+
[content-hash file-name-in-html]
48+
(let [file-name-split (str/split file-name-in-html #"\.")
49+
new-file-name-split (-> file-name-split
50+
(butlast)
51+
(concat [content-hash (last file-name-split)]))]
52+
(str/join "." new-file-name-split)))
53+
54+
55+
(defn- hash-css
56+
"Rename css file in html with hash of the file."
57+
[{:keys [output-file-path file-name-in-html html-file-path]
58+
:or {html-file-path "resources/public/index.html"}}]
59+
(let [output-file (io/file output-file-path)
60+
new-file-name (-> (slurp output-file)
61+
(md5)
62+
(file-name-with-hash file-name-in-html))
63+
new-file-path (join-path (.getParent output-file) new-file-name)
64+
html (slurp html-file-path)]
65+
(.renameTo output-file (io/file new-file-path))
66+
(->> (str/replace html file-name-in-html new-file-name)
67+
(spit html-file-path))))
68+
2869
(defn build
2970
"Build an uberjar."
3071
[_]
3172
(clean)
73+
(hash-css {:output-file-path "resources/public/css/output-prod.css"
74+
:file-name-in-html "output.css"})
3275
(uber {:uber-file UBER-FILE
3376
:class-dir CLASS-DIR
3477
:main-ns MAIN-NS}))

shadow-cljs.edn

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
:devtools {:watch-dir "resources/public"
88
:watch-path "/assets"
99
:after-load ui.main/render!}
10-
:release {:build-hooks [; Update file name in index.html with hash
11-
(api.util.build/hash-css
12-
{:output-file-path "resources/public/css/output-prod.css"
13-
:file-name-in-html "output.css"})
14-
; Update js file name in index.html with hash
10+
:release {:build-hooks [; Update js file name in index.html with hash
1511
(shadow.html/copy-file
1612
"resources/public/index.html"
1713
"resources/public/index.html")]}}}}

src/clj/api/util/build.clj

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)