-
-
Notifications
You must be signed in to change notification settings - Fork 591
/
index.ts
125 lines (103 loc) · 4.73 KB
/
index.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
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
import startDevServer from './startDevServer';
class ServeCommand {
async apply(cli): Promise<void> {
const { logger } = cli;
await cli.makeCommand(
{
name: 'serve',
alias: 's',
description: 'Run the webpack dev server.',
usage: '[options]',
pkg: '@webpack-cli/serve',
dependencies: ['webpack-dev-server'],
},
() => {
let devServerFlags = [];
try {
// eslint-disable-next-line
devServerFlags = require('webpack-dev-server/bin/cli-flags').devServer;
} catch (error) {
logger.error(`You need to install 'webpack-dev-server' for running 'webpack serve'.\n${error}`);
process.exit(2);
}
const builtInOptions = cli.getBuiltInOptions();
return [...builtInOptions, ...devServerFlags];
},
async (options) => {
const builtInOptions = cli.getBuiltInOptions();
let devServerFlags = [];
try {
// eslint-disable-next-line
devServerFlags = require('webpack-dev-server/bin/cli-flags').devServer;
} catch (error) {
// Nothing, to prevent future updates
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const webpackOptions: Record<string, any> = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const devServerOptions: Record<string, any> = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const processors: Array<(opts: Record<string, any>) => void> = [];
for (const optionName in options) {
const kebabedOption = cli.utils.toKebabCase(optionName);
// `webpack-dev-server` has own logic for the `--hot` option
const isBuiltInOption =
kebabedOption !== 'hot' && builtInOptions.find((builtInOption) => builtInOption.name === kebabedOption);
if (isBuiltInOption) {
webpackOptions[optionName] = options[optionName];
} else {
const needToProcess = devServerFlags.find(
(devServerOption) => devServerOption.name === kebabedOption && devServerOption.processor,
);
if (needToProcess) {
processors.push(needToProcess.processor);
}
devServerOptions[optionName] = options[optionName];
}
}
for (const processor of processors) {
processor(devServerOptions);
}
webpackOptions.argv = { ...options, env: { WEBPACK_SERVE: true, ...options.env } };
const compiler = await cli.createCompiler(webpackOptions);
if (!compiler) {
return;
}
let servers;
if (cli.needWatchStdin(compiler) || devServerOptions.stdin) {
// TODO remove in the next major release
// Compatibility with old `stdin` option for `webpack-dev-server`
// Should be removed for the next major release on both sides
if (devServerOptions.stdin) {
delete devServerOptions.stdin;
}
process.stdin.on('end', () => {
Promise.all(
servers.map((server) => {
return new Promise<void>((resolve) => {
server.close(() => {
resolve();
});
});
}),
).then(() => {
process.exit(0);
});
});
process.stdin.resume();
}
try {
servers = await startDevServer(compiler, devServerOptions, options, logger);
} catch (error) {
if (error.name === 'ValidationError') {
logger.error(error.message);
} else {
logger.error(error);
}
process.exit(2);
}
},
);
}
}
export default ServeCommand;