Skip to content

Commit 1896fcf

Browse files
refactor(bin): move options and helpers into separate files (#1470)
1 parent 2ee13ab commit 1896fcf

File tree

3 files changed

+518
-323
lines changed

3 files changed

+518
-323
lines changed

bin/options.js

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
'use strict';
2+
3+
/* eslint-disable
4+
global-require,
5+
multiline-ternary,
6+
space-before-function-paren
7+
*/
8+
const ADVANCED_GROUP = 'Advanced options:';
9+
const DISPLAY_GROUP = 'Stats options:';
10+
const SSL_GROUP = 'SSL options:';
11+
const CONNECTION_GROUP = 'Connection options:';
12+
const RESPONSE_GROUP = 'Response options:';
13+
const BASIC_GROUP = 'Basic options:';
14+
15+
const options = {
16+
bonjour: {
17+
type: 'boolean',
18+
describe: 'Broadcasts the server via ZeroConf networking on start'
19+
},
20+
lazy: {
21+
type: 'boolean',
22+
describe: 'Lazy'
23+
},
24+
inline: {
25+
type: 'boolean',
26+
default: true,
27+
describe: 'Inline mode (set to false to disable including client scripts like livereload)'
28+
},
29+
progress: {
30+
type: 'boolean',
31+
describe: 'Print compilation progress in percentage',
32+
group: BASIC_GROUP
33+
},
34+
'hot-only': {
35+
type: 'boolean',
36+
describe: 'Do not refresh page if HMR fails',
37+
group: ADVANCED_GROUP
38+
},
39+
stdin: {
40+
type: 'boolean',
41+
describe: 'close when stdin ends'
42+
},
43+
open: {
44+
type: 'string',
45+
describe: 'Open the default browser, or optionally specify a browser name'
46+
},
47+
useLocalIp: {
48+
type: 'boolean',
49+
describe: 'Open default browser with local IP'
50+
},
51+
'open-page': {
52+
type: 'string',
53+
describe: 'Open default browser with the specified page',
54+
requiresArg: true
55+
},
56+
color: {
57+
type: 'boolean',
58+
alias: 'colors',
59+
default: function supportsColor() {
60+
return require('supports-color');
61+
},
62+
group: DISPLAY_GROUP,
63+
describe: 'Enables/Disables colors on the console'
64+
},
65+
info: {
66+
type: 'boolean',
67+
group: DISPLAY_GROUP,
68+
default: true,
69+
describe: 'Info'
70+
},
71+
quiet: {
72+
type: 'boolean',
73+
group: DISPLAY_GROUP,
74+
describe: 'Quiet'
75+
},
76+
'client-log-level': {
77+
type: 'string',
78+
group: DISPLAY_GROUP,
79+
default: 'info',
80+
describe: 'Log level in the browser (info, warning, error or none)'
81+
},
82+
https: {
83+
type: 'boolean',
84+
group: SSL_GROUP,
85+
describe: 'HTTPS'
86+
},
87+
key: {
88+
type: 'string',
89+
describe: 'Path to a SSL key.',
90+
group: SSL_GROUP
91+
},
92+
cert: {
93+
type: 'string',
94+
describe: 'Path to a SSL certificate.',
95+
group: SSL_GROUP
96+
},
97+
cacert: {
98+
type: 'string',
99+
describe: 'Path to a SSL CA certificate.',
100+
group: SSL_GROUP
101+
},
102+
pfx: {
103+
type: 'string',
104+
describe: 'Path to a SSL pfx file.',
105+
group: SSL_GROUP
106+
},
107+
'pfx-passphrase': {
108+
type: 'string',
109+
describe: 'Passphrase for pfx file.',
110+
group: SSL_GROUP
111+
},
112+
'content-base': {
113+
type: 'string',
114+
describe: 'A directory or URL to serve HTML content from.',
115+
group: RESPONSE_GROUP
116+
},
117+
'watch-content-base': {
118+
type: 'boolean',
119+
describe: 'Enable live-reloading of the content-base.',
120+
group: RESPONSE_GROUP
121+
},
122+
'history-api-fallback': {
123+
type: 'boolean',
124+
describe: 'Fallback to /index.html for Single Page Applications.',
125+
group: RESPONSE_GROUP
126+
},
127+
compress: {
128+
type: 'boolean',
129+
describe: 'Enable gzip compression',
130+
group: RESPONSE_GROUP
131+
},
132+
port: {
133+
describe: 'The port',
134+
group: CONNECTION_GROUP
135+
},
136+
'disable-host-check': {
137+
type: 'boolean',
138+
describe: 'Will not check the host',
139+
group: CONNECTION_GROUP
140+
},
141+
socket: {
142+
type: 'String',
143+
describe: 'Socket to listen',
144+
group: CONNECTION_GROUP
145+
},
146+
public: {
147+
type: 'string',
148+
describe: 'The public hostname/ip address of the server',
149+
group: CONNECTION_GROUP
150+
},
151+
host: {
152+
type: 'string',
153+
default: 'localhost',
154+
describe: 'The hostname/ip address the server will bind to',
155+
group: CONNECTION_GROUP
156+
},
157+
'allowed-hosts': {
158+
type: 'string',
159+
describe: 'A comma-delimited string of hosts that are allowed to access the dev server',
160+
group: CONNECTION_GROUP
161+
}
162+
};
163+
164+
module.exports = options;

bin/utils.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
*/
10+
const open = require('opn');
11+
12+
const 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
32+
const defaultTo = (value, def) => {
33+
return value == null ? def : value;
34+
};
35+
36+
function version () {
37+
return `webpack-dev-server ${require('../package.json').version}\n` +
38+
`webpack ${require('webpack/package.json').version}`;
39+
}
40+
41+
function status (uri, options, log, useColor) {
42+
const contentBase = Array.isArray(options.contentBase)
43+
? options.contentBase.join(', ')
44+
: options.contentBase;
45+
46+
if (options.socket) {
47+
log.info(`Listening to socket at ${colors.info(useColor, options.socket)}`);
48+
} else {
49+
log.info(`Project is running at ${colors.info(useColor, uri)}`);
50+
}
51+
52+
log.info(
53+
`webpack output is served from ${colors.info(useColor, options.publicPath)}`
54+
);
55+
56+
if (contentBase) {
57+
log.info(
58+
`Content not from webpack is served from ${colors.info(useColor, contentBase)}`
59+
);
60+
}
61+
62+
if (options.historyApiFallback) {
63+
log.info(
64+
`404s will fallback to ${colors.info(useColor, options.historyApiFallback.index || '/index.html')}`
65+
);
66+
}
67+
68+
if (options.bonjour) {
69+
log.info(
70+
'Broadcasting "http" with subtype of "webpack" via ZeroConf DNS (Bonjour)'
71+
);
72+
}
73+
74+
if (options.open) {
75+
let openOptions = {};
76+
let openMessage = 'Unable to open browser';
77+
78+
if (typeof options.open === 'string') {
79+
openOptions = { app: options.open };
80+
openMessage += `: ${options.open}`;
81+
}
82+
83+
open(uri + (options.openPage || ''), openOptions).catch(() => {
84+
log.warn(
85+
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
86+
);
87+
});
88+
}
89+
}
90+
91+
function bonjour (options) {
92+
const bonjour = require('bonjour')();
93+
94+
bonjour.publish({
95+
name: 'Webpack Dev Server',
96+
port: options.port,
97+
type: 'http',
98+
subtypes: [ 'webpack' ]
99+
});
100+
101+
process.on('exit', () => {
102+
bonjour.unpublishAll(() => {
103+
bonjour.destroy();
104+
});
105+
});
106+
}
107+
108+
module.exports = {
109+
status,
110+
colors,
111+
version,
112+
bonjour,
113+
defaultTo
114+
};

0 commit comments

Comments
 (0)