Skip to content

Commit 159fad2

Browse files
committed
CLJS-1196: Assert failed on 3190+ while :require-ing .js file in :libs directory
cljs.util - path can now take a string cljs.js-deps - IJavaScript add new -closure-lib? to protocol - library-graph-node mark closure libs, record :libs path where found if given - load-library* pass :libs path to library-graph-node cljs.closure - implement -closure-lib? on IJavaScript extended types - map->javascript-file merge :closure-lib info - rel-output-path Closure libraries need relative path treatment - lib-rel-path add helper to compute path relative to :libs dir where found to avoid name clashes - write-js? closure :libs also need to be written as JS
1 parent d9feda1 commit 159fad2

File tree

3 files changed

+59
-37
lines changed

3 files changed

+59
-37
lines changed

src/main/clojure/cljs/closure.clj

+45-30
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,15 @@
255255

256256
String
257257
(-foreign? [this] false)
258+
(-closure-lib? [this] false)
258259
(-url [this] nil)
259260
(-provides [this] (:provides (deps/parse-js-ns (string/split-lines this))))
260261
(-requires [this] (:requires (deps/parse-js-ns (string/split-lines this))))
261262
(-source [this] this)
262263

263264
clojure.lang.IPersistentMap
264265
(-foreign? [this] (:foreign this))
266+
(-closure-lib? [this] (:closure-lib this))
265267
(-url [this] (or (:url this)
266268
(deps/to-url (:file this))))
267269
(-provides [this] (map name (:provides this)))
@@ -273,6 +275,7 @@
273275
(defrecord JavaScriptFile [foreign ^URL url ^URL source-url provides requires lines source-map]
274276
deps/IJavaScript
275277
(-foreign? [this] foreign)
278+
(-closure-lib? [this] (:closure-lib this))
276279
(-url [this] url)
277280
(-provides [this] provides)
278281
(-requires [this] requires)
@@ -291,16 +294,19 @@
291294
(JavaScriptFile. foreign url source-url (map name provides) (map name requires) lines source-map)))
292295

293296
(defn map->javascript-file [m]
294-
(javascript-file
295-
(:foreign m)
296-
(when-let [f (:file m)]
297-
(deps/to-url f))
298-
(when-let [sf (:source-file m)]
299-
(deps/to-url sf))
300-
(:provides m)
301-
(:requires m)
302-
(:lines m)
303-
(:source-map m)))
297+
(merge
298+
(javascript-file
299+
(:foreign m)
300+
(when-let [f (:file m)]
301+
(deps/to-url f))
302+
(when-let [sf (:source-file m)]
303+
(deps/to-url sf))
304+
(:provides m)
305+
(:requires m)
306+
(:lines m)
307+
(:source-map m))
308+
(when (:closure-lib m)
309+
{:closure-lib true})))
304310

305311
(defn read-js
306312
"Read a JavaScript file returning a map of file information."
@@ -1121,37 +1127,45 @@
11211127
(- (count (.split #"\r?\n" fdeps-str -1)) 1)
11221128
0)})))))))
11231129

1130+
(defn lib-rel-path [{:keys [lib-path url] :as ijs}]
1131+
(string/replace
1132+
(util/path url)
1133+
(str (io/file (System/getProperty "user.dir") lib-path) File/separator)
1134+
""))
1135+
11241136
(defn ^String rel-output-path
11251137
"Given an IJavaScript which is either in memory, in a jar file,
11261138
or is a foreign lib, return the path relative to the output
11271139
directory."
1128-
[js]
1129-
(let [url (deps/-url js)]
1130-
(cond
1131-
url
1132-
(if (deps/-foreign? js)
1133-
(util/get-name url)
1134-
(path-from-jarfile url))
1135-
1136-
(string? js)
1137-
(let [digest (MessageDigest/getInstance "SHA-1")]
1138-
(.reset digest)
1139-
(.update digest (.getBytes ^String js "utf8"))
1140-
(str
1141-
(->> (DatatypeConverter/printHexBinary (.digest digest))
1142-
(take 7)
1143-
(apply str))
1144-
".js"))
1145-
1146-
:else (str (random-string 5) ".js"))))
1140+
([js] (rel-output-path js nil))
1141+
([js opts]
1142+
(let [url (deps/-url js)]
1143+
(cond
1144+
url
1145+
(cond
1146+
(deps/-closure-lib? js) (lib-rel-path js)
1147+
(deps/-foreign? js) (util/get-name url)
1148+
:else (path-from-jarfile url))
1149+
1150+
(string? js)
1151+
(let [digest (MessageDigest/getInstance "SHA-1")]
1152+
(.reset digest)
1153+
(.update digest (.getBytes ^String js "utf8"))
1154+
(str
1155+
(->> (DatatypeConverter/printHexBinary (.digest digest))
1156+
(take 7)
1157+
(apply str))
1158+
".js"))
1159+
1160+
:else (str (random-string 5) ".js")))))
11471161

11481162
(defn write-javascript
11491163
"Write or copy a JavaScript file to output directory. Only write if the file
11501164
does not already exist. Return IJavaScript for the file on disk at the new
11511165
location."
11521166
[opts js]
11531167
(let [out-dir (io/file (util/output-directory opts))
1154-
out-name (rel-output-path js)
1168+
out-name (rel-output-path js opts)
11551169
out-file (io/file out-dir out-name)
11561170
ijs {:url (deps/to-url out-file)
11571171
:requires (deps/-requires js)
@@ -1171,6 +1185,7 @@
11711185
(let [url ^URL (deps/-url js)]
11721186
(or (not url)
11731187
(= (.getProtocol url) "jar")
1188+
(deps/-closure-lib? js)
11741189
(deps/-foreign? js))))
11751190

11761191
(defn source-on-disk

src/main/clojure/cljs/js_deps.clj

+11-5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ case."
111111
(defprotocol IJavaScript
112112
(-foreign? [this] "Whether the Javascript represents a foreign
113113
library (a js file that not have any goog.provide statement")
114+
(-closure-lib? [this] "Whether the Javascript represents a Closure style
115+
library")
114116
(-url [this] "The URL where this JavaScript is located. Returns nil
115117
when JavaScript exists in memory only.")
116118
(-provides [this] "A list of namespaces that this JavaScript provides.")
@@ -205,18 +207,22 @@ case."
205207
(defn- library-graph-node
206208
"Returns a map of :provides, :requires, and :url given a URL to a goog-style
207209
JavaScript library containing provide/require 'declarations'."
208-
[url]
209-
(with-open [reader (io/reader url)]
210-
(-> reader line-seq parse-js-ns
211-
(assoc :url url))))
210+
([url] (library-graph-node url nil))
211+
([url lib-path]
212+
(with-open [reader (io/reader url)]
213+
(-> reader line-seq parse-js-ns
214+
(merge
215+
{:url url :closure-lib true}
216+
(when lib-path
217+
{:lib-path lib-path}))))))
212218

213219
(defn load-library*
214220
"Given a path to a JavaScript library, which is a directory
215221
containing Javascript files, return a list of maps
216222
containing :provides, :requires, :file and :url."
217223
[path]
218224
(->> (find-js-resources path)
219-
(map library-graph-node)
225+
(map #(library-graph-node % path))
220226
(filter #(seq (:provides %)))))
221227

222228
(def load-library (memoize load-library*))

src/main/clojure/cljs/util.cljc

+3-2
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@
106106
(.getName f))
107107

108108
(defn ^String path [x]
109-
{:pre [(or (file? x) (url? x))]}
109+
{:pre [(or (file? x) (url? x) (string? x))]}
110110
(cond
111111
(file? x) (.getAbsolutePath ^File x)
112-
(url? x) (.getPath ^URL x)))
112+
(url? x) (.getPath ^URL x)
113+
(string? x) x))
113114

114115
(defn ^String ext
115116
"Given a file, url or string return the file extension."

0 commit comments

Comments
 (0)