/
server.ts
executable file
·60 lines (55 loc) · 1.89 KB
/
server.ts
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
import startWebpackDevServer from './libs/webpack/webpackDevServer';
import webpackDllCompiler, { webpackDllPluginsCompiler } from './libs/webpack/webpackDllCompiler';
import EConfig from './libs/settings/EConfig';
import {displayAvailableIPs} from './libs/utils/ip';
import {warning} from './libs/utils/logs';
import {URL_PREFIX} from './libs/constants/constants';
const openBrowser = require('open');
// const config = require('./webpack.config');
// const {port} = config;
function autoOpenBrowser(open: boolean, ip: string, port: number, targetApp: string) {
const {name: projectName,devServer:{https}} = EConfig.getInstance();
if (open) {
if (!targetApp) {
warning(`忽略自动打开浏览器功能:`);
warning(`请提供指定需要app name作为相对路径`);
}
else {
openBrowser(`${https?'https':'http'}://${ip}:${port}/${URL_PREFIX}/${projectName}/${targetApp}`);
}
}
}
/**
* 程序入口点开始方法
*/
export default async function start(options?:any) {
try {
/**
* 按需创建编译webpack dll manifest文件
*/
await webpackDllCompiler();
await webpackDllPluginsCompiler();
/**
* 开启webpack dev server
*/
await startWebpackDevServer();
/**
* 获取配置文件
*/
const {open, server, apps, defaultPort} = EConfig.getInstance();
let ip = server;
if (server === `0.0.0.0`) {
/**
* 显示可选IP列表
*/
displayAvailableIPs();
/**
* 开启后判断配置文件是否需要自动打开浏览器
*/
ip = `localhost`;
}
autoOpenBrowser(open, ip, defaultPort, apps[0]);
} catch (e) {
console.error(e);
}
}