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.

Tree shaking only works with process.env.NODE_ENV?

See original GitHub issue

I’m finding that tree shaking only works with process.env.NODE_ENV and won’t work with process.env.REACT_APP_[my variable name]

For example, if I do the following myModule is not showing up in my production build.

import {myFunction} from './myModule'

if(process.env.NODE_ENV === 'development') {
    myFunction()
}

But, if I want to use a different environment variable myModule is always included.

import {myFunction} from './myModule'

if(process.env.REACT_APP_ENABLE_MY_FUNCTION === 'true') {
    myFunction()
}

Is it known that tree shaking only works with process.env.NODE_ENV? Is there any way to do tree shaking with a custom environment variable?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
NAllred91commented, Sep 21, 2020

Here it is…

If you want to do this:

import {myFunction} from './myModule'

if(process.env.REACT_APP_ENABLE_MY_FUNCTION === 'true') {
    myFunction()
}

You must be sure to define REACT_APP_ENABLE_MY_FUNCTION. If you do not define it then the if(process.env.REACT_APP_ENABLE_MY_FUNCTION === 'true') doesn’t get turned into if(false) at build time.

But it still evaluates to false at runtime.

So when using this command (REACT_APP_ENABLE_MY_FUNCTION=false npm run build), myModule is not included in the build. But if I just run npm run build without explicitly setting REACT_APP_ENABLE_MY_FUNCTION it is included.

I don’t see this documented anywhere, but it probably should be. Although I realize this behavior comes from something create-react-app depends on and not create-react-app itself. The only way I figured this out was by taking the time to go through the generated bundle.

4reactions
zoubingwucommented, Apr 12, 2022

Async imports only split code to another chunk but will not prevent it from bundling it to final dist.

My solution is to always define a value with webpack define plugin:

new webpack.DefinePlugin({
    'process.env.MOCK': JSON.stringify(process.env.MOCK === 'true')
}),

In you app code, don’t do any comparison check, so it will be replaced at build time and resolves to if (false) {...} then the whole import will be eliminated.

import startMock from './mock'

// don't do any comparison here.
if (process.env.MOCK) {
    startMock()
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree Shaking - webpack
Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module...
Read more >
javascript - 'process.env.NODE_ENV' replaced as a string or ...
Now, the tricky thing here is: Webpack does the deadcode elimination (a.k.a Tree shaking) during the build time, which removes all the branches ......
Read more >
Tree-Shaking: A Reference Guide - Smashing Magazine
In this article, we dive deeper on how exactly it works and how specs and practice intertwine to make bundles leaner and more...
Read more >
Environment Variables - SurviveJS
Starting from webpack 4, process.env.NODE_ENV is set within the build based on the given mode but not globally. To pass the variable to...
Read more >
rollpkg - npm
The esm build supports tree shaking and is ready to be used in ... builds are minified and any code that is gated...
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