-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
output.clj
523 lines (427 loc) · 17 KB
/
output.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
(ns shadow.build.output
(:require
[clojure.java.io :as io]
[cljs.source-map :as sm]
[clojure.set :as set]
[clojure.string :as str]
[cljs.compiler :as comp]
[clojure.data.json :as json]
[clojure.tools.logging :as log]
[shadow.build.data :as data]
[shadow.build.resource :as rc]
[shadow.build.log :as build-log]
[shadow.build.async :as async]
[shadow.cljs.util :as util])
(:import (java.io StringReader File ByteArrayOutputStream)
(java.util Base64)
(java.util.zip GZIPOutputStream)
(shadow.build.closure SourceMapReport)))
(defn clean-dir [dir]
(when (.exists dir)
(doseq [file (.listFiles dir)]
(.delete file)
)))
(defn closure-defines-json [state]
(let [closure-defines
(reduce-kv
(fn [def key value]
(let [key (if (symbol? key) (str (comp/munge key)) key)]
(assoc def key value)))
{}
(get-in state [:compiler-options :closure-defines] {}))]
(json/write-str closure-defines :escape-slashes false)))
(defn closure-defines [{:keys [build-options] :as state}]
(let [{:keys [asset-path cljs-runtime-path]} build-options]
(str "var CLOSURE_NO_DEPS = true;\n"
;; goog.findBasePath_() requires a base.js which we dont have
;; this is usually only needed for unoptimized builds anyways
"var CLOSURE_BASE_PATH = '" asset-path "/" cljs-runtime-path "/';\n"
"var CLOSURE_DEFINES = " (closure-defines-json state) ";\n")))
(def goog-base-id
;; can't alias due to cyclic dependency, this sucks
;; goog/base.js is treated special in several cases
[:shadow.build.classpath/resource "goog/base.js"])
(defn closure-defines-and-base [{:keys [asset-path cljs-runtime-path] :as state}]
(let [goog-rc (get-in state [:sources goog-base-id])
goog-base (get-in state [:output goog-base-id :js])]
(when-not (seq goog-base)
(throw (ex-info "no goog/base.js" {})))
;; FIXME: work arround for older cljs versions that used broked closure release, remove.
(when (< (count goog-base) 500)
(throw (ex-info "probably not the goog/base.js you were expecting"
(get-in state [:sources goog-base-id]))))
(str (closure-defines state)
goog-base
"\n")))
(defn ns-only [sym]
{:pre [(qualified-symbol? sym)]}
(symbol (namespace sym)))
(defn fn-call [sym]
{:pre [(qualified-symbol? sym)]}
(str "\n" (comp/munge sym) "();"))
(defn- ns-list-string [coll]
(->> coll
(map #(str "'" (comp/munge %) "'"))
(str/join ",")))
(defn directory? [^File x]
(and (instance? File x)
(or (not (.exists x))
(.isDirectory x))))
(defn closure-goog-deps
([state]
(closure-goog-deps state (-> state :sources keys)))
([{:keys [dead-js-deps] :as state} source-ids]
(->> source-ids
(remove #{goog-base-id})
(map #(get-in state [:sources %]))
(map (fn [{:keys [output-name deps provides] :as rc}]
(str "goog.addDependency(\"" output-name "\",["
(ns-list-string provides)
"],["
(->> (data/deps->syms state rc)
(remove '#{goog})
(remove dead-js-deps)
(ns-list-string))
"]);")))
(str/join "\n"))))
;; FIXME: this could inline everything from a jar since they will never be live-reloaded
;; but it would need to create a proper source-map for the module file
;; since we need that for CLJS files
(defn inlineable? [{:keys [type from-jar provides requires] :as src}]
;; only inline things from jars
(and from-jar
;; only js is inlineable since we want proper source maps for cljs
(= :js type)
;; only inline goog for now
(every? #(str/starts-with? (str %) "goog") requires)
(every? #(str/starts-with? (str %) "goog") provides)
))
(defn line-count [text]
(with-open [rdr (io/reader (StringReader. text))]
(count (line-seq rdr))))
(defn encode-source-map
[{:keys [resource-name prepend output-name] :as src}
{:keys [source source-map] :as output}]
(let [sm-opts
{;; :lines (line-count output)
:file output-name
:preamble-line-count (if-not (seq prepend)
0
(line-count prepend))
:sources-content [source]}]
(-> {output-name source-map}
(sm/encode* sm-opts)
(assoc "sources" [resource-name])
;; its nil which closure doesn't like
(dissoc "lineCount"))))
(defn encode-source-map-json
[src output]
(-> (encode-source-map src output)
(json/write-str)))
(defn generate-source-map-inline
[state
src
{:keys [source-map source-map-json] :as output}
prepend]
(when (or source-map source-map-json)
(let [source-map-json
(or source-map-json
(encode-source-map-json src output))
b64
(-> (Base64/getEncoder)
(.encodeToString (.getBytes source-map-json)))]
(str "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," b64 "\n"))))
(defn generate-source-map
[state
{:keys [resource-name output-name file input] :as src}
{:keys [source source-map source-map-json] :as output}
js-file
prepend]
{:pre [(rc/valid-resource? src)
(map? output)]}
(when (or source-map source-map-json)
(let [sm-text
(str "\n//# sourceMappingURL=" output-name ".map\n")
src-map-file
(io/file (str (.getAbsolutePath js-file) ".map"))
use-fs-path?
(true? (get-in state [:compiler-options :source-map-use-fs-paths]))
source-url
(if (and use-fs-path? file)
(.getAbsolutePath file)
resource-name)
;; FIXME: make this use encode-source-map from above
source-map-json
(or source-map-json
(let [sm-opts
{;; :lines (line-count output)
:file output-name
:preamble-line-count (line-count prepend)
:sources-content [source]}
;; yay or nay on using flat filenames for source maps?
;; personally I don't like seeing only the filename without the path
source-map-v3
(-> {(util/flat-filename resource-name) source-map}
(sm/encode* sm-opts)
(dissoc "lineCount") ;; its nil which closure doesn't like
(assoc "sources" [source-url]))]
(json/write-str source-map-v3 :escape-slash false)))]
(io/make-parents src-map-file)
(spit src-map-file source-map-json)
sm-text)))
(defn flush-source [state src-id]
(let [{:keys [resource-name output-name last-modified] :as src}
(data/get-source-by-id state src-id)
{:keys [js compiled-at] :as output}
(data/get-output! state src)
js-file
(data/output-file state (get-in state [:build-options :cljs-runtime-path]) output-name)]
;; skip files we already have
(when (or (not (.exists js-file))
(zero? last-modified)
;; js is not compiled but maybe modified
(> (or compiled-at last-modified)
(.lastModified js-file)))
(io/make-parents js-file)
(util/with-logged-time
[state {:type :flush-source
:resource-name resource-name}]
(let [output (str js (generate-source-map state src output js-file ""))]
(spit js-file output))))))
(defn flush-sources
([{:keys [build-sources] :as state}]
(flush-sources state build-sources))
([state source-ids]
(doseq [src-id source-ids]
(async/queue-task state #(flush-source state src-id)))
state))
(defmulti flush-optimized-module
(fn [state mod]
(get mod :output-type ::default)))
(defn finalize-module-output
[state {:keys [goog-base prepend output append source-map-name source-map-json module-id output-name sources] :as mod}]
(let [any-shadow-js?
(->> (data/get-build-sources state)
(some #(= :shadow-js (:type %))))
shadow-js-outputs
(->> sources
(map #(data/get-source-by-id state %))
(filter #(= :shadow-js (:type %)))
(map #(data/get-output! state %))
(into []))
shadow-js-prepend
(when (seq shadow-js-outputs)
(let [provides
(->> shadow-js-outputs
(map :js)
(str/join ";\n"))]
(str provides
(when (seq provides)
";\n"))))
final-output
(str (when (and any-shadow-js? goog-base)
"var shadow$provide = {};\n")
prepend
shadow-js-prepend
output
append)]
{:output final-output
;; for source maps
:shadow-js-outputs shadow-js-outputs
:prepend-offset
(-> 0
(cond->
(seq prepend)
(+ (line-count prepend))
(and goog-base)
(inc) ;; var shadow$provide ...
))}))
(defmethod flush-optimized-module ::default
[state {:keys [dead source-map-json module-id output-name] :as mod}]
(if dead
(util/log state {:type :dead-module
:module-id module-id
:output-name output-name})
(let [{:keys [output prepend-offset shadow-js-outputs]}
(finalize-module-output state mod)
target
(data/output-file state output-name)
source-map-name
(str output-name ".map")
final-output
(str output
(when source-map-json
(str "\n//# sourceMappingURL=" source-map-name "\n")))]
(io/make-parents target)
(spit target final-output)
(util/log state {:type :flush-module
:module-id module-id
:output-name output-name
:js-size (count final-output)})
(when source-map-json
(let [sm-index
(-> {:version 3
:file output-name
:offset prepend-offset
:sections []}
(util/reduce->
(fn [{:keys [offset] :as sm-index}
{:keys [js source-map-json] :as src}]
(let [sm
(json/read-str (or source-map-json "{}"))
lines
(line-count js)]
(-> sm-index
(update :offset + lines)
(update :sections conj {:offset {:line offset :column 0}
:map sm}))))
shadow-js-outputs))
sm
(json/read-str source-map-json)
sm-index
(-> sm-index
(update :sections conj {:offset {:line (:offset sm-index) :column 0} :map sm})
(dissoc :offset))
target
(data/output-file state source-map-name)]
(spit target
(json/write-str sm-index)))))))
(defn flush-optimized
;; FIXME: can't alias this due to cyclic dependency
[{modules :shadow.build.closure/modules
:keys [build-options]
:as state}]
(when-not (seq modules)
(throw (ex-info "flush before optimize?" {})))
(when (= :js (get-in state [:build-options :module-format]))
(let [env-file
(data/output-file state "cljs_env.js")]
(io/make-parents env-file)
(spit env-file
(str "module.exports = {};\n"))))
(util/with-logged-time
[state {:type :flush-optimized}]
(doseq [mod modules]
(flush-optimized-module state mod))
;; with-logged-time expects that we return the compiler-state
state
))
(defn js-module-root [sym]
(let [s (comp/munge (str sym))]
(if-let [idx (str/index-of s ".")]
(subs s 0 idx)
s)))
(defn js-module-src-prepend [state {:keys [resource-id resource-name output-name provides requires deps] :as src} require?]
(let [dep-syms
(data/deps->syms state src)
roots
(into #{"goog"} (map js-module-root) dep-syms)]
(str (when require?
(str "var $CLJS = require(\"./cljs_env\");\n"
"var $jscomp = $CLJS.$jscomp;\n"))
;; the only actually global var goog sometimes uses that is not on goog.global
;; actually only: goog/promise/thenable.js goog/proto2/util.js?
(when (str/starts-with? resource-name "goog")
"var COMPILED = false;\n")
(when require?
;; emit requires to actual files to ensure that they were loaded properly
;; can't ensure that the files were loaded before this as goog.require would
(->> dep-syms
(remove #{'goog})
(map #(data/get-source-id-by-provide state %))
(distinct)
(map #(data/get-source-by-id state %))
(remove util/foreign?)
(map (fn [{:keys [output-name] :as x}]
(str "require(\"./" output-name "\");")))
(str/join "\n")))
"\n"
;; require roots will exist
(->> roots
(map (fn [root]
(str "var " root "=$CLJS." root ";")))
(str/join "\n"))
"\n"
;; provides may create new roots
(->> provides
(map js-module-root)
(remove roots)
(map (fn [root]
(str "var " root "=$CLJS." root " || ($CLJS." root " = {});")))
(str/join "\n"))
"\n$CLJS.SHADOW_ENV.setLoaded(" (pr-str output-name) ");\n"
"\n")))
(defn js-module-src-append [state {:keys [ns provides] :as src}]
;; export the shortest name always, some goog files have multiple provides
(let [export
(->> provides
(map str)
(sort)
(map comp/munge)
(first))]
(str "\nmodule.exports = " export ";\n")))
(defn js-module-env
[{:keys [polyfill-js] :as state} {:keys [runtime] :or {runtime :node} :as config}]
(str "var $CLJS = {};\n"
"var CLJS_GLOBAL = process.browser ? (typeof(window) != 'undefined' ? window : self) : global;\n"
;; closure accesses these defines via goog.global.CLOSURE_DEFINES
"var CLOSURE_DEFINES = $CLJS.CLOSURE_DEFINES = " (closure-defines-json state) ";\n"
"CLJS_GLOBAL.CLOSURE_NO_DEPS = true;\n"
;; so devtools can access it
"CLJS_GLOBAL.$CLJS = $CLJS;\n"
"var goog = $CLJS.goog = {};\n"
;; the global must be overriden in goog/base.js since it contains some
;; goog.define(...) which would otherwise be exported to "this"
;; but we need it on $CLJS
(-> (data/get-output! state {:resource-id goog-base-id})
(get :js)
(str/replace "goog.global = this;" "goog.global = $CLJS;"))
(if (seq polyfill-js)
(str "\n" polyfill-js
"\n$CLJS.$jscomp = $jscomp;")
(str "\n$CLJS.$jscomp = {};"))
;; set global back to actual global so things like setTimeout work
"\ngoog.global = CLJS_GLOBAL;"
(slurp (io/resource "shadow/boot/static.js"))
(slurp (io/resource "shadow/build/targets/npm_module_goog_overrides.js"))
"\nmodule.exports = $CLJS;\n"
))
(defn flush-dev-js-modules
[{::comp/keys [build-info] :as state} mode config]
(util/with-logged-time [state {:type :npm-flush
:output-path (.getAbsolutePath (get-in state [:build-options :output-dir]))}]
(let [env-file
(data/output-file state "cljs_env.js")
env-content
(js-module-env state config)
env-modified?
(or (not (.exists env-file))
(not= env-content (slurp env-file)))]
(when env-modified?
(io/make-parents env-file)
(spit env-file env-content))
(doseq [src-id (:build-sources state)
:when (not= src-id goog-base-id)
:let [src (get-in state [:sources src-id])]
:when (not (util/foreign? src))]
(let [{:keys [resource-name output-name last-modified]}
src
{:keys [js] :as output}
(data/get-output! state src)
target
(data/output-file state output-name)]
;; flush everything if env was modified, otherwise only flush modified
(when (or env-modified?
(contains? (:compiled build-info) resource-name)
(not (.exists target))
(>= last-modified (.lastModified target)))
(let [prepend
(js-module-src-prepend state src true)
content
(str prepend
js
(js-module-src-append state src)
(generate-source-map state src output target prepend))]
(spit target content)
))))))
state)