/
js_support.clj
164 lines (145 loc) · 5.65 KB
/
js_support.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
(ns shadow.build.js-support
(:require [shadow.build.resource :as rc]
[shadow.cljs.util :as util]
[shadow.build.data :as data]
[shadow.build.closure :as closure]
[clojure.string :as str]
[shadow.build.npm :as npm]
[shadow.build.classpath :as cp])
(:import (com.google.javascript.jscomp.deps ModuleNames)))
(defn shim-require-resource
([state js-require]
(shim-require-resource state js-require js-require))
([state js-name js-require]
(let [js-ns-alias
(-> (ModuleNames/fileToModuleName js-name)
(->> (str "shadow.js.shim."))
(symbol))
register-shadow-js
(= :shadow (get-in state [:js-options :js-provider]))
;; since we can't stop closure from rewriting require("react") even when it shouldn't
;; we create a second alias var that it replaces the require with a var we actually created
commonjs-ns
(symbol (ModuleNames/fileToModuleName (str js-ns-alias)))
name
(str js-ns-alias ".js")
require-fn
(or (and (= :external (get-in state [:js-options :js-provider])) "shadow$bridge")
(get-in state [:js-options :require-fn] "require"))]
{:resource-id [::require js-name]
:resource-name name
:output-name (util/flat-js-name name)
:type :goog
:cache-key [js-ns-alias name require-fn]
:last-modified 0
::require-shim true
:js-require js-require
:ns js-ns-alias
:provides #{js-ns-alias commonjs-ns}
:requires #{}
:deps (-> '[goog]
(cond->
register-shadow-js
(conj 'shadow.js)))
;; for :npm-module support since we don't have a property to export
;; but need to export the entire "ns" which is just the result of require
:export-self true
;; we emit this as a goog.provide so it the same code can be used for :advanced
;; as it won't touch the require call unless specifically told to
:source (str "goog.provide(\"" js-ns-alias "\");\n"
"goog.provide(\"" commonjs-ns "\");\n"
js-ns-alias " = " require-fn "(\"" js-require "\");\n"
(when register-shadow-js
(str "shadow.js.add_native_require(\"" js-ns-alias "\", " js-ns-alias ");\n"))
;; FIXME: this default business is annoying
commonjs-ns ".default = " js-ns-alias ";\n"
)}
)))
;; (:require ["some$nested.access" :as sugar])
(defn shim-require-sugar-resource
[require-from js-require was-symbol?]
(let [[prefix suffix]
(str/split js-require #"\$" 2)
;; need to resolve (:require ["./foo.js$bar"]) since it may have different meanings
;; in other namespaces but use the same name
prefix
(if (util/is-relative? prefix)
(str "/" (cp/resolve-rel-path (:resource-name require-from) prefix))
prefix)
js-ns-alias
(-> (str "shadow.js.shim."
(ModuleNames/fileToModuleName (str prefix "$" suffix)))
(symbol))
resource-name
(str js-ns-alias ".js")
dep
(if was-symbol?
(symbol prefix)
prefix)]
{:resource-id [::require js-ns-alias]
:resource-name resource-name
:output-name (util/flat-js-name resource-name)
::split-require true
:type :goog
:cache-key [js-ns-alias resource-name]
:last-modified 0
:ns js-ns-alias
:provides #{js-ns-alias}
:requires #{}
:deps ['goog dep]
:export-self true
:source-fn
(fn [state]
(let [{:keys [ns type] :as rc}
(if was-symbol?
(data/get-source-by-provide state dep)
(let [alias (data/get-string-alias state js-ns-alias prefix)]
(data/get-source-by-provide state alias)))]
(str "goog.provide(\"" js-ns-alias "\");\n"
(case type
:shadow-js
(str js-ns-alias " = " (npm/shadow-js-require rc false)
;; (:require ["some$foo.bar"]) emitting require("some")["foo"]["bar"]
;; FIXME: should this generate externs instead?
(->> (str/split suffix #"\.")
(map #(str "[\"" % "\"]"))
(str/join ""))
";\n")
;; plain suffix for sources going through :advanced
(str js-ns-alias " = " (or ns dep) "." suffix ";\n"))
)))}))
(defn shim-import-resource
[{:shadow.build/keys [mode] :as state} js-name]
(let [js-ns-alias
(-> (ModuleNames/fileToModuleName js-name)
;; https://cdn.pika.dev/preact@^10.0.0
;; ^ not replaced by the above fn
;; not that anyone should ever use version ranges in an import ...
(str/replace "^" "_CARET_")
(str/replace "module$" "import$")
(symbol))
shim-ns-alias
js-ns-alias
#_ (symbol (str "shadow.js.shim." js-ns-alias))
name
(str js-ns-alias ".js")
import
(if (str/starts-with? js-name "esm:")
(subs js-name 4)
js-name)]
{:resource-id [::require js-name]
:resource-name name
:output-name (util/flat-js-name name)
:type :goog
:cache-key [js-ns-alias name]
:last-modified 0
::import-shim true
:js-import import
:js-alias js-ns-alias
:ns shim-ns-alias
:provides #{shim-ns-alias}
:requires #{}
:deps []
:source ""
#_(str "goog.provide(\"" shim-ns-alias "\");\n"
shim-ns-alias " = " js-ns-alias ";\n")}))