UNPKG

1.09 kBJavaScriptView Raw
1// require('./init-node-env')()
2
3/**
4 * 获取 Web 服务器端口号
5 * @param {Object|number} port
6 * @param {string} [env=process.env.WEBPACK_BUILD_ENV]
7 * @returns {number}
8 */
9module.exports = (port, env = process.env.WEBPACK_BUILD_ENV) => {
10 // console.log({
11 // ENV: process.env.WEBPACK_BUILD_ENV,
12 // SERVER_PORT: process.env.SERVER_PORT,
13 // __SERVER_PORT__: __SERVER_PORT__
14 // });
15 const defaultPort =
16 process.env.WEBPACK_BUILD_ENV === 'prod' &&
17 typeof process.env.SERVER_PORT !== 'undefined'
18 ? process.env.SERVER_PORT
19 : /* typeof process.env.SERVER_PORT === 'undefined' && */ typeof __SERVER_PORT__ !==
20 'undefined'
21 ? __SERVER_PORT__
22 : process.env.SERVER_PORT;
23
24 if (typeof port === 'object') {
25 if (!env) env = 'prod';
26 if (typeof port[env] !== 'undefined') return port[env];
27 return defaultPort;
28 }
29
30 if (typeof port !== 'undefined' && typeof port !== 'boolean') return port;
31
32 return defaultPort;
33};