cljs.repl.browser/send-static

functionsince v0.0-1211Edit
(send-static {path :path, :as request} conn {:keys [static-dir output-dir host port gzip?], :or {output-dir "out"}, :as opts})

Source code @ clojurescript:src/main/clojure/cljs/repl/browser.clj
(defn send-static
  [{path :path :as request} conn
   {:keys [static-dir output-dir host port gzip?] :or {output-dir "out"} :as opts}]
  (let [output-dir (when-not (.isAbsolute (io/file output-dir)) output-dir)]
    (if (and static-dir (not= "/favicon.ico" path))
      (let [path (if (= "/" path) "/index.html" path)
            local-path
            (cond->
              (seq (for [x (if (string? static-dir) [static-dir] static-dir)
                         :when (.exists (io/file (str x path)))]
                     (str x path)))
              (complement nil?) first)
            local-path
            (if (nil? local-path)
              (cond
                (re-find #".jar" path)
                (io/resource (second (string/split path #".jar!/")))
                (string/includes? path (System/getProperty "user.dir"))
                (io/file (string/replace path (str (System/getProperty "user.dir") "/") ""))
                (#{"/cljs-logo-icon-32.png" "/cljs-logo.svg"} path)
                (io/resource (subs path 1))
                :else nil)
              local-path)]
        (cond
          local-path
          (let [mime-type (path->mime-type ext->mime-type path "text/plain")
                encoding (mime-type->encoding mime-type "UTF-8")]
            (server/send-and-close conn 200 (slurp local-path :encoding encoding)
                                   mime-type encoding (and gzip? (or (= "text/javascript" mime-type)
                                                                     (= "application/wasm" mime-type)))))

          ;; "/index.html" doesn't exist, provide our own
          (= path "/index.html")
          (server/send-and-close conn 200
            (default-index (str output-dir "/main.js"))
            "text/html" "UTF-8")

          ;; "/main.js" doesn't exist, provide our own
          (= path (cond->> "/main.js" output-dir (str "/" output-dir )))
          (let [closure-defines (-> `{"goog.json.USE_NATIVE_JSON" true
                                      clojure.browser.repl/HOST ~host
                                      clojure.browser.repl/PORT ~port}
                                  (merge (:closure-defines @browser-state))
                                  cljsc/normalize-closure-defines
                                  json/write-str)]
            (server/send-and-close conn 200
              (str "var CLOSURE_UNCOMPILED_DEFINES = " closure-defines ";\n"
                   "var CLOSURE_NO_DEPS = true;\n"
                   "document.write('');\n"
                   "document.write('');\n"
                   (when (.exists (io/file output-dir "cljs_deps.js"))
                     (str "document.write('');\n"))
                   "document.write('');\n"
                   "document.write('');\n")
              "text/javascript" "UTF-8"))

          :else (server/send-404 conn path)))
      (server/send-404 conn path))))