Skip to content

Commit 6c5e5ec

Browse files
author
Guillaume Chau
committed
Fix automatic ip detection
1 parent 0f287c4 commit 6c5e5ec

File tree

1 file changed

+36
-7
lines changed
  • packages/vue-component-dev-server/server

1 file changed

+36
-7
lines changed
Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
1-
import localIp from 'local-ipv4-address'
1+
import os from 'os'
22

33
function getMeteorPort() {
4-
const reg = /:\/\/.+:(\d+)/gi;
5-
const result = reg.exec(Meteor.absoluteUrl());
4+
const reg = /:\/\/.+:(\d+)/gi
5+
const result = reg.exec(Meteor.absoluteUrl())
66
if(result && result.length >= 2) {
7-
return parseInt(result[1]) + 3;
7+
return parseInt(result[1]) + 3
88
}
99
}
1010

11+
function getLocalIp () {
12+
const ifaces = os.networkInterfaces()
13+
14+
let ip
15+
for (const key of Object.keys(ifaces)) {
16+
const interfaces = ifaces[key]
17+
for (const iface of interfaces) {
18+
if ('IPv4' !== iface.family || iface.internal !== false) {
19+
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
20+
} else {
21+
ip = iface.address
22+
break
23+
}
24+
}
25+
if (ip) {
26+
break
27+
}
28+
}
29+
30+
if (!ip) {
31+
console.warn(`[HMR] No local IP detected. If you want to connect from a remote device, set the local IP with the 'HMR_URL' env. variable.`)
32+
ip = '127.0.0.1'
33+
} else {
34+
// console.warn(`[HMR] Local IP detected: '${ip}'. If you have issues connecting from a remote device, set the local IP with the 'HMR_URL' env. variable.`)
35+
}
36+
37+
return ip
38+
}
39+
1140
const localIpSync = Meteor.wrapAsync(cb => {
1241
localIp().then(result => {
1342
cb(null, result)
@@ -17,11 +46,11 @@ const localIpSync = Meteor.wrapAsync(cb => {
1746
})
1847

1948
// to define only dev port with same url
20-
const PORT = parseInt(process.env.HMR_PORT) || parseInt(process.env.VUE_DEV_SERVER_PORT) || getMeteorPort() || 3003;
49+
const PORT = parseInt(process.env.HMR_PORT) || parseInt(process.env.VUE_DEV_SERVER_PORT) || getMeteorPort() || 3003
2150

2251
// to define full url with port (example: https://dev.example.com:8443) or only domain
23-
const DEVURL = process.env.HMR_URL || process.env.VUE_DEV_SERVER_URL || localIpSync();
52+
const DEVURL = process.env.HMR_URL || process.env.VUE_DEV_SERVER_URL || getLocalIp()
2453

2554

2655
// Client-side config
27-
__meteor_runtime_config__.VUE_DEV_SERVER_URL = DEVURL.indexOf(':') === -1 ? `${DEVURL}:${PORT}` : DEVURL;
56+
__meteor_runtime_config__.VUE_DEV_SERVER_URL = `${DEVURL}:${PORT}`

0 commit comments

Comments
 (0)