envify will replace your environment variable checks with ordinary strings -
only the variables you use will be included, so you don't have to worry about,
say, AWS_SECRET_KEY leaking through either. Take this example script:
if (process.env.NODE_ENV === "development") { console.log('development only')}
After running it through envify with NODE_ENV set to production, you'll
get this:
if ("production" === "development") { console.log('development only')}
By running this through a good minifier (e.g.
terser), the above code would be
stripped out completely.
However, if you bundled the same script with NODE_ENV set to development:
if ("development" === "development") { console.log('development only')}
The if statement will evaluate to true, so the code won't be removed.
By default, environment variables that are not defined will be left untouched.
This is because in some cases, you might want to run an envify transform over
your source more than once, and removing these values would make that
impossible.
However, if any references to process.env are remaining after transforming
your source with envify, browserify will automatically insert its shim for
Node's process object, which will increase the size of your bundle. This weighs
in at around 2KB, so if you're trying to be conservative with your bundle size
you can "purge" these remaining variables such that any missing ones are simply
replaced with undefined.
To do so through the command-line, simply use the subarg syntax and include
purge after envify, e.g.:
browserify index.js -t [ @browserify/envify purge --NODE_ENV development ]
Or if you're using the module API, you can pass _: "purge" into your
arguments like so: