|
138 | 138 | (defn register-module [mod internal-name]
|
139 | 139 | (swap! loaded-modules assoc internal-name mod))
|
140 | 140 |
|
141 |
| -(defn load-js-module [libname internal-name] |
| 141 | +(defn debug [& xs] |
| 142 | + (binding [*print-fn* *print-err-fn*] |
| 143 | + (apply prn xs))) |
| 144 | + |
| 145 | +(defn load-js-module [libname internal-name reload?] |
142 | 146 | (-> (if-let [resolve (:resolve @ctx)]
|
143 | 147 | (-> (resolve libname)
|
144 | 148 | (.catch
|
145 | 149 | (fn [_]
|
146 | 150 | ((.-resolve (:require @ctx)) libname))))
|
147 | 151 | (js/Promise.resolve ((.-resolve (:require @ctx)) libname)))
|
148 | 152 | (.then (fn [path]
|
149 |
| - (esm/dynamic-import |
150 |
| - (let [path (if (and windows? (fs/existsSync path)) |
151 |
| - (str (url/pathToFileURL path)) |
152 |
| - path)] |
153 |
| - path)))) |
| 153 | + (let [file-url (if (str/starts-with? (str path) "file:") |
| 154 | + path |
| 155 | + (when (and (or windows? reload?) (fs/existsSync path)) |
| 156 | + (str (url/pathToFileURL path)))) |
| 157 | + path (if (and reload? |
| 158 | + ;; not "node:fs" etc |
| 159 | + file-url) |
| 160 | + (str file-url "?uuid=" (random-uuid)) |
| 161 | + (or file-url path))] |
| 162 | + (esm/dynamic-import path)))) |
154 | 163 | (.then (fn [mod]
|
155 | 164 | (register-module mod internal-name)
|
156 | 165 | mod))))
|
|
237 | 246 | feat (load-module feat libname as refer rename libspecs ns-opts)
|
238 | 247 | (string? libname)
|
239 | 248 | (let [libname (if (str/starts-with? libname "./")
|
240 |
| - (path/resolve (path/dirname (:file ns-opts)) libname) |
| 249 | + (path/resolve (path/dirname (or (:file ns-opts) ".")) |
| 250 | + libname) |
241 | 251 | libname)
|
242 | 252 | [libname properties*] (split-libname libname)
|
243 | 253 | munged (munge libname)
|
|
266 | 276 | (sci/add-class! internal-subname mod-field)
|
267 | 277 | (sci/add-import! current-ns internal-subname field))))))
|
268 | 278 | (handle-libspecs (next libspecs) ns-opts))
|
269 |
| - mod (js/Promise.resolve |
270 |
| - (-> |
271 |
| - (or |
272 |
| - ;; skip loading if module was already loaded |
273 |
| - (some-> (get @loaded-modules internal-name) |
274 |
| - js/Promise.resolve) |
275 |
| - (load-js-module libname internal-name) |
276 |
| - ;; else load module and register in loaded-modules under internal-name |
277 |
| - ) |
278 |
| - (.then (fn [mod] |
279 |
| - (if properties |
280 |
| - (gobj/getValueByKeys mod properties) |
281 |
| - mod)))))] |
| 279 | + mod (let [reload? (contains? (:opts ns-opts) :reload)] |
| 280 | + (js/Promise.resolve |
| 281 | + (-> |
| 282 | + (or |
| 283 | + ;; skip loading if module was already loaded |
| 284 | + (and (not reload?) |
| 285 | + (some-> (get @loaded-modules internal-name) |
| 286 | + js/Promise.resolve)) |
| 287 | + (load-js-module libname internal-name reload?) |
| 288 | + ;; else load module and register in loaded-modules under internal-name |
| 289 | + ) |
| 290 | + (.then (fn [mod] |
| 291 | + (if properties |
| 292 | + (gobj/getValueByKeys mod properties) |
| 293 | + mod))))))] |
282 | 294 | (-> mod
|
283 | 295 | (.then after-load)))
|
284 | 296 | :else
|
285 | 297 | ;; assume symbol
|
286 |
| - (if (sci/eval-form (ctx/get-ctx) (list 'clojure.core/find-ns (list 'quote libname))) |
| 298 | + (if (and (not (contains? (:opts ns-opts) :reload)) |
| 299 | + (sci/eval-form (ctx/get-ctx) (list 'clojure.core/find-ns (list 'quote libname)))) |
287 | 300 | ;; built-in namespace
|
288 | 301 | (do (sci/binding [sci/ns (:ns ns-opts)
|
289 | 302 | sci/file (:file ns-opts)]
|
|
336 | 349 | ns-obj (sci/binding [sci/ns @sci/ns]
|
337 | 350 | (sci/eval-form (ctx/get-ctx) (list 'do (list* 'ns ns-name other-forms) '*ns*)))
|
338 | 351 | libspecs (mapcat rest require-forms)
|
| 352 | + ns-opts (into #{} (filter keyword? libspecs)) |
| 353 | + libspecs (remove keyword? libspecs) |
339 | 354 | opts (assoc opts :ns ns-obj)]
|
340 |
| - (handle-libspecs libspecs opts))) |
| 355 | + (handle-libspecs libspecs (assoc opts :opts ns-opts)))) |
341 | 356 |
|
342 | 357 | (defn eval-require [require-form]
|
343 | 358 | (let [args (rest require-form)
|
| 359 | + args (remove keyword? args) |
344 | 360 | libspecs (mapv #(sci/eval-form (ctx/get-ctx) %) args)
|
345 | 361 | sci-ns @sci/ns
|
346 | 362 | sci-file @sci/file]
|
|
681 | 697 | (if *old-require*
|
682 | 698 | (apply old-require args)
|
683 | 699 | (await (.then (identity ;; with-async-bindings {sci/file @sci/file}
|
684 |
| - (handle-libspecs args {:ns @sci/ns |
685 |
| - :file @sci/file})) |
| 700 | + (let [opts (into #{} (filter keyword? args)) |
| 701 | + args (remove keyword? args)] |
| 702 | + (handle-libspecs args {:ns @sci/ns |
| 703 | + :file @sci/file |
| 704 | + :opts opts}))) |
686 | 705 | (fn [_]))))))
|
687 | 706 |
|
688 | 707 | (def ^:dynamic *file* sci/file) ;; make clj-kondo+lsp happy
|
|
0 commit comments