score:1

Accepted answer

you'll need to take a look at defineplugin.

its job is to find instances of a given string, then replace them with some other string. add this code to your plugins array to configure it to build the production version of react.

new webpack.defineplugin({
  'process.env.node_env': json.stringify('production')
})

then make sure you are using the uglifyjs plugin too and that it comes after the defineplugin in your config.

this will replace all of the following expressions:

if (process.env.node_env === "development") {
  // do slow development code
}

with code that looks like:

if ("production" === "development") {
  // do slow development code
}

uglify can recognise this at compile time as an if expression that will never be true, and remove the entire statement.

if you want to remove all console.log statements, the easiest way is to make it a no op in an initialization file.

if (process.env.node_env === "production") {
  console.log = () => {};
  console.info = () => {};
  // ...
}

make sure this code runs before before any of the files that might have logging statements in.


Related Query

More Query from same tag