UNPKG

2.59 kBJavaScriptView Raw
1'use strict';
2
3/* eslint-disable
4 no-shadow,
5 global-require,
6 multiline-ternary,
7 array-bracket-spacing,
8 space-before-function-paren
9*/
10const open = require('opn');
11
12const colors = {
13 info(useColor, msg) {
14 if (useColor) {
15 // Make text blue and bold, so it *pops*
16 return `\u001b[1m\u001b[34m${msg}\u001b[39m\u001b[22m`;
17 }
18
19 return msg;
20 },
21 error(useColor, msg) {
22 if (useColor) {
23 // Make text red and bold, so it *pops*
24 return `\u001b[1m\u001b[31m${msg}\u001b[39m\u001b[22m`;
25 }
26
27 return msg;
28 },
29};
30
31// eslint-disable-next-line
32const defaultTo = (value, def) => {
33 return value == null ? def : value;
34};
35
36function version() {
37 return (
38 `webpack-dev-server ${require('../package.json').version}\n` +
39 `webpack ${require('webpack/package.json').version}`
40 );
41}
42
43function status(uri, options, log, useColor) {
44 const contentBase = Array.isArray(options.contentBase)
45 ? options.contentBase.join(', ')
46 : options.contentBase;
47
48 if (options.socket) {
49 log.info(`Listening to socket at ${colors.info(useColor, options.socket)}`);
50 } else {
51 log.info(`Project is running at ${colors.info(useColor, uri)}`);
52 }
53
54 log.info(
55 `webpack output is served from ${colors.info(useColor, options.publicPath)}`
56 );
57
58 if (contentBase) {
59 log.info(
60 `Content not from webpack is served from ${colors.info(
61 useColor,
62 contentBase
63 )}`
64 );
65 }
66
67 if (options.historyApiFallback) {
68 log.info(
69 `404s will fallback to ${colors.info(
70 useColor,
71 options.historyApiFallback.index || '/index.html'
72 )}`
73 );
74 }
75
76 if (options.bonjour) {
77 log.info(
78 'Broadcasting "http" with subtype of "webpack" via ZeroConf DNS (Bonjour)'
79 );
80 }
81
82 if (options.open) {
83 let openOptions = {};
84 let openMessage = 'Unable to open browser';
85
86 if (typeof options.open === 'string') {
87 openOptions = { app: options.open };
88 openMessage += `: ${options.open}`;
89 }
90
91 open(uri + (options.openPage || ''), openOptions).catch(() => {
92 log.warn(
93 `${openMessage}. If you are running in a headless environment, please do not use the --open flag`
94 );
95 });
96 }
97}
98
99function bonjour(options) {
100 const bonjour = require('bonjour')();
101
102 bonjour.publish({
103 name: 'Webpack Dev Server',
104 port: options.port,
105 type: 'http',
106 subtypes: ['webpack'],
107 });
108
109 process.on('exit', () => {
110 bonjour.unpublishAll(() => {
111 bonjour.destroy();
112 });
113 });
114}
115
116module.exports = {
117 status,
118 colors,
119 version,
120 bonjour,
121 defaultTo,
122};