/
resolve.clj
767 lines (647 loc) · 30.1 KB
/
resolve.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
(ns shadow.build.resolve
"utility functions for resolving dependencies from entries"
(:require
[clojure.string :as str]
[shadow.cljs.util :as util]
[shadow.build.classpath :as cp]
[shadow.build.npm :as npm]
[shadow.build.data :as data]
[shadow.build.js-support :as js-support]
[shadow.build.cljs-bridge :as cljs-bridge]
[shadow.build.babel :as babel])
(:import [com.google.javascript.jscomp ShadowESModuleRewriter]
(java.io File)))
(defmulti resolve-deps*
(fn [state {:keys [type]}]
type))
(defn resolved? [{:keys [resolved-set] :as state} resource-id]
(contains? resolved-set resource-id))
(defn stack-push [{:keys [resolved-stack] :as state} resource-id]
(update state :resolved-stack conj {:resource-id resource-id :deps #{}}))
(defn stack-pop [{:keys [resolved-stack] :as state}]
(let [{:keys [resource-id deps] :as last} (peek resolved-stack)]
(-> state
;; collecting this here since we need it later for cache invalidation
(update :immediate-deps assoc resource-id deps)
(update :resolved-stack pop))))
(declare resolve-require)
(defn resolve-deps
[{:keys [resolved-stack] :as state} {:keys [resource-id deps] :as rc}]
{:pre [(data/build-state? state)]}
(let [head-idx
(when (seq resolved-stack)
(-> resolved-stack count dec))]
(-> state
(cond->
head-idx
(update-in [:resolved-stack head-idx :deps] conj resource-id)
;; dont resolve twice
(not (resolved? state resource-id))
(-> (update :resolved-set conj resource-id)
(stack-push resource-id)
(util/reduce->
(fn [state dep]
(resolve-require state rc dep))
deps)
(stack-pop)
(update :resolved-order conj resource-id))))))
(defmulti find-resource-for-string*
(fn [state require-from require was-symbol?]
(get-in state [:js-options :js-provider]))
:default ::default)
(defn classpath-resource? [{:keys [type classpath] :as rc}]
(or classpath
;; only :js and :shadow-js are allowed to go off classpath
(not (contains? #{:js :shadow-js} type))))
(defmethod find-resource-for-string* ::default [_ _ _ _]
(throw (ex-info "invalid [:js-options :js-provider] config" {})))
(defn find-resource-for-string [state require-from require was-symbol?]
;; only allow (:require ["foo$bar.baz" :as x]) requires from CLJS and the REPL
;; but requires with $ in their name from JS should look up actual files
(if (and (str/includes? require "$")
(or (nil? require-from)
(= :cljs (:type require-from))))
;; syntax sugar
;; (:require ["some$nested.access" :as foo])
;; results in 2 separate resources, one doing the require "some" the other doing "nested.access"
(js-support/shim-require-sugar-resource require-from require was-symbol?)
;; regular require
(find-resource-for-string* state require-from require was-symbol?)))
(def global-resolve-config
{"jquery"
{:export-globals ["$", "jQuery"]}})
(def native-node-modules
#{"assert" "buffer_ieee754" "buffer" "child_process" "cluster" "console"
"constants" "crypto" "_debugger" "dgram" "dns" "domain" "events" "freelist"
"fs" "http" "http2" "https" "_linklist" "module" "net" "os" "path" "punycode"
"querystring" "readline" "repl" "stream" "string_decoder" "sys" "timers"
"tls" "tty" "url" "util" "vm" "zlib" "_http_server" "process" "v8" "worker_threads"})
(defn find-npm-resource
[npm require-from ^String require]
{:pre [(npm/service? npm)
(or (nil? require-from)
(map? require-from))
(string? require)]}
;; per build :resolve config that may override where certain requires go
;; FIXME: should this only allow overriding package requires?
;; relative would need to be relative to the project, otherwise a generic
;; "./something.js" would override anything from any package
;; just assume ppl will only override packages for now
(let [resolve-cfg
(let [x (get-in npm [:js-options :resolve require])]
;; can't use `or` since `false` is a legal return
(if-not (nil? x)
x
(get global-resolve-config require)
))]
(if (false? resolve-cfg)
npm/empty-rc
(let [{:keys [target]}
resolve-cfg
rc
(case target
;; no resolve config, or resolve config without :target
nil
(npm/find-resource npm require-from require)
;; {"react" {:target :global :global "React"}}
:global
(npm/js-resource-for-global require resolve-cfg)
;; {"react" {:target :file :file "some/path.js"}}
:file
(npm/js-resource-for-file npm require resolve-cfg)
;; {"react" {:target :npm :require "preact"}}
:npm
(let [other
(if (and (= :release (get-in npm [:js-options :mode])) (contains? resolve-cfg :require-min))
(:require-min resolve-cfg)
(:require resolve-cfg))]
;; FIXME: maybe allow to add some additional stuff?
(when (= require other)
(throw (ex-info "can't resolve to self" {:require require :other other})))
(or (find-npm-resource npm require-from other)
(throw (ex-info (format ":resolve override for \"%s\" to \"%s\" which does not exist" require other)
{:tag ::invalid-override
:require-from require-from
:require require
:other other}))))
(throw (ex-info "unknown resolve target"
{:require require
:config resolve-cfg})))]
(when rc ;; don't assoc into nil (aka resource not found)
(cond-> rc
resolve-cfg
(-> (assoc :resource-config (assoc resolve-cfg :original-require require))
;; make sure that any change to the resolve config invalidated the cache
(update :cache-key conj resolve-cfg))))))))
;; FIXME: this is now a near duplicate of :shadow, should refactor and remove dupes
(defmethod find-resource-for-string* :closure
[{:keys [js-options babel classpath] :as state} {:keys [file] :as require-from} require was-symbol?]
(let [abs? (util/is-absolute? require)
rel? (util/is-relative? require)
cp-rc? (when require-from
(classpath-resource? require-from))]
(cond
;; entry requires must always be absolute
(and (nil? require-from) abs?)
(cp/find-js-resource classpath require)
;; requires from non classpath resources go directly to npm resolve
;; as do ambiguous requires, eg "react" not "./foo"
(or (not cp-rc?)
(and (not abs?)
(not rel?)))
(find-npm-resource
(:npm state)
(when (and (= :js (:type :require-from))
(not (:classpath require-from)))
require-from)
require)
(util/is-absolute? require)
(cp/find-js-resource classpath require)
(util/is-relative? require)
(cp/find-js-resource classpath require-from require)
:else
(throw (ex-info "unsupported require" {:require require}))
)))
(defn make-babel-source-fn [{:keys [source file] :as rc}]
(-> rc
(dissoc :source)
(assoc :source-fn
(fn [{:keys [babel] :as state}]
(if (get-in state [:js-options :use-babel])
(babel/convert-source babel state source (.getAbsolutePath file))
(ShadowESModuleRewriter/rewrite source))
))))
(defn maybe-esm-rewrite [{:keys [js-esm deps] :as rc}]
{:pre [(map? rc)]}
(if (and (:classpath rc) js-esm)
;; es6 from the classpath is left as :js type so its gets processed by closure
rc
;; es6 from node_modules and any commonjs is converted to :shadow-js
;; maybe rewritten by babel since closure doesn't support the __esModule convention
;; that babel created and it is still to widely used to not support
(let [babel-rewrite?
js-esm
deps
(-> '[shadow.js]
;; skip using global helpers for now
#_(cond-> babel-rewrite? (conj 'shadow.js.babel))
(into deps))]
(-> rc
(assoc :deps deps)
(assoc :type :shadow-js)
(cond->
babel-rewrite?
(make-babel-source-fn)
)))))
(defmethod find-resource-for-string* :shadow
[{:keys [js-options classpath] :as state} {:keys [file] :as require-from} require was-symbol?]
(cond
(or (and (:keep-native-requires js-options)
(or (contains? native-node-modules require)
(str/starts-with? require "node:")))
(contains? (:keep-as-require js-options) require))
(js-support/shim-require-resource state require)
(or (str/starts-with? require "esm:")
(str/starts-with? require "https://")
(str/starts-with? require "http://")
(contains? (:keep-as-import js-options) require))
(js-support/shim-import-resource state require)
:else
(let [abs? (util/is-absolute? require)
rel? (util/is-relative? require)
cp-rc? (when require-from
(classpath-resource? require-from))]
(when-let [rc
(cond
;; entry requires must always be absolute
(and (nil? require-from) abs?)
(cp/find-js-resource classpath require)
;; requires from non classpath resources go directly to npm resolve
;; as do ambiguous requires, eg "react" not "./foo"
(or (not cp-rc?)
(and (not abs?)
(not rel?)))
(find-npm-resource
(:npm state)
;; only consider require-from for shadow-js files
;; otherwise it may end up looking at directories it should not look at
;; relative to classpath files
(when (= :shadow-js (:type require-from))
require-from)
require)
(util/is-absolute? require)
(cp/find-js-resource classpath require)
(util/is-relative? require)
(cp/find-js-resource classpath require-from require)
:else
(throw (ex-info "unsupported require" {:require require})))]
(maybe-esm-rewrite rc)
))))
(defmethod find-resource-for-string* :require
[{:keys [npm js-options classpath mode] :as state} require-from require was-symbol?]
(let [cp-rc? (when require-from
(classpath-resource? require-from))]
(cond
(util/is-absolute? require)
(if-not cp-rc?
(throw (ex-info "absolute require not allowed for non-classpath resources" {:require require}))
(some->
(cp/find-js-resource classpath require)
(maybe-esm-rewrite)))
(util/is-relative? require)
(some->
(cp/find-js-resource classpath require-from require)
(maybe-esm-rewrite))
:else
(when (or (contains? native-node-modules require)
;; if the require was a string we just pass it through
;; we don't need to check if it exists since the runtime
;; will take care of that. we do not actually need anything from the package
(not was-symbol?)
;; treat every symbol as valid when :npm-deps in deps.cljs on the classpath contain it
(npm/is-npm-dep? (:npm state) require)
;; if `(:require [react ...])` was used we need to check if the package
;; is actually installed. otherwise we will blindy return a shim for
;; every symbol, no matter it was a typo or actually intended JS package.
(npm/find-package (:npm state) require))
;; technically this should be done before the find-package above
;; but I'm fine with this breaking for symbol requires
(let [resolve-cfg (get-in js-options [:resolve require])]
(cond
(nil? resolve-cfg)
(js-support/shim-require-resource state require)
(false? resolve-cfg)
(assoc npm/empty-rc :deps []) ;; FIXME: why the shadow.js dep in npm?
(= :npm (:target resolve-cfg))
(let [{:keys [require require-min]} resolve-cfg
require
(or (and (= :release mode) require-min)
require)]
(find-resource-for-string state require-from require false))
(= :file (:target resolve-cfg))
(npm/js-resource-for-file npm require resolve-cfg)
(= :global (:target resolve-cfg))
(npm/js-resource-for-global require resolve-cfg)
(= :resource (:target resolve-cfg))
(cp/find-js-resource classpath (:resource resolve-cfg))
:else
(throw (ex-info "override not supported for :js-provider :require" resolve-cfg))
))))))
(defmethod find-resource-for-string* :import
[{:keys [npm js-options classpath mode] :as state} require-from require was-symbol?]
(let [cp-rc? (when require-from
(classpath-resource? require-from))]
(cond
(util/is-absolute? require)
(if-not cp-rc?
(throw (ex-info "absolute require not allowed for non-classpath resources" {:require require}))
(some->
(cp/find-js-resource classpath require)
(maybe-esm-rewrite)))
(util/is-relative? require)
(some->
(cp/find-js-resource classpath require-from require)
(maybe-esm-rewrite))
:else
(when (or (contains? native-node-modules require)
;; if the require was a string we just pass it through
;; we don't need to check if it exists since the runtime
;; will take care of that. we do not actually need anything from the package
(not was-symbol?)
;; treat every symbol as valid when :npm-deps in deps.cljs on the classpath contain it
(npm/is-npm-dep? (:npm state) require)
;; `(:require [react ...])` check if <js-package-dir>/react exists
(npm/find-package (:npm state) require))
;; technically this should be done before the find-package above
;; but I'm fine with this breaking for symbol requires
(let [resolve-cfg (get-in js-options [:resolve require])]
(cond
(nil? resolve-cfg)
(js-support/shim-import-resource state require)
(false? resolve-cfg)
(assoc npm/empty-rc :deps []) ;; FIXME: why the shadow.js dep in npm?
(= :npm (:target resolve-cfg))
(let [{:keys [require require-min]} resolve-cfg
require
(or (and (= :release mode) require-min)
require)]
(find-resource-for-string state require-from require false))
(= :file (:target resolve-cfg))
(npm/js-resource-for-file npm require resolve-cfg)
(= :global (:target resolve-cfg))
(npm/js-resource-for-global require resolve-cfg)
(= :resource (:target resolve-cfg))
(cp/find-js-resource classpath (:resource resolve-cfg))
:else
(throw (ex-info "override not supported for :js-provider :import" resolve-cfg))
))))))
(defmethod find-resource-for-string* :external
[{:keys [npm js-options classpath mode] :as state} require-from require was-symbol?]
(let [cp-rc? (when require-from
(classpath-resource? require-from))]
(cond
(util/is-absolute? require)
(if-not cp-rc?
(throw (ex-info "absolute require not allowed for non-classpath resources" {:require require}))
(some->
(cp/find-js-resource classpath require)
(maybe-esm-rewrite)))
(util/is-relative? require)
(some->
(cp/find-js-resource classpath require-from require)
(maybe-esm-rewrite))
:else
(when (or (contains? native-node-modules require)
;; if the require was a string we just pass it through
;; we don't need to check if it exists since the runtime
;; will take care of that. we do not actually need anything from the package
(not was-symbol?)
;; if `(:require [react ...])` was used we need to check if the package
;; is actually installed. otherwise we will blindy return a shim for
;; every symbol, no matter it was a typo or actually intended JS package.
(npm/find-package (:npm state) require)
;; treat every symbol as valid when :npm-deps in deps.cljs on the classpath contain it
(npm/is-npm-dep? (:npm state) require))
(js-support/shim-require-resource state require)))))
(defn resolve-string-require
[state {require-from-ns :ns :as require-from} require]
{:pre [(data/build-state? state)
(string? require)]}
;; in a watch recompile cycle strings may already be resolved so we can re-use those
;; resolve is a bit costly since it has to do a bunch of FS interop
;; FIXME: properly cleanup :str->sym, should not be possible to get some ns here but nil for rc
(let [rc
(when-let [resolved-sym (get-in state [:str->sym require-from-ns require])]
(when-let [resource-id (get-in state [:sym->id resolved-sym])]
(get-in state [:sources resource-id])))]
(if rc
(resolve-deps state rc)
;; fresh resolve
(let [{:keys [resource-id ns] :as rc}
(find-resource-for-string state require-from require false)]
(when-not rc
(throw (ex-info
(if require-from
(format "The required JS dependency \"%s\" is not available, it was required by \"%s\"." require (:resource-name require-from))
(format "The required JS dependency \"%s\" is not available." require))
{:tag ::missing-js
:js-package-dirs (get-in state [:npm :js-package-dirs])
:require require
:require-from (:resource-name require-from)
:resolved-stack
(->> (:resolved-stack state)
(map :resource-id)
(map #(get-in state [:sources % :resource-name]))
(into []))})))
(-> state
(data/maybe-add-source rc)
(cond->
require-from-ns
(data/add-string-lookup require-from-ns require ns)
(nil? require-from)
(-> (update :js-entries conj ns)
(update :resolved-entries conj resource-id)))
(resolve-deps rc)
)))))
(defn ensure-non-circular!
[{:keys [resolved-stack] :as state} resource-id]
(let [resolved-ids (into [] (map :resource-id) resolved-stack)]
(when (some #(= resource-id %) resolved-ids)
(let [path (->> (conj resolved-ids resource-id)
(drop-while #(not= resource-id %))
(map (fn [rc-id]
(let [{:keys [ns resource-name]} (data/get-source-by-id state rc-id)]
(or ns resource-name))))
(into []))]
(throw (ex-info (format "Circular dependency detected: %s" (str/join " -> " path))
{:tag ::circular-dependency
:resource-id resource-id
:path path
:stack resolved-ids}))))))
(declare find-resource-for-symbol)
;; check if the provided namespace matches the filename
;; catches errors where demo.with_underscore is defined in demo/with_underscore.cljs
;; when it should be demo.with-underscore
(defn check-correct-ns! [{:keys [virtual macros-ns ns type resource-name]}]
(when (and (= type :cljs) (not macros-ns) (not virtual))
(let [expected-ns (util/filename->ns resource-name)]
(when (not (= expected-ns ns))
(throw (ex-info "Resource does not have expected namespace"
{:tag ::unexpected-ns
:resource resource-name
:expected-ns expected-ns
:actual-ns ns}))))))
;; (:require [some.with_dash]) resolves correct but par-compile tracks the actual some.with-dash ns
;; so ensure that all requires use actual ns name to avoid par-compile getting stuck
;; FIXME: could just rewrite requires to the proper name instead? or add alias?
;; for sake of keeping this clean I think its best to just abort and let user correct it
(defn check-correct-require! [{:keys [type ns]} require-from require]
(when (and (= type :cljs) (not= ns require))
(let [require-from-ns
(when require-from
(:ns require-from))]
(throw
(ex-info
(if require-from-ns
(format "Namespace %s required %s, but the target is %s" require-from-ns require ns)
(format "Require of %s should be %s" require ns))
{:tag ::require-mismatch
:require require
:require-from require-from-ns
:ns ns})))))
(defn find-resource-for-symbol*
[{:keys [classpath] :as state} require-from require]
;; check if ns is an alias
(or (when-let [alias (get-in state [:ns-aliases require])]
;; if alias was previously used we already have all the data
(if-let [rc-id (get-in state [:sym->id alias])]
[(get-in state [:sources rc-id]) state]
(find-resource-for-symbol state require-from alias)))
;; otherwise check if the classpath provides a symbol
(when-let [rc (cp/find-resource-for-provide classpath require)]
(check-correct-ns! rc)
(check-correct-require! rc require-from require)
[rc state])
;; special cases where clojure.core.async gets aliased to cljs.core.async
(when (str/starts-with? (str require) "clojure.")
(let [cljs-sym
(-> (str require)
(str/replace #"^clojure\." "cljs.")
(symbol))]
;; auto alias clojure.core.async -> cljs.core.async if it exists
(when-let [rc (cp/find-resource-for-provide classpath cljs-sym)]
(check-correct-ns! rc)
(check-correct-require! rc require-from cljs-sym)
[rc
(-> state
(update :ns-aliases assoc require cljs-sym)
;; must remember that we used an alias in both cases
;; compiling cljs.core.async will not provide clojure.core.async since it is unaware
;; that it was referenced via an alias, only really required by par-compile since that
;; needs to know which namespaces where provided before compiling other namespaces
(update :ns-aliases-reverse assoc cljs-sym require))])))
;; special case for symbols that should be strings
;; (:require [react]) should be (:require ["react"]) as it is a magical symbol
;; that becomes available if node_modules/react exists but not otherwise
(when-let [{:keys [ns] :as rc}
(find-resource-for-string state require-from (str require) true)]
[rc
(-> state
(update :magic-syms conj require)
(update :ns-aliases assoc require ns))])
;; the ns was not found, handled elsewhere
[nil state]
))
(defn find-resource-for-symbol
[state require-from require]
;; check if already registered by add-source
(let [rc-id (get-in state [:sym->id require])]
(if-not rc-id
;; unknown resource, not defined in REPL
(find-resource-for-symbol* state require-from require)
;; if the resource in the current compiler state was defined in the REPL
;; without a file attached but that file was created in the meantime
;; the file version should be picked over the virtual REPL resource
(let [{:keys [defined-in-repl] :as rc} (get-in state [:sources rc-id])]
(if-not defined-in-repl
[rc state]
(let [[actual state] (find-resource-for-symbol* state require-from require)]
(if actual
[actual
(-> state
;; FIXME: ugly smell. remove-source-by-id doesn't remove virtual sources
;; since they usually can't change and are generated by the compiler
;; REPL lets you get into a state where they can change though (file created after ns eval)
;; should make a proper remove-source-by-id with force option or so
(update-in [:sources rc-id] dissoc :virtual)
(data/remove-source-by-id rc-id))]
[rc state]
)))))))
(defn reinspect-cljc-rc
[state {:keys [url resource-name macros-ns] :as rc} reader-features]
(let [{:keys [name deps requires] :as ast}
(cljs-bridge/get-resource-info
resource-name
(or (:source rc) ;; nREPL load-file supplies source
(slurp url))
reader-features)
provide-name
(if-not macros-ns
name
(symbol (str name "$macros")))]
(-> rc
(assoc
:ns-info (dissoc ast :env)
:ns provide-name
:provides #{provide-name}
:requires (into #{} (vals requires))
:macro-requires
(-> #{}
(into (-> ast :require-macros vals))
(into (-> ast :use-macros vals)))
:deps deps)
(cond->
macros-ns
(assoc :macros-ns true)))))
(defn resolve-symbol-require [{:keys [classpath] :as state} require-from require]
{:pre [(data/build-state? state)]}
(let [[{:keys [resource-id ns resource-name type] :as rc} state]
(find-resource-for-symbol state require-from require)]
(when-not rc
(throw
(ex-info
(if require-from
(format "The required namespace \"%s\" is not available, it was required by \"%s\"." require (:resource-name require-from))
(format "The required namespace \"%s\" is not available." require))
{:tag ::missing-ns
:stack (:resolved-stack state)
:require require
:require-from (:resource-name require-from)})))
(let [reader-features
(data/get-reader-features state)
;; when using custom :reader-features we need to reinspect the resource
;; just in case its requires are in some conditional we didn't cover before
;; since the classpath only reads with :cljs
rc
(if (or (not= type :cljs)
(not (str/ends-with? resource-name ".cljc"))
(= reader-features #{:cljs}))
rc
(reinspect-cljc-rc state rc reader-features))]
;; react symbol may have resolved to a JS dependency
;; CLJS does not allow circular dependencies, JS does
(when (= :cljs type)
(ensure-non-circular! state resource-id))
(-> state
;; in case of clojure->cljs aliases the rc may already be present
;; if clojure.x and cljs.x are both used in sources, the resource is
;; resolved twice but added once
(data/maybe-add-source rc)
(resolve-deps rc)
(cond->
(nil? require-from)
(update :resolved-entries conj resource-id))
))))
(defn resolve-require [state require-from require]
{:pre [(data/build-state? state)]}
(cond
(symbol? require)
(resolve-symbol-require state require-from require)
(string? require)
(resolve-string-require state require-from require)
:else
(throw (ex-info "invalid require, only symbols or strings are supported" {:require require}))
))
(defn resolve-entry [state entry]
(resolve-require state nil entry))
(defn resolve-cleanup [{:keys [resolved-order] :as state}]
(dissoc state :resolved-order :resolved-set :resolved-stack))
(defn resolve-entries
"returns [resolved-ids updated-state] where each resolved-id can be found in :sources of the updated state"
[{:keys [classpath] :as state} entries]
(let [{:keys [resolved-order] :as state}
(-> state
(assoc
:resolved-set #{}
:resolved-order []
:resolved-stack [])
(util/reduce-> resolve-entry entries))
auto-require-suffixes
(get-in state [:build-options :auto-require-suffixes])
auto-require-namespaces
(when (seq auto-require-suffixes)
(->> resolved-order
(map #(get-in state [:sources %]))
(filter #(= :cljs (:type %)))
;; FIXME: maybe let use configure to include files from jar?
(remove :from-jar)
(mapcat (fn [{:keys [ns]}]
(->> auto-require-suffixes
(map #(symbol (str ns %))))))
(filter (fn [spec-ns]
(or (get-in state [:sym->id spec-ns])
(cp/has-resource? classpath spec-ns))))
(vec)))]
(if-not (seq auto-require-namespaces)
[resolved-order (resolve-cleanup state)]
(let [[extra-sources state]
(resolve-entries state auto-require-namespaces)]
[(into resolved-order extra-sources)
(resolve-cleanup state)]))))
(defn resolve-repl
"special case for REPL which always resolves based on the current ns"
[state repl-ns deps]
{:pre [(symbol? repl-ns)]}
(let [{:keys [resource-id] :as repl-rc}
(data/get-source-by-provide state repl-ns)
{:keys [resolved-order] :as state}
(-> state
(assoc
:resolved-set #{}
:resolved-order []
:resolved-stack [])
(resolve-deps (assoc repl-rc :deps deps)))]
;; FIXME: resolve-deps will include the resource itself, might need a rework
;; for now just remove it since we just want to know the new deps
[(into [] (remove #{resource-id}) resolved-order)
(dissoc state :resolved-order :resolved-set :resolved-stack)]))