webpack environment variables code example

Example 1: set env production webpack build

// webpack.config.js
module.exports = env => {
	const isProduction = env.NODE_ENV === 'production'
    // ...
    return {
      // ...
    }
}

// package.json
{
	...
    "scripts": {
    	"build": "webpack --env.NODE_ENV=production"
    },
    ...
}

Example 2: webpack env argument

// webpack.config.js
    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV === 'production' ? 'production' : 'development')
        })
    ]