-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
shared.clj
279 lines (212 loc) · 6.95 KB
/
shared.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
(ns shadow.build.targets.shared
(:require [clojure.spec.alpha :as s]
[clojure.string :as str]
[clojure.java.io :as io]
[clojure.set :as set]
[cljs.compiler :as cljs-comp]
[shadow.build.api :as cljs]
[shadow.build.data :as data]
[shadow.build.modules :as modules]
[clojure.data.json :as json]))
(defn unquoted-qualified-symbol? [sym]
(and (qualified-symbol? sym)
(not (str/starts-with? (str sym) "'"))))
(defn unquoted-simple-symbol? [sym]
(and (simple-symbol? sym)
(not (str/starts-with? (str sym) "'"))))
(defn non-empty-string? [x]
(and (string? x)
(not (str/blank? x))))
(s/def ::output-dir non-empty-string?)
(s/def ::output-to non-empty-string?)
(s/def ::http-root non-empty-string?)
(s/def ::http-port pos-int?)
(s/def ::http-handler unquoted-qualified-symbol?)
(s/def ::enabled boolean?)
(s/def ::log boolean?)
(s/def ::autoload boolean?)
(s/def ::after-load unquoted-qualified-symbol?)
(s/def ::before-load unquoted-qualified-symbol?)
(s/def ::before-load-async unquoted-qualified-symbol?)
(s/def ::devtools-url non-empty-string?)
(s/def ::use-document-host boolean?)
(s/def ::build-notify unquoted-qualified-symbol?)
(s/def ::devtools
(s/keys
:opt-un
[::http-root
::http-port
::http-handler
::enabled
::log
::autoload
::after-load
::before-load
::before-load-async
::use-document-host
::build-notify
::devtools-url]))
(s/def ::entry
(s/or :sym unquoted-simple-symbol?
:str string?))
(s/def ::init-fn unquoted-qualified-symbol?)
(s/def ::entries
(s/coll-of ::entry :kind vector?))
(s/def ::output-dir non-empty-string?)
(s/def ::asset-path non-empty-string?)
;; OLD, only allowed in config so they don't break.
;; rewritten to :output-dir and :asset-path
(s/def ::public-dir non-empty-string?)
(s/def ::public-path non-empty-string?)
;; ---
;; will just be added as is (useful for comments, license, ...)
(s/def ::prepend string?)
(s/def ::append string?)
;; these go through closure optimized, should be valid js
(s/def ::prepend-js string?)
(s/def ::append-js string?)
(s/def ::depends-on
(s/coll-of keyword? :kind set?))
(s/def ::module
(s/and
;; {init-fn foo.bar/init} should fail
(s/map-of keyword? any?)
(s/keys
:opt-un
[::entries
::entry
::init-fn
::depends-on
::prepend
::prepend-js
::append-js
::append])))
(s/def ::modules
(s/map-of
simple-keyword?
::module
:min-count 1))
(s/def ::chunks
(s/map-of
simple-keyword?
::module
:min-count 1))
(defn prepend [tail head]
{:pre [(vector? head)]}
(into head tail))
(defn hud-defines [hud]
(let [all-off
'{shadow.cljs.devtools.client.hud/show-warnings false
shadow.cljs.devtools.client.hud/show-progress false
shadow.cljs.devtools.client.hud/show-errors false}]
(cond
(true? hud)
{} ;; defaults to all true, no need to set them
(false? hud)
all-off
(set? hud)
(reduce
(fn [m key]
(assoc m
(symbol "shadow.cljs.devtools.client.hud" (str "show-" (name key)))
true))
all-off
hud))))
(comment
(hud-defines #{:progress}))
(defn repl-defines
[{:keys [worker-info] :as state} build-config]
(let [{:keys [proc-id ssl host port]}
worker-info
{:keys [build-id]}
build-config
{:keys [ignore-warnings
devtools-url
build-notify
autoload
use-document-host
use-document-protocol
reload-strategy
repl-pprint
log-style
log]
:as devtools}
(:devtools build-config)]
(merge
{'shadow.cljs.devtools.client.env/enabled
true
'shadow.cljs.devtools.client.env/log
(not (false? log))
'shadow.cljs.devtools.client.env/autoload
(not (false? autoload))
'shadow.cljs.devtools.client.env/module-format
(name (get-in state [:build-options :module-format]))
'shadow.cljs.devtools.client.env/use-document-host
(not (false? use-document-host))
'shadow.cljs.devtools.client.env/use-document-protocol
(true? use-document-protocol)
'shadow.cljs.devtools.client.env/server-host
(or (and (not= host "0.0.0.0") host) "localhost")
'shadow.cljs.devtools.client.env/server-port
port
'shadow.cljs.devtools.client.env/repl-pprint
(true? repl-pprint)
'shadow.cljs.devtools.client.env/ignore-warnings
(true? ignore-warnings)
'shadow.cljs.devtools.client.env/ssl
(true? ssl)
'shadow.cljs.devtools.client.env/reload-strategy
(if (= :full reload-strategy)
"full"
"optimized")
'shadow.cljs.devtools.client.env/build-id
(name build-id)
'shadow.cljs.devtools.client.env/proc-id
(str proc-id)
'shadow.cljs.devtools.client.env/devtools-url
(or devtools-url "")}
(when build-notify
{'shadow.cljs.devtools.client.env/custom-notify-fn (cljs-comp/munge build-notify)})
(when log-style
{'shadow.cljs.devtools.client.env/log-style log-style})
(when (contains? devtools :hud)
(hud-defines (:hud devtools))))))
(defn merge-repl-defines [state config]
(update-in state [:compiler-options :closure-defines] merge (repl-defines state config)))
(defn inject-node-repl
[state {:keys [devtools] :as config}]
(if (false? (:enabled devtools))
state
(-> state
(update-in [:compiler-options :closure-defines] merge (repl-defines state config))
(update-in [::modules/config :main :entries] prepend '[shadow.cljs.devtools.client.node])
)))
(defn set-output-dir [state mode {:keys [id output-dir] :as config}]
(-> state
(cond->
(seq output-dir)
(assoc-in [:build-options :output-dir] (io/file output-dir))
(not (seq output-dir))
(assoc-in [:build-options :output-dir] (data/cache-file state "out")))))
(defn inject-preloads [state module-id config]
(let [preloads (get-in config [:devtools :preloads])]
(if-not (seq preloads)
state
(update-in state [::modules/config module-id :entries] prepend preloads)
)))
(defn bootstrap-host-build? [{:keys [sym->id] :as state}]
(contains? sym->id 'shadow.cljs.bootstrap.env))
(defn bootstrap-host-info [state]
(reduce
(fn [state {:keys [module-id sources] :as mod}]
(let [all-provides
(->> sources
(map #(data/get-source-by-id state %))
(map :provides)
(reduce set/union))
load-str
(str "shadow.cljs.bootstrap.env.set_loaded(" (json/write-str all-provides) ");")]
(update-in state [:output [:shadow.build.modules/append module-id] :js] str "\n" load-str "\n")
))
state
(:build-modules state)))