|
812 | 812 | (or (contains? global-exports (symbol module))
|
813 | 813 | (contains? global-exports (name module)))))
|
814 | 814 |
|
| 815 | +(defn goog-module-dep? |
| 816 | + [module] |
| 817 | + (let [[module _] (lib&sublib module) |
| 818 | + module-type (get-in @env/*compiler* [:js-dependency-index (str module) :module])] |
| 819 | + (= :goog module-type))) |
| 820 | + |
815 | 821 | (defn confirm-var-exists
|
816 | 822 | ([env prefix suffix]
|
817 | 823 | (let [warn (confirm-var-exist-warning env prefix suffix)]
|
|
1010 | 1016 | (str "node$module$" (munge (string/replace (str name) #"[.\/]" #?(:clj "\\$"
|
1011 | 1017 | :cljs "$$")))))
|
1012 | 1018 |
|
| 1019 | +(defn munge-goog-module-lib |
| 1020 | + ([name] |
| 1021 | + (str "goog$module$" (munge (string/replace (str name) #"[.\/]" #?(:clj "\\$" :cljs "$$"))))) |
| 1022 | + ([ns name] |
| 1023 | + (str (munge ns) "." (munge-goog-module-lib name)))) |
| 1024 | + |
1013 | 1025 | (defn munge-global-export [name]
|
1014 | 1026 | (str "global$module$" (munge (string/replace (str name) #"[.\/]" #?(:clj "\\$"
|
1015 | 1027 | :cljs "$$")))))
|
|
1031 | 1043 |
|
1032 | 1044 | (defn ns->module-type [ns]
|
1033 | 1045 | (cond
|
| 1046 | + (goog-module-dep? ns) :goog-module |
1034 | 1047 | (js-module-exists? ns) :js
|
1035 | 1048 | (node-module-dep? ns) :node
|
1036 | 1049 | (dep-has-global-exports? ns) :global))
|
|
1072 | 1085 | :op :js-var
|
1073 | 1086 | :foreign true}))
|
1074 | 1087 |
|
| 1088 | +(defmethod resolve* :goog-module |
| 1089 | + [env sym full-ns current-ns] |
| 1090 | + {:name (symbol (str current-ns) (str (munge-goog-module-lib full-ns) "." (name sym))) |
| 1091 | + :ns current-ns |
| 1092 | + :op :var}) |
| 1093 | + |
1075 | 1094 | (defmethod resolve* :global
|
1076 | 1095 | [env sym full-ns current-ns]
|
1077 | 1096 | (let [pre (extern-pre sym current-ns)]
|
|
1135 | 1154 | :op :js-var
|
1136 | 1155 | :ns current-ns})))
|
1137 | 1156 |
|
| 1157 | +(defn resolve-import |
| 1158 | + "goog.modules are deterministically assigned to a property of the namespace, |
| 1159 | + we cannot expect the reference will be globally available, so we resolve to |
| 1160 | + namespace local reference." |
| 1161 | + [env import] |
| 1162 | + (if (goog-module-dep? import) |
| 1163 | + (symbol (munge-goog-module-lib (-> env :ns :name) import)) |
| 1164 | + import)) |
| 1165 | + |
1138 | 1166 | ;; core.async calls `macroexpand-1` manually with an ill-formed
|
1139 | 1167 | ;; :locals map. Normally :locals maps symbols maps, but
|
1140 | 1168 | ;; core.async adds entries mapping symbols to symbols. We work
|
|
1207 | 1235 | ;; check if prefix is some existing def
|
1208 | 1236 | (if-let [resolved (resolve-var env prefix nil false)]
|
1209 | 1237 | (update resolved :name #(symbol (str % "." suffix)))
|
1210 |
| - (let [idx (.lastIndexOf s ".") |
| 1238 | + ;; glib imports (i.e. (:import [goog.module ModuleLoader]) |
| 1239 | + ;; are always just dotted symbols after the recursion |
| 1240 | + (let [s (str |
| 1241 | + (cond->> s |
| 1242 | + (goog-module-dep? sym) |
| 1243 | + (resolve-import env))) |
| 1244 | + idx (.lastIndexOf (str s) ".") |
1211 | 1245 | pre (subs s 0 idx)
|
1212 | 1246 | suf (subs s (inc idx))]
|
1213 | 1247 | {:op :var
|
|
0 commit comments