-
-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathwebpackDevServerAddress.js
More file actions
65 lines (55 loc) · 1.87 KB
/
Copy pathwebpackDevServerAddress.js
File metadata and controls
65 lines (55 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* eslint-env node */
const fs = require('node:fs');
const process = require('node:process');
const { execSync } = require('node:child_process');
// The advertised address cannot be derived from the bind address: a published port is allocated,
// and a wildcard bind host is not browser-fetchable. Advertise an unreachable one and the page
// returns 200 with no JavaScript. Mirrored in settings.py; dependency-free so its test needs no
// node_modules.
const DEFAULT_WEBPACK_DEV_HOST = '127.0.0.1';
const DEFAULT_WEBPACK_DEV_PORT = 4000;
const WILDCARD_HOSTS = ['0.0.0.0', '::', '[::]'];
function isWSL() {
try {
const version = fs.readFileSync('/proc/version', 'utf8');
return version.toLowerCase().includes('microsoft');
} catch (err) {
return false;
}
}
function getWebpackDevHost() {
if (process.env.WEBPACK_DEV_HOST) {
return process.env.WEBPACK_DEV_HOST;
}
if (!isWSL()) {
return DEFAULT_WEBPACK_DEV_HOST;
}
try {
return execSync('hostname -I').toString().trim().split(' ')[0];
} catch (err) {
console.warn('Failed to get WSL IP address:', err);
return DEFAULT_WEBPACK_DEV_HOST;
}
}
function getWebpackDevPort() {
const port = parseInt(process.env.WEBPACK_DEV_PORT, 10);
return Number.isNaN(port) ? DEFAULT_WEBPACK_DEV_PORT : port;
}
function getWebpackDevPublicPort() {
const port = parseInt(process.env.WEBPACK_DEV_PUBLIC_PORT, 10);
return Number.isNaN(port) ? getWebpackDevPort() : port;
}
function getWebpackDevPublicHost() {
if (process.env.WEBPACK_DEV_PUBLIC_HOST) {
return process.env.WEBPACK_DEV_PUBLIC_HOST;
}
const devHost = getWebpackDevHost();
// Loopback is the best a build-time value can do with a wildcard.
return WILDCARD_HOSTS.includes(devHost) ? DEFAULT_WEBPACK_DEV_HOST : devHost;
}
module.exports = {
getWebpackDevHost,
getWebpackDevPort,
getWebpackDevPublicHost,
getWebpackDevPublicPort,
};