|
54 | 54 | (defn random-string [length]
|
55 | 55 | (apply str (take length (repeatedly random-char))))
|
56 | 56 |
|
| 57 | +(defn- sym->var |
| 58 | + "Converts a namespaced symbol to a var, loading the requisite namespace if |
| 59 | + needed. For use with a function defined under a keyword in opts. The kw and |
| 60 | + ex-data arguments are used to form exceptions." |
| 61 | + [sym kw ex-data] |
| 62 | + (let [ns (namespace sym) |
| 63 | + _ (when (nil? ns) |
| 64 | + (throw |
| 65 | + (ex-info (str kw " symbol " sym " is not fully qualified") |
| 66 | + (merge ex-data {kw sym})))) |
| 67 | + var-ns (symbol ns)] |
| 68 | + (when (not (find-ns var-ns)) |
| 69 | + (try |
| 70 | + (locking ana/load-mutex |
| 71 | + (require var-ns)) |
| 72 | + (catch Throwable t |
| 73 | + (throw (ex-info (str "Cannot require namespace referred by " kw " value " sym) |
| 74 | + (merge ex-data {kw sym}) |
| 75 | + t))))) |
| 76 | + |
| 77 | + (find-var sym))) |
| 78 | + |
| 79 | +(defn- opts-fn |
| 80 | + "Extracts a function from opts, by default expecting a function value, but |
| 81 | + converting from a namespaced symbol if needed." |
| 82 | + [kw opts] |
| 83 | + (when-let [fn-or-sym (kw opts)] |
| 84 | + (cond-> fn-or-sym (symbol? fn-or-sym) (sym->var kw {})))) |
| 85 | + |
57 | 86 | ;; Closure API
|
58 | 87 | ;; ===========
|
59 | 88 |
|
|
2469 | 2498 | (js-transforms js-module opts)
|
2470 | 2499 |
|
2471 | 2500 | (symbol? preprocess)
|
2472 |
| - (let [ns (namespace preprocess) |
2473 |
| - _ (when (nil? ns) |
2474 |
| - (throw |
2475 |
| - (ex-info (str "Preprocess symbol " preprocess " is not fully qualified") |
2476 |
| - {:file (:file js-module) |
2477 |
| - :preprocess preprocess}))) |
2478 |
| - preprocess-ns (symbol ns)] |
2479 |
| - (when (not (find-ns preprocess-ns)) |
2480 |
| - (try |
2481 |
| - (locking ana/load-mutex |
2482 |
| - (require preprocess-ns)) |
2483 |
| - (catch Throwable t |
2484 |
| - (throw (ex-info (str "Cannot require namespace referred by :preprocess value " preprocess) |
2485 |
| - {:file (:file js-module) |
2486 |
| - :preprocess preprocess} |
2487 |
| - t))))) |
2488 |
| - |
| 2501 | + (let [preprocess-var (sym->var preprocess :preprocess {:file (:file js-module)})] |
2489 | 2502 | (try
|
2490 |
| - ((find-var preprocess) js-module opts) |
| 2503 | + (preprocess-var js-module opts) |
2491 | 2504 | (catch Throwable t
|
2492 | 2505 | (throw (ex-info (str "Error running preprocessing function " preprocess)
|
2493 |
| - {:file (:file js-module) |
2494 |
| - :preprocess preprocess} |
2495 |
| - t))))) |
| 2506 | + {:file (:file js-module) |
| 2507 | + :preprocess preprocess} |
| 2508 | + t))))) |
2496 | 2509 |
|
2497 | 2510 | :else
|
2498 | 2511 | (do
|
|
2895 | 2908 | "Given a source directory, produce runnable JavaScript. Watch the source
|
2896 | 2909 | directory for changes rebuilding when necessary. Takes the same arguments as
|
2897 | 2910 | cljs.closure/build in addition to some watch-specific options:
|
2898 |
| - - :watch-fn, a function of no arguments to run after a successful build. |
2899 |
| - - :watch-error-fn, a function receiving the exception of a failed build." |
| 2911 | + - :watch-fn, a function of no arguments to run after a successful build. May |
| 2912 | + be a function value or a namespaced symbol identifying a function, |
| 2913 | + in which case the associated namespace willl be loaded and the |
| 2914 | + symbol resolved. |
| 2915 | + - :watch-error-fn, a function receiving the exception of a failed build. May |
| 2916 | + be a function value or a namespaced symbol, loaded as |
| 2917 | + with :watch-fn." |
2900 | 2918 | ([source opts]
|
2901 | 2919 | (watch source opts
|
2902 | 2920 | (if-not (nil? env/*compiler*)
|
|
2919 | 2937 | (println "... done. Elapsed"
|
2920 | 2938 | (/ (unchecked-subtract (System/nanoTime) start) 1e9) "seconds")
|
2921 | 2939 | (flush))
|
2922 |
| - (when-let [f (:watch-fn opts)] |
| 2940 | + (when-let [f (opts-fn :watch-fn opts)] |
2923 | 2941 | (f))
|
2924 | 2942 | (catch Throwable e
|
2925 |
| - (if-let [f (:watch-error-fn opts)] |
| 2943 | + (if-let [f (opts-fn :watch-error-fn opts)] |
2926 | 2944 | (f e)
|
2927 | 2945 | (binding [*out* *err*]
|
2928 | 2946 | (println (Throwables/getStackTraceAsString e)))))))
|
|
0 commit comments