rstacruz / webpack-tricks

Tips and tricks in using Webpack
2.35k stars 61 forks source link

Use EnvironmentPlugin instead of DefinePlugin #10

Closed pvorona closed 7 years ago

pvorona commented 7 years ago

In case you only want to set NODE_ENV, it is possible to pass it from env vars like this:

new webpack.EnvironmentPlugin([
  'NODE_ENV',
]),

no need to use define plugin

xavhan commented 7 years ago

I like to use this trick for this kind of things : when you use npm scripts for running webpack it set "npm_lifecycle_event" to the npm script name.

$ npm run build

const ENV = process.env.npm_lifecycle_event
if(ENV === 'build'){
  // production process
}
else{
  // dev process
}
rstacruz commented 7 years ago

In Webpack 2, doing webpack -p now sets NODE_ENV=production, so I think checking for env is best done with process.env.NODE_ENV. I've noted this now since I started adding in Webpack 2 notes.