Tree shaking only works with process.env.NODE_ENV?
See original GitHub issueI’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:
- Created 3 years ago
- Reactions:6
- Comments:13 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here it is…
If you want to do this:
You must be sure to define
REACT_APP_ENABLE_MY_FUNCTION
. If you do not define it then theif(process.env.REACT_APP_ENABLE_MY_FUNCTION === 'true')
doesn’t get turned intoif(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 runnpm run build
without explicitly settingREACT_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.
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:
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.