[英]Figwheel environment variables
I understand Figwheel allows me to specify different builds. 我知道Figwheel允许我指定不同的版本。 (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.
例如,在开发人员中,我连接到某个API端点,而在生产中,它是一个不同的端点。 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.
理想情况下,我希望以某种方式(大概属于project.clj)指定环境特定的变量,然后在我的cljs代码中访问它们。
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. 然后在我的cljs代码中,我想以某种方式访问它们。 If it matters, I am running a Reagent project.
如果有关系,我正在运行一个Reagent项目。
One way to do it is via closure-defines
. 一种方法是通过
closure-defines
。
Eg in your project.clj
: 例如在您的
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. version
是我的项目在这里的定义。 So you can adjust that to reading an env-var etc. 因此,您可以将其调整为读取env-var等。
(def version
(->
(clojure.java.shell/sh "git" "describe" "--always")
:out
clojure.string/trim))
Then in your example.core
ns: 然后在你的
example.core
ns中:
(goog-define version "dev")
And then use it like a regular def
-ed thing. 然后用它像一个普通的
def
-ed的事情。
I'm assuming you've already read all about lein profiles . 我假设您已经阅读了有关lein配置文件的所有内容。 If you haven't seen it yet, be sure to check out Dynamic Eval in lein:
如果尚未看到,请确保在lein中查看Dynamic Eval :
{: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. 然后,您可以使用动态读取的信息来设置不同的
:main
或不同的:source-paths
以引入具有不同常量的代码。
Of course, don't forget to review all of the compiler options . 当然,不要忘记查看所有编译器选项 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.