-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstart.ts
87 lines (79 loc) · 2.59 KB
/
start.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
import process from 'process';
import webpack from 'webpack';
import webpackDevServer from 'webpack-dev-server';
import portfinder from 'portfinder';
import chalk from 'chalk';
import ora from 'ora';
import config from '../config/webpack.dev';
import { clearConsole, openBrowser, formatMessage } from './util';
const isInteractive = process.stdout.isTTY;
let spinner = new ora();
spinner.start('start server\n');
let serverConfig: webpackDevServer.Configuration = {
clientLogLevel: 'warning',
compress: true,
historyApiFallback: true,
inline: true,
open: true,
overlay: {
warnings: true,
errors: true,
},
hot: true,
port: 8080,
host: 'localhost',
quiet: true,
...config.devServer,
};
webpackDevServer.addDevServerEntrypoints(config, serverConfig);
portfinder
.getPortPromise({ port: serverConfig.port, host: serverConfig.host })
.then((port: number) => {
let url = `http://${serverConfig.host}:${port}`;
let compiler = webpack(config);
// "invalid" event fires when you have changed a file, and Webpack is
// recompiling a bundle. WebpackDevServer takes care to pause serving the
// bundle, so if you refresh, it'll wait instead of serving the old one.
// "invalid" is short for "bundle invalidated", it doesn't imply any errors.
compiler.hooks.invalid.tap('invalid', () => {
if (isInteractive) {
clearConsole();
}
console.log('Compiling...');
});
// "done" event fires when Webpack has finished recompiling the bundle.
// Whether or not you have warnings or errors, you will get this event.
compiler.hooks.done.tap('done', (stats) => {
let message = formatMessage(stats);
if (stats.hasErrors()) {
console.log(chalk.red('compile error occur:\n'));
console.log(message + '\n\n');
return;
}
if (stats.hasWarnings()) {
console.log(chalk.red('compile warn occur:\n'));
console.log(message + '\n\n');
}
console.log(chalk.green(`application is running here:`, url));
});
compiler.hooks.run.tap('run', (compilation) => {
let message = formatMessage(compilation.getStats());
process.stdout.write(message + '\n\n');
});
let server = new webpackDevServer(compiler, serverConfig);
server.listen(port, serverConfig.host, (err) => {
spinner.stop();
if (err) {
process.stdout.write(JSON.stringify(err));
process.exit(1);
}
if (isInteractive) {
clearConsole();
}
openBrowser(url);
});
})
.catch((err) => {
process.stdout.write(JSON.stringify(err));
process.exit(1);
});