question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Build time ENV vars / constants

See original GitHub issue

❔ Question

  • Is it possible to define global constants at build time, the same way you would do that with webpack’s DefinePlugin?
  • Is it also possible to filter out parts of the application code based on these constants?

🔦 Context

I would like to be able to set global variables by means of ENV variables set either in an .env file or on the command line. These should be inlined into the code at compile time, and as such the user should have no influence over them at run time. This differs from the ENV vars that parcel already support, which can be changed at run time.

Look at the code sample below for an example. It is a contrived example but the method is quite powerful for stitching together/removing code for multiple versions of an app, but keeping one code base.

💻 Code Sample

Below is an example of how I would do this in webpack.

Defining the constants in the webpack DefinePlugin:

new webpack.DefinePlugin({
    ICE_CREAM: JSON.stringify(process.env.ICE_CREAM === "true")
});

In webpack I can filter out the certain parts of the code based on these constants:

if (ICE_CREAM) {
    console.log("yay! we have ice cream!");
} else {
    console.log("dang! no ice cream!");
}

Since webpack inlines the values of the constants and removes dead code, this would translate after building (when running webpack with env var ICE_CREAM=“true”) to:

if (true) { console.log("yay! we have ice cream!"); }

and to the following when the env var ICE_CREAM is not set:

if (false) {} else { console.log("dang! no ice cream!"); }

(I am not sure that this is exactly how it is translated in webpack, but you get the idea) 😃

🌍 Your Environment

Software Version(s)
Parcel 1.12.3
Node 12.1.0
Yarn 1.15.2
Operating System os x

KUDO’s

Thanks for an awesome piece of software! Parcel really improved my workflow tremendously. 👏

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tysoehcommented, Mar 30, 2020

Another possible solution: parcel-plugin-elecron-dotenv. The babel plugin wasn’t ideal for us; we wanted something that exactly mimicked the behavior of Parcel for browser targets.

0reactions
jakoblaegdsmandcommented, Jul 17, 2019

Ok, clear. Thanks for the pointer, I looked myself for this in the codebase, but couldn’t pinpoint it.

For people in the same situation as described above, looking for a solution to this: You can use this babel plugin: https://babeljs.io/docs/en/babel-plugin-transform-inline-environment-variables. It will inline ENV variables with a whitelist.

Thanks for the help, closing!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting Default Docker Environment Variables During Image ...
What if you want to specify default ENV values while building your Docker image? Do you really need to edit your Dockerfile every...
Read more >
Environment variables as build-time variables #675 - GitHub
MyEnvVar doesn't have a value in build time. I think what is said about build arguments: all your environmental variables are available in...
Read more >
Docker Tip #47: Build Time vs Run Time ENV Variables
Docker allows you to set both build time and run time ENV variables and even lets you overwrite build time ENV vars at...
Read more >
Buildtime vs runtime environment variables with Next.js and ...
For a Next.js app, buildtime environment variables are variables that are used when the next build command runs. Runtime variables are ...
Read more >
Resolving Variables at Build Time
1. In the Advanced Designer, on the Project page, click Variables. · 2. Click the Build Time Variables button. · 3. Click the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found