Link Search Menu Expand Document

Conditional build based on NODE_ENV

dumber has built-in support of NODE_ENV. For example, if you use NODE_ENV in app code like this:

if (process.env.NODE_ENV === 'production') {
  doSomething();
}

It will respect the NODE_ENV value captured at the bundling time.

# NODE_ENV is empty string
npx gulp build

# NODE_ENV is "production"
env NODE_ENV=production npx gulp build

The two different builds will capture different NODE_ENV value in generated bundle files.

Code removal

dumber not only captures the NODE_ENV value for app to consume at runtime, but it also pro-actively removes some conditional branch.

The very first code snippet in this page will be removed totally from final bundle when NODE_ENV is not production.

The code removal only works for very simple usage. It would not remove code for slightly more complex situations. For example:

const env = process.env;
if (env.NODE_ENV === 'production') {
  doSomething();
}

Or

if (aBoolean || process.env.NODE_ENV === 'production') {
  doSomething();
}

However it does work for simple if-else. One of the branches will be removed.

if (process.env.NODE_ENV === 'production') {
  doSomething();
} else {
  doSomethingElse();
}