-
-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathbootstrap.clj
More file actions
287 lines (232 loc) · 9.2 KB
/
bootstrap.clj
File metadata and controls
287 lines (232 loc) · 9.2 KB
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
(ns shadow.build.targets.bootstrap
(:refer-clojure :exclude (compile flush resolve))
(:require [clojure.java.io :as io]
[clojure.set :as set]
[clojure.pprint :refer (pprint)]
[shadow.cljs.util :as util]
[shadow.build :as build]
[shadow.build.classpath :as cp]
[shadow.build.resolve :as resolve]
[shadow.build.compiler :as impl]
[shadow.build.data :as data]
[shadow.build.cache :as cache]
[shadow.build.modules :as modules]
[clojure.data.json :as json]
[shadow.build.api :as build-api]
[cljs.compiler :as comp]
[clojure.string :as str]
[shadow.build.npm :as npm]
[shadow.build.output :as output]))
(defn make-macro-resource [macro-ns]
(let [path
(util/ns->path macro-ns)
rc-url
(or (io/resource (str path ".clj"))
(io/resource (str path ".cljc")))
last-mod
(util/url-last-modified rc-url)
rc
(->> {:resource-id [::macro macro-ns]
:resource-name (str path "$macros.cljc")
:type :cljs
:url rc-url
:last-modified last-mod
:cache-key [last-mod]
:macros-ns true
:output-name (str macro-ns "$macros.js")
:source (slurp rc-url)}
;; extract requires, deps
(cp/inspect-cljs))]
rc
))
(defn resolve [state {:keys [entries macros exclude] :as config}]
(let [entries
(into '[cljs.core] entries)
exclude
(into #{} exclude)
[deps state]
(resolve/resolve-entries state entries)
;; resolving macros in a second pass
;; because they are circular in nature
;; cljs.core requires cljs.core$macros which requires on cljs.core
;; the files themselves do not depend on the macros when compiled normally
;; only the bootstrap compiler will require them
macros-from-deps
(->> (for [dep-id deps
:let [{:keys [macro-requires] :as src} (data/get-source-by-id state dep-id)]
macro-ns macro-requires
:when (not (contains? exclude macro-ns))]
macro-ns)
(distinct)
(into []))
#_#_macros-from-deps
'[demo.macro]
macros
(into macros-from-deps macros)
macro-resources
(into [] (map make-macro-resource) macros)
all-entries
(-> []
(into entries)
(into (map :ns) macro-resources))
[deps state]
(-> state
(util/reduce-> data/add-source macro-resources)
(resolve/resolve-entries all-entries))]
(assoc state :build-sources deps)
))
(defn make-index [output-data]
(->> output-data
(map #(dissoc %
:source-file :source
:js-file :js
:ana-file :ana-json))
(into [])))
(defn prepare-output [{:keys [build-sources dead-js-deps] :as state} mode {:keys [exclude] :as config}]
(let [hash-fn
(if (= :dev mode)
;; dev mode just use the name, avoids generating too many hash filenames
(fn [name text] name)
;; release mode names are <hash>.name
(fn [name text]
(str (-> (util/md5hex text)
;; no idea how likely conflicts are if only taking the first 4 bytes?
(subs 0 8)
(str/lower-case))
"." name)))
exclude
(into #{} exclude)]
(->> build-sources
(map (fn [src-id]
(let [{:keys [type resource-name resource-id output-name ns] :as src}
(data/get-source-by-id state src-id)
{:keys [js source source-map-json source-map] :as output}
(data/get-output! state src)
flat-name
(hash-fn (util/flat-filename resource-name) source)
source-name
(str "/src/" flat-name)
source-file
(data/output-file state "src" flat-name)
;; FIXME: I don't think this is required anymore, need to verify
js
(str js
;; always expose all JS names, we don't know which the user is going to use
(when (= :shadow-js type)
(str "\ngoog.provide(\"" ns "\");"
;; FIXME: not safe to always require everything due to cyclic dependencies in JS
"\ngoog.global. " ns "=" (npm/shadow-js-require src) "\n")))
js-name-hash
(hash-fn output-name js)
js-name
(str "/js/" js-name-hash)
js-file
(data/output-file state "js" js-name-hash)
[ana-file ana-name ana-json :as ana]
(when (= type :cljs)
(let [ana
(get-in state [:compiler-env :cljs.analyzer/namespaces ns])
ana-json
(cache/write-str ana)
ns-name
(hash-fn (comp/munge ns) ana-json)
ana-name
(str "/ana/" ns-name ".transit.json")
ana-file
(data/output-file state "ana" (str ns-name ".transit.json"))]
[ana-file ana-name ana-json]))
sm-append
(when (get-in state [:compiler-options :source-map])
(output/generate-source-map
state
(assoc src :output-name js-name-hash)
output
js-file
""))
js
(str js sm-append)
resolved-deps
(->> (data/deps->syms state src)
(remove dead-js-deps))]
(-> {:resource-id resource-id
:type type
:provides (:provides src)
:requires (into #{} resolved-deps)
:deps resolved-deps
:ns ns
:timestamp (or (:compiled-at output)
(:last-modified output))
:source-name source-name
:source source
:source-file source-file
:js js
:js-name js-name
:js-file js-file}
(cond->
(= type :cljs)
(assoc :macro-requires (into #{} (remove exclude) (:macro-requires src)))
ana
(assoc
:ana-file ana-file
:ana-name ana-name
:ana-json ana-json
)))
))))))
(defn flush [{:keys [build-sources bootstrap-options] :as state} mode {:keys [exclude] :as config}]
(let [index-file
(data/output-file state "index.transit.json")
index-dir
(.getParentFile index-file)
output-data
(prepare-output state mode config)
index
{:sources (make-index output-data)
:exclude (into #{} exclude)}]
(io/make-parents index-file)
(io/make-parents index-dir "src" "foo")
(io/make-parents index-dir "js" "foo")
(io/make-parents index-dir "ana" "foo")
(cache/write-file index-file index)
(doseq [{:keys [type
source-file source
js-file js
ana-file ana-json
timestamp] :as x}
output-data]
(when (or (not (.exists js-file))
(not (.exists source-file))
(not timestamp)
(zero? timestamp)
;; timestamp
(> timestamp (.lastModified js-file))
(> timestamp (.lastModified source-file))
(and (= type :cljs)
(or (not (.exists ana-file))
(> timestamp (.lastModified ana-file)))))
(spit source-file source)
(spit js-file js)
(when (= type :cljs)
(spit ana-file ana-json)
))))
state)
(defn configure [state mode {:keys [output-dir] :as config}]
(-> state
(assoc ::build/skip-optimize true)
(build-api/with-build-options
{:output-dir (io/file output-dir)})
;; FIXME: allow closure?
(assoc-in [:js-options :js-provider]
(get-in config [:js-options :js-provider] :shadow))))
(defn process
[{::build/keys [stage mode config] :as state}]
(case stage
:configure
(configure state mode config)
:resolve
(resolve state config)
:compile-finish
state
:flush
(flush state mode config)
state
))