Skip to content

Examining how webpack removed code from the bundle depending on mode

Notifications You must be signed in to change notification settings

pahund/webpack-devprod-experiment

Repository files navigation

webpack-devprod-experiment

With webpack 4, you can set mode: 'production' in your webpack config.

In your code, check for development mode like this (see index.js):

if (process.env.NODE_ENV === 'development') {
    console.log('********** DEVELOPMENT **********');
}

When webpack creates a bundle with mode: 'production', all the code inside these if clauses, along with the if clauses themselves, will be automatically removed from the bundle.

There is no need to use the define plugin explicitly (it is used by webpack “under the hood”), and it's not necessary to use something like webpack-unassert-loader or webpack-strip-block.

I've made this repo to demonstrate. After checking out the code and installing dependencies with npm install, you can run either of these commands:

  • npm run build-dev
  • npm run build-prod

…to have webpack create a bundle-dev.js and a bundle-prod.js file, respectively.

If you take a look at those bundles, you'll see that the log statement ********** PRODUCTION ********** is only included in the production bundle, and the log statement ********** DEVELOPMENT ********** only in the development bundle.

See related question on Stack Overflow: https://stackoverflow.com/questions/45776264/remove-some-code-lines-in-production-distribution-files

About

Examining how webpack removed code from the bundle depending on mode

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published