Use environment variables as compiler defines.
Adding this library to your build will turn all available environment variables into compiler defines prefixed with env.*
.
You can then use them like so:
#if env.CI
doSomethingSpecific();
#end
#if (env.NODE_ENV == "production")
trace('seems like this is a production build');
#end
Additionally, there's a define called env.os.<platform>
, where platform
is the lower cased result of Sys.systemName()
. Use at your own risk.
Ideally, functionality like this would come directly from the compiler, but for the time being this library can do the trick with certain limitations:
- Be sure to list
-lib env-define
as far ahead in your build args as possible. This library uses init macros to add defines and if this happens after typing started, things can go quite horribly wrong, e.g.: HaxeFoundation/haxe#8368 - The defines will not be availble in macro context
- Depending on how many environment vars you have, you may find yourself burried in env defines. When printing
haxe.macro.Context.getDefines()
you'll probably want to filter out keys having the prefix.