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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const { join } = require('path');
const { writeFileSync, mkdirSync, existsSync } = require('fs');
const rimraf = require('rimraf');
const serveStatic = require('serve-static');
const { resolve } = require('path');
const { portfinder, chalk, delay, createDebug, chokidar, signale, glob } = require('@umijs/utils');
const argv = require('./argv');
const setup = require('./middlewares/frontendMiddleware');
const pkg = require('../package.json');
const config = require('../config/config');
const mockMiddewares = require('./mock');
const emitter = require('./event');
const Server = require('./server');
const debug = createDebug('preset-build-in:proxy:createMiddleware');
// const proxyConfig = require('../config/proxy');
const loadDotEnv = require('./utils/loadDotEnv');
const { getSchema } = require('./openapi');
const cleanRequireCache = require('./utils/cleanRequireCache');
(async () => {
const defaultPort = process.env.PORT || argv.port || (config && config.devServer && config.devServer.port);
const port = await portfinder.getPortPromise({
port: defaultPort ? parseInt(String(defaultPort), 10) : 8080,
});
const homename = process.env.HOST || (config && config.devServer && config.devServer.host) || '127.0.0.1';
console.log(chalk.cyan('Starting the development server...'));
// process.send && process.send({ type: 'UPDATE_PORT', port });
const isHTTPS = process.env.HTTPS || (argv && argv.https);
await delay(500);
const absNodeModulesPath = `${process.cwd()}/node_modules`;
const openAPIFilesPath = join(absNodeModulesPath, 'civ_open_api');
const loadEnv = () => {
const basePath = join(process.cwd(), '.env');
const localPath = `${basePath}.local`;
loadDotEnv(basePath);
loadDotEnv(localPath);
};
loadEnv();
// const absNodeModulesPath = cwd + '/node_modules';
try {
if (existsSync(openAPIFilesPath)) {
rimraf.sync(openAPIFilesPath);
}
mkdirSync(join(openAPIFilesPath));
} catch (error) {
console.error(error);
}
const server = new Server({
compress: true,
https: !!isHTTPS,
headers: {
'access-control-allow-origin': '*',
},
port,
beforeMiddlewares: [
(config.mock || process.env.MOCK !== 'none') && mockMiddewares,
],
afterMiddlewares: [serveStatic(openAPIFilesPath)],
proxy: config.proxy,
...(config.devServer || {}),
});
setup(
server.app,
{
outputPath: resolve(process.cwd(), pkg.name.toLocaleLowerCase()),
publicPath: `/${pkg.name.toLocaleLowerCase()}`,
},
config,
{
port,
homename,
},
);
// proxy config 热更新
const ignore = [
// ignore mock files under node_modules
'node_modules/**'
];
const cwd = process.cwd();
const proxyWatcherPaths = [
...(glob.sync('config/proxy.js', {
cwd,
ignore,
}) || []),
...(glob.sync('**/proxy.js', {
cwd,
ignore,
}) || []),
...(glob.sync('.env', {
cwd,
ignore,
}) || []),
...(glob.sync('.env.local', {
cwd,
ignore,
}) || [])
];
const watcher = chokidar.watch(proxyWatcherPaths, {
ignoreInitial: true,
});
const errors = [];
watcher.on('ready', () => debug('Initial scan complete. Ready for changes')).on('all', async (event, file) => {
debug(`[${event}] ${file}, reload proxy config`);``
errors.splice(0, errors.length);
cleanRequireCache(Array.from(new Set(proxyWatcherPaths)));
if (!errors.length) {
const hotProxy = require('../config/proxy');
server.setupProxy && server.setupProxy(hotProxy[process.env.NODE_ENV], true);
signale.success(`Proxy config parse success`);
}
});
process.once('SIGINT', async () => {
await watcher.close();
});
emitter.on('onDevCompileDone', async () => {
try {
const openAPIConfig = config.openAPI;
if (openAPIConfig && openAPIConfig.schemaPath) {
const openAPIJson = await getSchema(openAPIConfig.schemaPath);
writeFileSync(
join(openAPIFilesPath, 'civ-plugins_openapi.json'),
JSON.stringify(openAPIJson, null, 2),
);
}
} catch (error) {
console.error(error);
}
});
await server.listen({
port,
homename,
});
})();