我知道Figwheel允许我指定不同的构建。(也许将它们视为环境的另一种方式是?)
根据构建/环境的不同,我可能需要在代码中使用不同的行为。例如,在dev中,我连接到某个API端点,而在prod中,它是一个不同的端点。理想情况下,我希望以某种方式(假设这属于project.clj)指定特定于环境的变量,然后在我的cljs代码中访问它们。
有没有这样做的机制?
我的想象是这样的:
: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
}然后在我的cljs代码中,我想以某种方式访问它们。如果重要的话,我正在运行一个Reagent项目。
发布于 2018-04-27 13:38:02
一种方法是通过closure-defines。
例如,在您的project.clj中:
:cljsbuild { ; ...
:builds [{:id "min"
;;; XXX: map with your values
:compiler {:closure-defines {"example.core.version" ~version}
; ...在我的项目中,version是一个def。因此,您可以将其调整为读取env-var等。
(def version
(->
(clojure.java.shell/sh "git" "describe" "--always")
:out
clojure.string/trim))然后在你的example.core ns中:
(goog-define version "dev")然后像使用常规的def-ed一样使用它。
发布于 2018-04-27 23:34:06
我假设你已经读过关于lein profiles的所有内容了。如果你还没有看过它,请务必在lein中查看Dynamic Eval:
{:user {:compile-path #=(eval (System/getenv "ci.compile-path" )),
:target-path #=(eval (System/getenv "ci.target-path" )) }}然后,您可以使用动态读取的信息来设置不同的:main,或者使用不同的:source-paths来引入具有不同常量的代码。
当然,别忘了回顾一下the compiler options的全部内容。
https://stackoverflow.com/questions/50055610
复制相似问题