/
api.clj
436 lines (344 loc) · 12 KB
/
api.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
(ns shadow.build.api
(:require [cljs.analyzer :as cljs-ana]
[clojure.java.io :as io]
[shadow.build.resolve :as res]
[shadow.build.classpath :as cp]
[shadow.build.npm :as npm]
[shadow.build.modules :as modules]
[shadow.build.compiler :as impl]
[shadow.cljs.util :as util]
[shadow.build.cljs-bridge :as cljs-bridge]
[shadow.build.closure :as closure]
[shadow.build.data :as data]
[shadow.build.output :as output]
[shadow.build.log :as build-log]
[shadow.build.resource :as rc]
[shadow.jvm-log :as log]
[clojure.set :as set]
[shadow.build.babel :as babel])
(:import (java.util.concurrent ExecutorService)))
(defn build-state? [build]
(data/build-state? build))
(defn deep-merge [a b]
(cond
(nil? a)
b
(nil? b)
a
(and (map? a) (map? b))
(merge-with deep-merge a b)
(and (vector? a) (vector? b))
(->> (concat a b)
(distinct)
(into []))
(and (set? a) (set? b))
(set/union a b)
(string? b)
b
(number? b)
b
(boolean? b)
b
(keyword? b)
b
(symbol? b)
b
:else
(throw (ex-info "failed to merge config value" {:a a :b b}))
))
(def default-compiler-options
{:optimizations :none
:static-fns true
;; Seems to be causing issues in reagent
;; TypeError: "'requestAnimationFrame' called on an object that does not implement interface Window"
;; https://github.com/reagent-project/reagent/blob/901607c45fa82868c319fbf87d853b5bab3fa64e/src/reagent/impl/batching.cljs#L16-L24
;; :fn-invoke-direct true
:elide-asserts false
:closure-configurators []
:infer-externs :auto
:language-in :ecmascript-next-in
;; still need to set this since it otherwise ends up using strict mode default
:language-out :ecmascript5
:output-feature-set :es5
:closure-warnings
{:check-types :off}
:source-map-include-sources-content true
:source-map-detail-level :all
:closure-threads
(-> (Runtime/getRuntime)
(.availableProcessors))
:closure-defines
'{goog.DEBUG false
goog.LOCALE "en"
goog.TRANSPILE "never"
goog.ENABLE_DEBUG_LOADER false}})
(def default-build-options
{:print-fn :console
:module-format :goog ;; or :js, maybe :es6 in the future?
:asset-path "js"
;; during development this inludes goog/* sources into the module file
;; this is done to reduce the number of requests made
;; it only inlines goog sources since we don't need source maps for those
:dev-inline-js true
:cljs-runtime-path "cljs-runtime"
:cache-level :all
:par-timeout 60000
;; namespaces that are known to rely on macro side-effects during compilation
;; they will not be cached themselves
;; and files that require them directly won't be cached to ensure that all
;; the expected side-effects can still occur.
:cache-blockers
'#{clara.rules
clara.macros}
})
(def default-js-options
;; don't change the :js-provider default, node targets assume it is :require
{:js-provider :require ;; :closure, :require, :include maybe :webpack, maybe something
:generate-externs true
:packages {}
:source-map-include-sources-content true
:source-map-detail-level :all
})
(defn init []
(-> {:shadow.build/marker true
:project-dir
(-> (io/file "")
(.getAbsoluteFile))
:cache-dir
(io/file "target" "shadow-cljs" "cache")
;; builds can add files during :configure that will trigger a reload in watch
:extra-config-files
#{}
;; {<stage> {<hook-id> <hook-fn}}}
:build-hooks
{}
:logger build-log/stdout
:compiler-options
default-compiler-options
:build-options
default-build-options
:js-options
default-js-options
::compile-cycle 0
;; string property names collected while compiling JS
;; will be used to generate externs for closure
:js-properties
#{}
;; resource-id -> resource
:sources
{}
;; resource-id -> output
:output
{}
;; set of resource-ids that were previously compiled (in watch mode)
;; if the id is present it will skip loading the cache for it on recompile
;; since that will always be stale
:previously-compiled
#{}
;; polyfills injected depending on source
::closure/shadow-js-injected-libs #{}
::closure/classpath-js-injected-libs #{}
::closure/goog-injected-libs #{}
:pending-tasks-ref
(atom [])
:last-progress-ref
(volatile! (System/currentTimeMillis))
;; FIXME: should these ever be configurable?
:analyzer-passes
[cljs-ana/infer-type impl/find-protocols-pass impl/find-js-require-pass]}
(data/init)))
;; helper methods that validate their args, sort of
(defn with-npm [state npm]
{:pre [(npm/service? npm)]}
(assoc state :npm npm))
(defn with-babel [state babel]
{:pre [(babel/service? babel)]}
(assoc state :babel babel))
(defn with-classpath
([state]
(with-classpath state))
([state cp]
{:pre [(cp/service? cp)]}
(assoc state :classpath cp)))
(defn with-logger [state logger]
{:pre [(satisfies? build-log/BuildLog logger)]}
(assoc state :logger logger))
(defn with-cache-dir [state cache-dir]
{:pre [(util/is-file-instance? cache-dir)]}
(assoc state :cache-dir cache-dir))
(defn with-executor [state executor]
{:pre [(instance? ExecutorService executor)]}
(assoc state :executor executor))
(defn with-build-options [state opts]
(update state :build-options deep-merge opts))
(defn merge-build-options [state opts]
(update state :build-options deep-merge opts))
(defn with-compiler-options [state opts]
(update state :compiler-options deep-merge opts))
(defn merge-compiler-options [state opts]
(update state :compiler-options deep-merge opts))
(defn with-js-options [state opts]
(update state :js-options deep-merge opts))
(defn enable-source-maps [state]
(update state :compiler-options merge {:source-map "/dev/null"}))
(defn configure-modules [state modules]
(modules/configure state modules))
(defn analyze-modules
"takes module config and resolves all sources needed to compile"
[state]
(modules/analyze state))
(defn compile-sources
"compiles a list of sources in dependency order
compiles :build-sources if no list is given, use prepare-modules to make :build-sources"
([{:keys [build-sources] :as state}]
(-> state
(cljs-bridge/ensure-compiler-env)
(cljs-bridge/register-ns-aliases)
(cljs-bridge/register-goog-names)
(impl/compile-all build-sources)))
([state source-ids]
(-> state
(assoc :build-sources source-ids)
(compile-sources))))
(defn optimize [{:keys [classpath] :as state}]
(let [deps-externs
(cp/get-deps-externs classpath)]
(-> state
(assoc :deps-externs deps-externs)
(closure/optimize))))
(defn check [state]
(closure/check state))
(defn resolve-entries [state entries]
(res/resolve-entries state entries))
(comment
(defn compile-all-for-ns
"compiles all files required by ns"
[state ns]
(let [state
(prepare-compile state)
deps
(get-deps-for-entry state ns)]
(-> state
(assoc :build-sources deps)
(compile-sources deps))
))
(defn compile-all-for-src
"compiles all files required by src name"
[state src-name]
(let [state
(prepare-compile state)
deps
(get-deps-for-src state src-name)]
(-> state
(assoc :build-sources deps)
(compile-sources deps))
)))
(defn add-closure-configurator
"adds a closure configurator 2-arity function that will be called before the compiler is invoked
signature of the callback is (fn [compiler compiler-options])
Compiler and CompilerOptions are mutable objects, the return value of the callback is ignored
CLJS default configuration is done first, all configurators are applied later and may override
any options.
See:
com.google.javascript.jscomp.Compiler
com.google.javascript.jscomp.CompilerOptions"
[state callback]
(update state :closure-configurators conj callback))
(defn find-resources-affected-by
"returns the set all resources and the immediate dependents of those sources
intended for cache invalidation if one or more resources are changed
a resource may change a function signature and we need to invalidate all namespaces
that may be using that function to immediately get warnings"
[state modified]
{:pre [(set? modified)]}
(->> (:sources state)
(vals)
(map :resource-id)
(filter (fn [other-id]
(let [deps-of
(get-in state [:immediate-deps other-id])
uses-modified-resources
(set/intersection modified deps-of)]
(seq uses-modified-resources)
)))
(into modified)))
(defn reset-resources [state source-ids]
{:pre [(coll? source-ids)]}
(if-not (seq source-ids)
state
(let [modified (set source-ids)
all-deps-to-reset (find-resources-affected-by state modified)]
(reduce
(fn [state resource-id]
(cond
;; fully remove specified resources (eg. modified sources found by watch)
(contains? modified resource-id)
(data/remove-source-by-id state resource-id)
;; do nothing with sources defined in the REPL since the steps that produced
;; them may not be repeatable. just keep them as is.
(get-in state [:sources resource-id :defined-in-repl])
state
;; affected deps only have their output removed since their source didn't change
:else
(data/remove-output-by-id state resource-id)))
state
all-deps-to-reset))))
(defn reset-namespaces [state provides]
(let [source-ids
(->> provides
(map #(get-in state [:sym->id %]))
(remove nil?)
(into #{}))]
(reset-resources state source-ids)))
(defn- macro-test-fn [macros]
(fn [{:keys [type macro-requires source-ns] :as src}]
(when (= :cljs type)
(or (seq (set/intersection macros macro-requires))
;; bootstrap macros
(contains? macros source-ns))
)))
(defn build-affected-by-macros?
"checks whether any sources currently used by the build use any of the given macro namespaces"
[state macros]
{:pre [(set? macros)]}
(->> (:sources state)
(vals)
(some (macro-test-fn macros))))
(defn build-affected-by-macro?
[state macro-ns]
{:pre [(symbol? macro-ns)]}
(build-affected-by-macros? state #{macro-ns}))
(defn find-resources-using-macros [state macros]
(->> (:sources state)
(vals)
(filter (macro-test-fn macros))
(map :resource-id)
(into #{})))
(defn reset-resources-using-macros [state macros]
{:pre [(set? macros)]}
(->> (find-resources-using-macros state macros)
(reset-resources state)))
(defn reset-always-compile-namespaces
"removes all namespaces marked with (ns ^:dev/always some.thing ...) from the build state
so they are recompiled."
[state]
(let [always-compile-source-ids
(->> (:build-sources state)
(filter (fn [source-id]
(let [src (get-in state [:sources source-id])]
(and src
(= :cljs (:type src))
(let [src-meta (get-in src [:ns-info :meta])]
(or (:figwheel-always src-meta)
(:dev/always src-meta)))))))
(into #{}))]
(reset-resources state always-compile-source-ids)
))
(defn add-sources-for-entries
"utility function to simplify testing"
[state entries]
(let [[resolved resolved-state]
(res/resolve-entries state entries)]
;; FIXME: maybe add resolved somewhere
resolved-state
))