1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const open = require('opn');
|
11 |
|
12 | const colors = {
|
13 | info(useColor, msg) {
|
14 | if (useColor) {
|
15 |
|
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 |
|
24 | return `\u001b[1m\u001b[31m${msg}\u001b[39m\u001b[22m`;
|
25 | }
|
26 |
|
27 | return msg;
|
28 | },
|
29 | };
|
30 |
|
31 |
|
32 | const defaultTo = (value, def) => {
|
33 | return value == null ? def : value;
|
34 | };
|
35 |
|
36 | function version() {
|
37 | return (
|
38 | `webpack-dev-server ${require('../package.json').version}\n` +
|
39 | `webpack ${require('webpack/package.json').version}`
|
40 | );
|
41 | }
|
42 |
|
43 | function 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 |
|
99 | function 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 |
|
116 | module.exports = {
|
117 | status,
|
118 | colors,
|
119 | version,
|
120 | bonjour,
|
121 | defaultTo,
|
122 | };
|