场景描述
为了开发方便,我使用mac笔记本在公司开发,所以我是通过链接wifi来获取白名单IP来访问公司gitlab的地址
但是不便的是WIFI提供的ip是不断变化化的,比如上午,下午晚上,吃个饭的功夫,再打开电脑ip就不一样了,本来ip变了也不用太在意,但是项目的历史债务问题,通过修改package.json和webpack的config文件来修改开发分支的IP
优化前
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js --host 192.168.3.197",
host: '192.168.3.197',
优化后
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
'use strict'
const path = require('path')
function getIPAdress() {
var interfaces = require('os').networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
return alias.address;
}
}
}
}
var ip = getIPAdress();
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
// host: '192.168.3.197', 原测试IP
host: ip, // can be overwritten by process.env.HOST
port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: true,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
....
}
}