简体   繁体   中英

Figwheel environment variables

I understand Figwheel allows me to specify different builds. (Perhaps another way to think of them is as environments?)

Based on the build/environment, I may require a different behavior in my code. For example, when in dev, I connect to a certain API endpoint, and in prod it's a different endpoint. Ideally, I would like some way (presumably this belongs in project.clj) of specifying environment specific variables, and then access them in my cljs code.

Is there a mechanism to do this?

I'm picturing something like this:

:cljsbuild {
    :builds [{:id "dev"
              :source-paths ["src"]
              :figwheel true
              :env-variables {foo "bar"
                              bar "foo"} ; <-------
              :compiler {:main hello-seymore.core 
                         :asset-path "cljs/out"
                         :output-to  "resources/public/cljs/main.js"
                         :output-dir "resources/public/cljs/out"} 
             }
             {:id "prod"
              :env-variables {foo "different value for foo"
                              bar "different value for bar"}}] ; <-------
              ; etc
   }

And then in my cljs code I would like to access them somehow. If it matters, I am running a Reagent project.

One way to do it is via closure-defines .

Eg in your project.clj :

:cljsbuild { ; ...
            :builds [{:id "min"
                      ;;; XXX: map with your values
                      :compiler {:closure-defines {"example.core.version" ~version}
                                 ; ...

version is a def in my project here. So you can adjust that to reading an env-var etc.

(def version
  (->
    (clojure.java.shell/sh "git" "describe" "--always")
    :out
    clojure.string/trim))

Then in your example.core ns:

(goog-define version "dev")

And then use it like a regular def -ed thing.

I'm assuming you've already read all about lein profiles . If you haven't seen it yet, be sure to check out Dynamic Eval in lein:

{:user {:compile-path  #=(eval (System/getenv "ci.compile-path" )),
        :target-path   #=(eval (System/getenv "ci.target-path"  )) }}

You could then use the dynamically read information to set a different :main , or a different :source-paths to pull in code with different constants.

Of course, don't forget to review all of the compiler options .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM