-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathindex.ts
More file actions
123 lines (106 loc) · 3.7 KB
/
Copy pathindex.ts
File metadata and controls
123 lines (106 loc) · 3.7 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import type { IPluginAPI } from '@fesjs/shared';
import type { WebpackBuildConfig } from '../../../shared';
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { removeSync } from 'fs-extra/esm';
import getFolderSize from 'get-folder-size';
import pc from 'picocolors';
import { cleanTmpPathExceptCache, getBundleAndConfigs } from '../../common/buildDevUtils';
import connectHistoryMiddleware from './connectHistoryMiddleware';
import { startDevServer } from './devServer';
async function handleCacheClean(cwd: string) {
return new Promise((resolve) => {
const cachePath = path.join(cwd, '.cache/webpack');
if (!fs.existsSync(cachePath)) {
return resolve(0);
}
// 大于 5G 清除缓存,修复 webpack 缓存无限增长问题
// https://github.com/webpack/webpack/issues/13291
getFolderSize.loose(cachePath).then((size) => {
if (size > 5 * 1024 * 1024 * 1024) {
removeSync(cachePath);
}
resolve(size);
});
});
}
export default (api: IPluginAPI<WebpackBuildConfig>) => {
const {
paths,
utils: { getPort, getHostName, changePort, logger },
} = api;
let port: number;
let hostname: string;
let server: any;
async function destroy() {
await server?.stop();
}
api.registerCommand({
command: 'dev',
description: 'start a local http service for development',
options: [
{
name: '--port',
description: 'http service port, like 8000',
},
{
name: '--https',
description: 'whether to turn on the https service',
},
],
async fn({ args = {} }) {
await handleCacheClean(api.paths.cwd);
port = await getPort(process.env.PORT || args.port || api.config.devServer?.port);
changePort(port);
hostname = getHostName(api.config.devServer?.host);
// enable https
const isHTTPS = process.env.HTTPS || args.https || api.config.devServer?.https;
cleanTmpPathExceptCache({
absTmpPath: paths.absTmpPath,
});
await api.applyPlugins({
key: 'onGenerateFiles',
type: api.ApplyPluginsType.event,
});
await api.startWatch();
// dev
const { bundleConfig } = await getBundleAndConfigs({ api });
const beforeMiddlewares = await api.applyPlugins({
key: 'addBeforeMiddlewares',
type: api.ApplyPluginsType.add,
initialValue: [],
args: {},
});
const middlewares = await api.applyPlugins({
key: 'addMiddlewares',
type: api.ApplyPluginsType.add,
initialValue: [],
args: {},
});
server = startDevServer({
webpackConfig: bundleConfig,
host: hostname,
port,
proxy: api.config.proxy,
https: isHTTPS,
beforeMiddlewares: [connectHistoryMiddleware(api), ...beforeMiddlewares],
afterMiddlewares: [...middlewares],
customerDevServerConfig: api.config.devServer,
});
return {
destroy,
};
},
});
api.registerMethod({
name: 'restartServer',
fn() {
logger.info(pc.gray('Try to restart dev server...'));
destroy();
process.send?.({
type: 'RESTART',
});
},
});
};