/
webpack-reporter.js
45 lines (37 loc) · 1.46 KB
/
webpack-reporter.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
module.exports = function reportReadiness(options) {
const uri = createDomain(options);
const yargs = require('yargs');
const argv = yargs.argv;
argv.color = require('supports-color');
const useColor = argv.color;
let startSentence = `Project is running at ${colorInfo(useColor, uri)}`;
if (options.socket) {
startSentence = `Listening to socket at ${colorInfo(useColor, options.socket)}`;
}
console.log((argv.progress ? '\n' : '') + startSentence);
console.log(`webpack output is served from ${colorInfo(useColor, options.publicPath)}`);
const contentBase = Array.isArray(options.contentBase) ? options.contentBase.join(', ') : options.contentBase;
if (contentBase) {
console.log(`Content not from webpack is served from ${colorInfo(useColor, contentBase)}`);
}
if (options.historyApiFallback) {
console.log(`404s will fallback to ${colorInfo(useColor, options.historyApiFallback.index || '/index.html')}`);
}
};
function createDomain(opts) {
const protocol = opts.https ? 'https' : 'http';
const url = require('url');
// the formatted domain (url without path) of the webpack server
return opts.public ? `${protocol}://${opts.public}` : url.format({
protocol: protocol,
hostname: opts.host,
port: opts.socket ? 0 : opts.port.toString()
});
}
function colorInfo(useColor, msg) {
if (useColor) {
// Make text blue and bold, so it *pops*
return `\u001b[1m\u001b[33m${msg}\u001b[39m\u001b[22m`;
}
return msg;
}