Skip to content

Instantly share code, notes, and snippets.

@tcrosen
Last active January 27, 2022 15:23
Show Gist options
  • Save tcrosen/736bf01efe10c20ac5bf to your computer and use it in GitHub Desktop.
Save tcrosen/736bf01efe10c20ac5bf to your computer and use it in GitHub Desktop.
How to exclude development/test code from production builds in 30 seconds with Webpack

webpack.config.js

var webpack = require('webpack');

var featureFlagsPlugin = new webpack.DefinePlugin({
  __DEV__: !!process.env.DEV,
  __RELEASE__: !!process.env.RELEASE
});

module.exports = {
  entry: {
    app: './index.js'
  },
  
  // ...
  
  plugins: [featureFlagsPlugin]
};

index.js

console.log('Hello from your library');

if (__DEV__) {
  console.log('in __DEV__ mode');
}

if (__RELEASE__) {
  console.log('in __RELEASE__ mode');
}
$ webpack 
$ DEV=1 webpack 
$ RELEASE=1 webpack 

Windows

> webpack
> set DEV=1 && webpack 
> set RELEASE=1 && webpack 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment