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
|
const updateNotifier = require('update-notifier');
const webpack = require('webpack');
const weblog = require('webpack-log');
const eventbus = require('./lib/bus');
const getOptions = require('./lib/options');
const getServer = require('./lib/server');
const WebpackServeError = require('./lib/WebpackServeError');
const pkg = require('./package.json');
module.exports = (opts) => {
updateNotifier({ pkg }).notify();
process.env.WEBPACK_SERVE = true;
return getOptions(opts)
.then((results) => {
const { options, configs } = results;
const log = weblog({ name: 'serve', id: 'webpack-serve' });
options.bus = eventbus(options);
const { bus } = options;
if (!options.compiler) {
try {
options.compiler = webpack(configs.length > 1 ? configs : configs[0]);
} catch (e) {
throw new WebpackServeError(
`An error was thrown while initializing Webpack\n ${e.toString()}`
);
}
}
const compilers = options.compiler.compilers || [options.compiler];
for (const comp of compilers) {
comp.hooks.compile.tap('WebpackServe', () => {
bus.emit('build-started', { compiler: comp });
});
comp.hooks.done.tap('WebpackServe', (stats) => {
const json = stats.toJson();
if (stats.hasErrors()) {
bus.emit('compiler-error', { json, compiler: comp });
}
if (stats.hasWarnings()) {
bus.emit('compiler-warning', { json, compiler: comp });
}
bus.emit('build-finished', { stats, compiler: comp });
});
}
const { close, server, start } = getServer(options);
start(options);
let closing = false;
const exit = (signal) => {
if (!closing) {
closing = true;
close(() => {
log.info(`Process Ended via ${signal}`);
server.kill();
process.exit(0);
});
}
};
for (const signal of ['SIGINT', 'SIGTERM']) {
process.on(signal, () => exit(signal));
}
process.on('exit', exit);
return Object.freeze({
close,
compiler: options.compiler,
on(...args) {
options.bus.on(...args);
},
options,
});
})
.catch((err) => {
// eslint-disable-next-line no-console
console.log('An error was thrown while starting webpack-serve.\n ', err);
throw err;
});
};
|