Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access environment variables in client side code during build in Meteor?

Tags:

meteor

Related:

  • https://github.com/meteor/meteor/issues/11287
  • how to use environment variable in client and server at the very beginning

I'm also aware of METEOR_SETTINGS.

I have a Meteor app that will be deployed for different clients with different theming/features. I want to use an env variable such as process.env.CLIENT and have code like if(process.env.CLIENT === 'foo'). During build the env variable should be replaced and dead code elimination will remove/strip the if-block. This already works with NODE_ENV.

I don't want to ship code that a certain client does not need and I also don't want to implicitly expose the list of other clients. Both these things would happen if I check Meteor.settings at runtime. I need this to happen at compile time. Each client gets a different build.

How do I get environment variables through to the client? How are others solving this? process.env on the client currently is:

{"NODE_ENV":"development","TEST_METADATA":"{}"}

In other build tools I can either access all env variables on the client or those with a special prefix (for security reasons) like FOO_.

like image 854
Prinzhorn Avatar asked Oct 24 '25 07:10

Prinzhorn


1 Answers

Creating multiple, distinct builds is not something Meteor support natively. I think it's also fairly unusual.

However, since it is separate processes, you can just build support for it yourself using a bash script similar to this:

#!/bin/bash

FOO_VALUE=$1
echo "Using variable value $FOO_VALUE";

for file in $(find imports/ui/); do
  echo "Replacing special variables in $file"
  sed -i "s/FOO_CLIENT/$FOO_VALUE/g" $file;
done

DIR="build_$FOO_VALUE/"
mkdir -p $DIR
meteor build -d $DIR
like image 110
Christian Fritz Avatar answered Oct 27 '25 02:10

Christian Fritz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!