场景描述

为了开发方便,我使用mac笔记本在公司开发,所以我是通过链接wifi来获取白名单IP来访问公司gitlab的地址
但是不便的是WIFI提供的ip是不断变化化的,比如上午,下午晚上,吃个饭的功夫,再打开电脑ip就不一样了,本来ip变了也不用太在意,但是项目的历史债务问题,通过修改package.json和webpack的config文件来修改开发分支的IP

优化前

  1. "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js --host 192.168.3.197",
  1. host: '192.168.3.197',

优化后

  1. "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
  1. 'use strict'
  2. const path = require('path')
  3. function getIPAdress() {
  4. var interfaces = require('os').networkInterfaces();  
  5. for (var devName in interfaces) {    
  6. var iface = interfaces[devName];      
  7. for (var i = 0; i < iface.length; i++) {
  8. var alias = iface[i];
  9. if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
  10. return alias.address;
  11. }
  12. }  
  13. }
  14. }
  15. var ip = getIPAdress();
  16. module.exports = {
  17. dev: {
  18. // Paths
  19. assetsSubDirectory: 'static',
  20. assetsPublicPath: '/',
  21. proxyTable: {},
  22. // host: '192.168.3.197', 原测试IP
  23. host: ip, // can be overwritten by process.env.HOST
  24. port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
  25. autoOpenBrowser: true,
  26. errorOverlay: true,
  27. notifyOnErrors: true,
  28. poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
  29. ....
  30. }
  31. }