[Translation] webpack official website documentation: Guide -- 19. Using environment variables

Original translation, please indicate the source when reprinting.

Original address: https://webpack.js.org/guides/environment-variables/

 

You can use environment variables to eliminate the setting differences between development builds and production builds in webpack.config.js . You can take advantage of the standard access in Node.js modules: set an environment variable when running webpack , use process.env to point to the variable. The variable NODE_ENV is a commonly used de facto standard.

webpack.config.js

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

 

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

package.json

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

 

refer to

 https://blog.flennik.com/the-fine-art-of-the-webpack-2-config-dc4d19d7f172#.297u8iuz1

 

-- End --

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326891251&siteId=291194637