/
dev.js
193 lines (177 loc) · 5.33 KB
/
dev.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import fs from 'fs';
import openBrowser from 'react-dev-utils/openBrowser';
import webpack from 'webpack';
import assert from 'assert';
import WebpackDevServer from 'webpack-dev-server';
import chalk from 'chalk';
import { isPlainObject } from 'lodash';
import prepareUrls from './prepareUrls';
import clearConsole from './clearConsole';
import errorOverlayMiddleware from './errorOverlayMiddleware';
import send, { STARTING, DONE, ERROR } from './send';
import getPort from './getPort';
const isInteractive = process.stdout.isTTY;
const HOST = process.env.HOST || '0.0.0.0';
const PROTOCOL = process.env.HTTPS ? 'https' : 'http';
const CERT = process.env.HTTPS && process.env.CERT ? fs.readFileSync(process.env.CERT) : '';
const KEY = process.env.HTTPS && process.env.KEY ? fs.readFileSync(process.env.KEY) : '';
const noop = () => {};
process.env.NODE_ENV = 'development';
function getWebpackConfig(webpackConfig) {
return Array.isArray(webpackConfig) ? webpackConfig[0] : webpackConfig;
}
export default function dev({
webpackConfig,
_beforeServerWithApp,
beforeMiddlewares,
afterMiddlewares,
beforeServer,
afterServer,
contentBase,
onCompileDone = noop,
onFail = noop,
proxy,
port,
history,
base,
serverConfig: serverConfigFromOpts = {},
}) {
assert(webpackConfig, 'webpackConfig should be supplied.');
assert(
isPlainObject(webpackConfig) || Array.isArray(webpackConfig),
'webpackConfig should be plain object or array.',
);
getPort(port)
.then(port => {
if (port === null) {
return;
}
// Send message to parent process when port choosed
process.send({ type: 'UPDATE_PORT', port });
const compiler = webpack(webpackConfig);
let server = null;
let isFirstCompile = true;
const IS_CI = !!process.env.CI;
const SILENT = !!process.env.SILENT;
const urls = prepareUrls(PROTOCOL, HOST, port, base, history);
compiler.hooks.done.tap('af-webpack done', stats => {
if (stats.hasErrors()) {
// make sound
// ref: https://github.com/JannesMeyer/system-bell-webpack-plugin/blob/bb35caf/SystemBellPlugin.js#L14
if (process.env.SYSTEM_BELL !== 'none') {
process.stdout.write('\x07');
}
send({
type: ERROR,
});
onFail({ stats });
return;
}
let copied = '';
if (isFirstCompile && !IS_CI && !SILENT) {
try {
require('clipboardy').writeSync(urls.localUrlForBrowser);
copied = chalk.dim('(copied to clipboard)');
} catch (e) {
copied = chalk.red(`(copy to clipboard failed)`);
}
console.log();
console.log(
[
` App running at:`,
` - Local: ${chalk.cyan(urls.localUrlForTerminal)} ${copied}`,
` - Network: ${chalk.cyan(urls.lanUrlForTerminal)}`,
].join('\n'),
);
console.log();
}
onCompileDone({
port,
isFirstCompile,
stats,
server,
});
if (isFirstCompile) {
isFirstCompile = false;
openBrowser(urls.localUrlForBrowser);
send({
type: DONE,
urls: {
local: urls.localUrlForTerminal,
lan: urls.lanUrlForTerminal,
rawLocal: urls.localUrlForBrowser,
rawLanUrl: urls.rawLanUrl,
},
});
}
});
const serverConfig = {
disableHostCheck: true,
compress: true,
clientLogLevel: 'none',
hot: true,
quiet: true,
headers: {
'access-control-allow-origin': '*',
},
publicPath: getWebpackConfig(webpackConfig).output.publicPath,
watchOptions: {
ignored: /node_modules/,
},
historyApiFallback: false,
overlay: false,
host: HOST,
sockPort: port,
proxy,
https: !!process.env.HTTPS,
cert: CERT,
key: KEY,
contentBase: contentBase || process.env.CONTENT_BASE,
before(app) {
(beforeMiddlewares || []).forEach(middleware => {
app.use(middleware);
});
// internal usage for proxy
if (_beforeServerWithApp) {
_beforeServerWithApp(app);
}
app.use(errorOverlayMiddleware());
},
after(app) {
(afterMiddlewares || []).forEach(middleware => {
app.use(middleware);
});
},
...serverConfigFromOpts,
...(getWebpackConfig(webpackConfig).devServer || {}),
};
server = new WebpackDevServer(compiler, serverConfig);
['SIGINT', 'SIGTERM'].forEach(signal => {
process.on(signal, () => {
server.close(() => {
process.exit(0);
});
});
});
if (beforeServer) {
beforeServer(server);
}
server.listen(port, HOST, err => {
if (err) {
console.log(err);
return;
}
if (isInteractive) {
clearConsole();
}
console.log(chalk.cyan('Starting the development server...\n'));
send({ type: STARTING });
if (afterServer) {
afterServer(server, port);
}
});
})
.catch(err => {
console.log(err);
});
}