fasterthanlime / shin

:warning: (def shin (dissoc clojurescript :jvm :google_closure)) (deprecated)
MIT License
35 stars 1 forks source link

Pretend clojure.java.io is accessible #111

Open fasterthanlime opened 9 years ago

fasterthanlime commented 9 years ago

In Reagent, interop.clj, apart from #110, has one big problem:

It requires a clojure.java thing:

(:require [clojure.java.io :as io])

And then uses it here:

(defmacro import-react
  []
  "Import React.js.
  This can be used instead of adding :preamble in project.clj
  (or adding react.js in a script tag). This may be more convenient when
  using :optimizations :none, since that doesn't take :preamble into account.
  Imports minimized version of React if :elide-asserts is true."
  (if-not (or (nil? @react-import-ns)
              (= *ns* @react-import-ns))
    ;; React was already imported in another namespace; so we avoid
    ;; duplicate imports.
    true
    (let [srcfile (if *assert* "reagent/react.js"
                    "reagent/react.min.js")
          src (slurp (io/resource srcfile))]
      (if (nil? @react-import-ns)
        (reset! react-import-ns *ns*))
      `(js/eval ~(str "if (typeof React != 'undefined' &&
                      typeof console != 'undefined') {
                      console.log('WARNING: React is already defined');
                      }"
                      src "; \n"
                      "if (typeof module != 'undefined' &&
                      typeof global != 'undefined' &&
                      module.exports && module.exports.DOM) {
                      global.React = module.exports;
                      } \n
                      //@ sourceURL=" srcfile "\n")))))

It doesn't look too hard to fake io/resource and slurp for those needs.

It looks like io/resource gets from a pathlist configured in project.clj, in resource-paths, cf. https://github.com/reagent-project/reagent/blob/master/project.clj#L31