|
135 | 135 | (defn send-static |
136 | 136 | [{path :path :as request} conn |
137 | 137 | {:keys [static-dir output-to output-dir host port gzip?] :or {output-dir "out"} :as opts}] |
138 | | - (if (and static-dir (not= "/favicon.ico" path)) |
139 | | - (let [path (if (= "/" path) "/index.html" path) |
| 138 | + (let [output-dir (when-not (.isAbsolute (io/file output-dir)) output-dir)] |
| 139 | + (if (and static-dir (not= "/favicon.ico" path)) |
| 140 | + (let [path (if (= "/" path) "/index.html" path) |
| 141 | + local-path |
| 142 | + (cond-> |
| 143 | + (seq (for [x (if (string? static-dir) [static-dir] static-dir) |
| 144 | + :when (.exists (io/file (str x path)))] |
| 145 | + (str x path))) |
| 146 | + (complement nil?) first) |
| 147 | + local-path |
| 148 | + (if (nil? local-path) |
| 149 | + (cond |
| 150 | + (re-find #".jar" path) |
| 151 | + (io/resource (second (string/split path #".jar!/"))) |
| 152 | + (string/includes? path (System/getProperty "user.dir")) |
| 153 | + (io/file (string/replace path (str (System/getProperty "user.dir") "/") "")) |
| 154 | + (#{"/cljs-logo-icon-32.png" "/cljs-logo.svg"} path) |
| 155 | + (io/resource (subs path 1)) |
| 156 | + :else nil) |
| 157 | + local-path)] |
| 158 | + (cond |
140 | 159 | local-path |
141 | | - (cond-> |
142 | | - (seq (for [x (if (string? static-dir) [static-dir] static-dir) |
143 | | - :when (.exists (io/file (str x path)))] |
144 | | - (str x path))) |
145 | | - (complement nil?) first) |
146 | | - local-path |
147 | | - (if (nil? local-path) |
148 | | - (cond |
149 | | - (re-find #".jar" path) |
150 | | - (io/resource (second (string/split path #".jar!/"))) |
151 | | - (string/includes? path (System/getProperty "user.dir")) |
152 | | - (io/file (string/replace path (str (System/getProperty "user.dir") "/") "")) |
153 | | - (#{"/cljs-logo-icon-32.png" "/cljs-logo.svg"} path) |
154 | | - (io/resource (subs path 1)) |
155 | | - :else nil) |
156 | | - local-path)] |
157 | | - (cond |
158 | | - local-path |
159 | | - (if-let [ext (some #(if (.endsWith path %) %) (keys ext->mime-type))] |
160 | | - (let [mime-type (ext->mime-type ext "text/plain") |
161 | | - encoding (mime-type->encoding mime-type "UTF-8")] |
162 | | - (server/send-and-close conn 200 (slurp local-path :encoding encoding) |
163 | | - mime-type encoding (and gzip? (= "text/javascript" mime-type)))) |
164 | | - (server/send-and-close conn 200 (slurp local-path) "text/plain")) |
165 | | - ;; "/index.html" doesn't exist, provide our own |
166 | | - (= path "/index.html") |
167 | | - (server/send-and-close conn 200 |
168 | | - (default-index (or output-to (str output-dir "/main.js"))) "text/html" "UTF-8") |
169 | | - (= path (str "/" output-dir "/main.js") ) |
170 | | - (let [closure-defines (-> `{clojure.browser.repl/HOST ~host |
171 | | - clojure.browser.repl/PORT ~port} |
172 | | - cljsc/normalize-closure-defines |
173 | | - json/write-str)] |
| 160 | + (if-let [ext (some #(if (.endsWith path %) %) (keys ext->mime-type))] |
| 161 | + (let [mime-type (ext->mime-type ext "text/plain") |
| 162 | + encoding (mime-type->encoding mime-type "UTF-8")] |
| 163 | + (server/send-and-close conn 200 (slurp local-path :encoding encoding) |
| 164 | + mime-type encoding (and gzip? (= "text/javascript" mime-type)))) |
| 165 | + (server/send-and-close conn 200 (slurp local-path) "text/plain")) |
| 166 | + ;; "/index.html" doesn't exist, provide our own |
| 167 | + (= path "/index.html") |
174 | 168 | (server/send-and-close conn 200 |
175 | | - (str "var CLOSURE_UNCOMPILED_DEFINES = " closure-defines ";\n" |
176 | | - "var CLOSURE_NO_DEPS = true;\n" |
177 | | - "document.write('<script src=\"" output-dir "/goog/base.js\"></script>');\n" |
178 | | - "document.write('<script src=\"" output-dir "/goog/deps.js\"></script>');\n" |
179 | | - (when (.exists (io/file output-dir "cljs_deps.js")) |
180 | | - "document.write('<script src=\"" output-dir "/cljs_deps.js\"></script>');\n") |
181 | | - "document.write('<script src=\"" output-dir "/brepl_deps.js\"></script>');\n" |
182 | | - "document.write('<script>goog.require(\"clojure.browser.repl.preload\");</script>');\n") |
183 | | - "text/javascript" "UTF-8")) |
184 | | - :else (server/send-404 conn path))) |
185 | | - (server/send-404 conn path))) |
| 169 | + (default-index (or output-to (str output-dir "/main.js"))) |
| 170 | + "text/html" "UTF-8") |
| 171 | + (= path (cond->> "/main.js" output-dir (str "/" output-dir ))) |
| 172 | + (let [closure-defines (-> `{clojure.browser.repl/HOST ~host |
| 173 | + clojure.browser.repl/PORT ~port} |
| 174 | + cljsc/normalize-closure-defines |
| 175 | + json/write-str)] |
| 176 | + (server/send-and-close conn 200 |
| 177 | + (str "var CLOSURE_UNCOMPILED_DEFINES = " closure-defines ";\n" |
| 178 | + "var CLOSURE_NO_DEPS = true;\n" |
| 179 | + "document.write('<script src=\"" output-dir "/goog/base.js\"></script>');\n" |
| 180 | + "document.write('<script src=\"" output-dir "/goog/deps.js\"></script>');\n" |
| 181 | + (when (.exists (io/file output-dir "cljs_deps.js")) |
| 182 | + "document.write('<script src=\"" output-dir "/cljs_deps.js\"></script>');\n") |
| 183 | + "document.write('<script src=\"" output-dir "/brepl_deps.js\"></script>');\n" |
| 184 | + "document.write('<script>goog.require(\"clojure.browser.repl.preload\");</script>');\n") |
| 185 | + "text/javascript" "UTF-8")) |
| 186 | + :else (server/send-404 conn path))) |
| 187 | + (server/send-404 conn path)))) |
186 | 188 |
|
187 | 189 | (server/dispatch-on :get |
188 | 190 | (fn [{:keys [path]} _ _] |
|
0 commit comments