-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.cljs
65 lines (58 loc) · 2.16 KB
/
build.cljs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(ns build
(:require ["esbuild" :as esbuild]
["fast-glob$default" :as fast-glob]
["node:child_process" :as cp]
["node:fs" :as fs]
[clojure.string :as str]))
(defn write-manifest [result manifest-path]
(let [result' (js->clj result)
manifest (->> (keys (get-in result' ["metafile" "outputs"]))
(map #(str/replace % #"^target/public/js/" ""))
(map (fn [s] [(str/replace s #"^(.+)\.[A-Z0-9]{8}\.js$" "$1.js") s]))
(into {}))
json (js/JSON.stringify (clj->js manifest) nil 2)]
(fs/writeFileSync manifest-path json)))
(def manifest-path "target/public/js/manifest.json")
(def manifest-plugin
{:name "manifest"
:setup (fn [build]
(set! (-> build .-initialOptions .-metafile) true)
(.onEnd build #(write-manifest % manifest-path)))})
(defn ^:async compile-cherry []
(let [cmd (concat ["./node_modules/.bin/cherry" "compile"]
(js/await (fast-glob "src/**/*.{cljc,cljs}"))
["--output-dir=target/cherry"])]
(js/Promise.
(fn [resolve reject]
(.exec cp (str/join " " cmd)
(fn [error stdout stderr]
(if error
(reject error)
(do
(when stderr (js/console.error stderr))
(when stdout (js/console.log stdout))
(resolve)))))))))
(def cherry-loader
{:name "cherry-loader"
:setup (fn [build] (.onStart build compile-cherry))})
(def ctx
(js/await
(.context
esbuild
(clj->js
{:entryPoints ["target/cherry/src/**/*.mjs",
"target/cherry/src/**/*.jsx"]
:entryNames "[dir]/[name].[hash]"
:format "esm"
:outdir "target/public/js/rpub"
:minify (not (contains? (set js/process.argv) "--no-minify"))
:jsx "automatic"
:plugins [cherry-loader manifest-plugin]}))))
(if (contains? (set js/process.argv) "--watch")
(do
(js/await (.watch ctx))
(println "Watching..."))
(do
(js/await (.rebuild ctx))
(println "Building...")
(js/process.exit 0)))