Skip to content

Commit 703e101

Browse files
committed
(wip)
* revert cljs.loader change for now * generalize munge-goog-module-lib * add resolve-import helper * fixup emission test
1 parent 4d14d42 commit 703e101

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/main/cljs/cljs/loader.cljs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
(ns cljs.loader
1010
(:require [goog.object :as gobj]
11-
[goog.html.legacyconversions :as legacy]
12-
[goog.module.ModuleLoader :as ModuleLoader])
13-
(:import [goog.module ModuleManager]))
11+
[goog.html.legacyconversions :as legacy])
12+
(:import [goog.module ModuleManager]
13+
[goog.module ModuleLoader]))
1414

1515
(def module-infos MODULE_INFOS) ;; set by compiler
1616
(def module-uris

src/main/clojure/cljs/analyzer.cljc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,8 +1016,11 @@
10161016
(str "node$module$" (munge (string/replace (str name) #"[.\/]" #?(:clj "\\$"
10171017
:cljs "$$")))))
10181018

1019-
(defn munge-goog-module-lib [name]
1020-
(str "goog$module$" (munge (string/replace (str name) #"[.\/]" #?(:clj "\\$" :cljs "$$")))))
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))))
10211024

10221025
(defn munge-global-export [name]
10231026
(str "global$module$" (munge (string/replace (str name) #"[.\/]" #?(:clj "\\$"
@@ -1151,6 +1154,15 @@
11511154
:op :js-var
11521155
:ns current-ns})))
11531156

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+
11541166
;; core.async calls `macroexpand-1` manually with an ill-formed
11551167
;; :locals map. Normally :locals maps symbols maps, but
11561168
;; core.async adds entries mapping symbols to symbols. We work

src/test/clojure/cljs/compiler/glib_module_test.clj

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66

77
(deftest test-glib-module-compile
88
(testing "glib modules compiled to Closure Compile expectations"
9-
(env/with-compiler-env (env/default-compiler-env)
10-
(comp-tests/compile-form-seq
11-
'[(ns test.foo
12-
(:require [goog.module.ModuleLoader :as module-loader]))
13-
(def EVENTS module-loader/EVENTS)]))))
9+
(let [src (env/with-compiler-env (env/default-compiler-env)
10+
(comp-tests/compile-form-seq
11+
'[(ns test.foo
12+
(:import [goog.module ModuleLoader]))
13+
(def module-loader (ModuleLoader.))]))]
14+
(is (re-find #"goog\.require\('goog\.module\.ModuleLoader'\)" src))
15+
(is (re-find #"test\.foo\.goog\$module\$goog\$module\$ModuleLoader = goog\.module\.get\('goog.module.ModuleLoader'\)" src)))))
1416

1517
(comment
1618

19+
(test/run-tests)
20+
1721
(println
1822
(env/with-compiler-env (env/default-compiler-env)
1923
(comp-tests/compile-form-seq
2024
'[(ns test.foo
21-
(:require [goog.module.ModuleLoader :as module-loader]))
22-
(def EVENTS module-loader/EVENTS)])))
25+
(:import [goog.module ModuleLoader]))
26+
(def module-loader (ModuleLoader.))])))
2327

2428
)

0 commit comments

Comments
 (0)