dev-info.js
854 Bytes
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
const fs = require('fs');
const path = require('path');
const url = require('url');
let info = {
host: '127.0.0.1',
port: 5001,
publicPath: 'http://127.0.0.1:5001/'
};
try {
let dev = JSON.parse(fs.readFileSync(path.join(__dirname, '../../.devhost')));
if (dev.publicPath) {
let urlObj = url.parse(dev.publicPath);
info = {
host: urlObj.hostname,
port: urlObj.port,
publicPath: dev.publicPath
};
} else {
info.host = dev.host;
info.publicPath = `http://${info.host}:${info.port}/`;
}
} catch (e) {
if (!process.env.NODE_ENV) {
console.error(e);
console.warn(`
推荐在项目主目录建一个 .devhost 文件,内容为
{
"host": "127.0.0.1"
}
`);
}
}
module.exports = info;