Environment Variables

Environment Variables

To disambiguate in your webpack.config.js between development and production builds, you may use environment variables.

The standard approach in Node.js modules can be applied: Set an environment variable when running webpack and refer to the variables using Node's process.env. The variable NODE_ENV is commonly used as de-facto standard (see here).

webpack.config.js

module.exports = {
  plugins: [
    new webpack.optimize.UglifyJsPlugin({
+      compress: process.env.NODE_ENV === 'production'
    })
  ]
};

Use the cross-env package to cross-platform-set environment variables:

package.json

{
  "scripts": {
    "build": "cross-env NODE_ENV=production PLATFORM=web webpack"
  }
}

References

© JS Foundation and other contributors
Licensed under the Creative Commons Attribution License 4.0.
https://webpack.js.org/guides/environment-variables

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部