Skip to content

Commit 537f60c

Browse files
author
dnolen
committed
CLJS-2280: Provide process.env :preload and auto-configure
1 parent 6ef5335 commit 537f60c

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/main/clojure/cljs/closure.clj

+20-8
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
:emit-constants :ups-externs :ups-foreign-libs :ups-libs :warning-handlers :preloads
168168
:browser-repl :cache-analysis-format :infer-externs :closure-generate-exports :npm-deps
169169
:fn-invoke-direct :checked-arrays :closure-module-roots :rewrite-polyfills :use-only-custom-externs
170-
:watch :watch-error-fn :watch-fn :install-deps})
170+
:watch :watch-error-fn :watch-fn :install-deps :process-shim})
171171

172172
(def string->charset
173173
{"iso-8859-1" StandardCharsets/ISO_8859_1
@@ -1955,7 +1955,9 @@
19551955
(format ":nodejs target with :none optimizations requires a :main entry")))
19561956

19571957
(defn check-preloads [{:keys [preloads optimizations] :as opts}]
1958-
(when (and (some? preloads) (not= optimizations :none))
1958+
(when (and (some? preloads)
1959+
(not= preloads '[process.env])
1960+
(not= optimizations :none))
19591961
(binding [*out* *err*]
19601962
(println "WARNING: :preloads should only be specified with :none optimizations"))))
19611963

@@ -2038,18 +2040,28 @@
20382040
(update opts :modules
20392041
#(ensure-cljs-base-module % opts)))
20402042

2043+
(defn shim-process? [opts]
2044+
(not (false? (:process-shim opts))))
2045+
20412046
(defn add-implicit-options
20422047
[{:keys [optimizations output-dir]
20432048
:or {optimizations :none
20442049
output-dir "out"}
20452050
:as opts}]
20462051
(let [opts (cond-> opts
2047-
(:closure-defines opts)
2048-
(assoc :closure-defines
2049-
(into {}
2050-
(map (fn [[k v]]
2051-
[(if (symbol? k) (str (comp/munge k)) k) v])
2052-
(:closure-defines opts))))
2052+
(shim-process? opts)
2053+
(-> (update-in [:preloads] (fnil conj []) 'process.env)
2054+
(cond->
2055+
(not= :none optimizations)
2056+
(update-in [:closure-defines 'process.env/NODE_ENV] (fnil str "production"))))
2057+
2058+
(or (:closure-defines opts) (shim-process? opts))
2059+
(update :closure-defines
2060+
(fn [defines]
2061+
(into {}
2062+
(map (fn [[k v]]
2063+
[(if (symbol? k) (str (comp/munge k)) k) v])
2064+
defines))))
20532065
(:browser-repl opts)
20542066
(update-in [:preloads] (fnil conj []) 'clojure.browser.repl.preload))
20552067
{:keys [libs foreign-libs externs]} (get-upstream-deps)

0 commit comments

Comments
 (0)