1

I have an Express+React app and I use webpack.

On dev I run "webpack --watch" but before deploying to production, I want to uglify my js.

Currently I uncomment the uglify plugin each time before production deployment.

Can it be done with some parameter passed to webpack command or something?

Thanks, Alex A.

alexarsh
  • 5,123
  • 12
  • 42
  • 51
  • Possible duplicate of [Webpack how to build production code and how to use it](http://stackoverflow.com/questions/35054082/webpack-how-to-build-production-code-and-how-to-use-it) – slugo Sep 22 '16 at 20:09

1 Answers1

2

You can specify an environment variable in your script like so:

package.json

"scripts": {
  "build": "webpack --watch",
  "build:production": "NODE_ENV=production webpack"
}

Then, in your webpack config, make sure to add uglify to your plugins like so

webpack.config.js

...
const config = {
  /* your regular config applicable to all environments */
}

// modifications to config when ran with NODE_ENV = production
if (process.env.NODE_ENV === 'production') {
  config.plugins = config.plugins.concat([
    new webpack.optimize.UglifyJsPlugin()
  ])
}

module.exports = config;
Mario Tacke
  • 5,378
  • 3
  • 30
  • 49