React: configuring webpack.config.js for production

First of all, here we will be using es6 syntax so we will need to rename our webpack.config.js file to webpack.config.babel.js so that it can be compiled.

Once we do so, we can use import instead of require and export default {} instead of module.exports = {}

Then we need to add the following:

Import webpack:

import webpack from 'webpack'

Check for production:

const LAUNCH_COMMAND = process.env.npm_lifecycle_event

const isProduction = LAUNCH_COMMAND === 'production'

const productionPlugin = new webpack.DefinePlugin({
  'process.env': {
    NODE_ENV: JSON.stringify('production')
  }
})

Add common properties such as ‘base’, ‘output’, and ‘module’ in base json:

const base = {
  entry: [ ... ],
  output: { ... },
  module: {
    loaders: [ ... ]
  }
}

Specify dev and production configs:

const devConfig = {
  plugins: [...]
}

const productionConfig = {]
  plugins: [productionPlugin, ...]
}

Merge objects to export:

export default Object.assign({}, base,
  isProduction === true
    ? productionConfig
    : devConfig
)