webpack-dev-server 3.1.14 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,46 +4,36 @@
4
4
 
5
5
  /* eslint-disable
6
6
  import/order,
7
- import/no-extraneous-dependencies,
8
- global-require,
9
7
  no-shadow,
10
- no-console,
11
- multiline-ternary,
12
- arrow-parens,
13
- array-bracket-spacing,
14
- space-before-function-paren
8
+ no-console
15
9
  */
16
10
  const debug = require('debug')('webpack-dev-server');
17
11
 
18
12
  const fs = require('fs');
19
13
  const net = require('net');
20
- const path = require('path');
21
14
 
22
- const portfinder = require('portfinder');
23
15
  const importLocal = require('import-local');
24
16
 
25
17
  const yargs = require('yargs');
26
18
  const webpack = require('webpack');
27
19
 
28
20
  const options = require('./options');
29
-
30
- const {
31
- colors,
32
- status,
33
- version,
34
- bonjour,
35
- defaultTo
36
- } = require('./utils');
37
-
38
21
  const Server = require('../lib/Server');
39
22
 
40
- const addEntries = require('../lib/utils/addEntries');
23
+ const colors = require('../lib/utils/colors');
24
+ const createConfig = require('../lib/utils/createConfig');
41
25
  const createDomain = require('../lib/utils/createDomain');
42
26
  const createLogger = require('../lib/utils/createLogger');
27
+ const defaultTo = require('../lib/utils/defaultTo');
28
+ const findPort = require('../lib/utils/findPort');
29
+ const getVersions = require('../lib/utils/getVersions');
30
+ const runBonjour = require('../lib/utils/runBonjour');
31
+ const status = require('../lib/utils/status');
32
+ const tryParseInt = require('../lib/utils/tryParseInt');
43
33
 
44
34
  let server;
45
35
 
46
- const signals = [ 'SIGINT', 'SIGTERM' ];
36
+ const signals = ['SIGINT', 'SIGTERM'];
47
37
 
48
38
  signals.forEach((signal) => {
49
39
  process.on(signal, () => {
@@ -70,7 +60,9 @@ try {
70
60
  require.resolve('webpack-cli');
71
61
  } catch (err) {
72
62
  console.error('The CLI moved into a separate package: webpack-cli');
73
- console.error('Please install \'webpack-cli\' in addition to webpack itself to use the CLI');
63
+ console.error(
64
+ "Please install 'webpack-cli' in addition to webpack itself to use the CLI"
65
+ );
74
66
  console.error('-> When using npm: npm i -D webpack-cli');
75
67
  console.error('-> When using yarn: yarn add -D webpack-cli');
76
68
 
@@ -78,26 +70,57 @@ try {
78
70
  }
79
71
 
80
72
  yargs.usage(
81
- `${version()}\nUsage: https://webpack.js.org/configuration/dev-server/`
73
+ `${getVersions()}\nUsage: https://webpack.js.org/configuration/dev-server/`
82
74
  );
83
75
 
84
- require('webpack-cli/bin/config-yargs')(yargs);
76
+ // webpack-cli@3.3 path : 'webpack-cli/bin/config/config-yargs'
77
+ let configYargsPath;
78
+ try {
79
+ require.resolve('webpack-cli/bin/config/config-yargs');
80
+ configYargsPath = 'webpack-cli/bin/config/config-yargs';
81
+ } catch (e) {
82
+ configYargsPath = 'webpack-cli/bin/config-yargs';
83
+ }
84
+ // eslint-disable-next-line import/no-extraneous-dependencies
85
+ // eslint-disable-next-line import/no-dynamic-require
86
+ require(configYargsPath)(yargs);
87
+
85
88
  // It is important that this is done after the webpack yargs config,
86
89
  // so it overrides webpack's version info.
87
- yargs.version(version());
90
+ yargs.version(getVersions());
88
91
  yargs.options(options);
89
92
 
90
93
  const argv = yargs.argv;
91
94
 
92
- const config = require('webpack-cli/bin/convert-argv')(yargs, argv, {
93
- outputFilename: '/bundle.js'
95
+ // webpack-cli@3.3 path : 'webpack-cli/bin/utils/convert-argv'
96
+ let convertArgvPath;
97
+ try {
98
+ require.resolve('webpack-cli/bin/utils/convert-argv');
99
+ convertArgvPath = 'webpack-cli/bin/utils/convert-argv';
100
+ } catch (e) {
101
+ convertArgvPath = 'webpack-cli/bin/convert-argv';
102
+ }
103
+ // eslint-disable-next-line import/no-extraneous-dependencies
104
+ // eslint-disable-next-line import/no-dynamic-require
105
+ const config = require(convertArgvPath)(yargs, argv, {
106
+ outputFilename: '/bundle.js',
94
107
  });
108
+
95
109
  // Taken out of yargs because we must know if
96
110
  // it wasn't given by the user, in which case
97
111
  // we should use portfinder.
98
112
  const DEFAULT_PORT = 8080;
99
113
 
100
- function processOptions (config) {
114
+ // Try to find unused port and listen on it for 3 times,
115
+ // if port is not specified in options.
116
+ // Because NaN == null is false, defaultTo fails if parseInt returns NaN
117
+ // so the tryParseInt function is introduced to handle NaN
118
+ const defaultPortRetry = defaultTo(
119
+ tryParseInt(process.env.DEFAULT_PORT_RETRY),
120
+ 3
121
+ );
122
+
123
+ function processOptions(config) {
101
124
  // processOptions {Promise}
102
125
  if (typeof config.then === 'function') {
103
126
  config.then(processOptions).catch((err) => {
@@ -109,226 +132,13 @@ function processOptions (config) {
109
132
  return;
110
133
  }
111
134
 
112
- const firstWpOpt = Array.isArray(config)
113
- ? config[0]
114
- : config;
115
-
116
- const options = config.devServer || firstWpOpt.devServer || {};
117
-
118
- if (argv.bonjour) {
119
- options.bonjour = true;
120
- }
121
-
122
- if (argv.host !== 'localhost' || !options.host) {
123
- options.host = argv.host;
124
- }
125
-
126
- if (argv['allowed-hosts']) {
127
- options.allowedHosts = argv['allowed-hosts'].split(',');
128
- }
129
-
130
- if (argv.public) {
131
- options.public = argv.public;
132
- }
133
-
134
- if (argv.socket) {
135
- options.socket = argv.socket;
136
- }
137
-
138
- if (argv.progress) {
139
- options.progress = argv.progress;
140
- }
141
-
142
- if (!options.publicPath) {
143
- // eslint-disable-next-line
144
- options.publicPath = firstWpOpt.output && firstWpOpt.output.publicPath || '';
145
-
146
- if (
147
- !/^(https?:)?\/\//.test(options.publicPath) &&
148
- options.publicPath[0] !== '/'
149
- ) {
150
- options.publicPath = `/${options.publicPath}`;
151
- }
152
- }
153
-
154
- if (!options.filename) {
155
- options.filename = firstWpOpt.output && firstWpOpt.output.filename;
156
- }
157
-
158
- if (!options.watchOptions) {
159
- options.watchOptions = firstWpOpt.watchOptions;
160
- }
161
-
162
- if (argv.stdin) {
163
- process.stdin.on('end', () => {
164
- // eslint-disable-next-line no-process-exit
165
- process.exit(0);
166
- });
167
-
168
- process.stdin.resume();
169
- }
170
-
171
- if (!options.hot) {
172
- options.hot = argv.hot;
173
- }
174
-
175
- if (!options.hotOnly) {
176
- options.hotOnly = argv['hot-only'];
177
- }
178
-
179
- if (!options.clientLogLevel) {
180
- options.clientLogLevel = argv['client-log-level'];
181
- }
182
-
183
- // eslint-disable-next-line
184
- if (options.contentBase === undefined) {
185
- if (argv['content-base']) {
186
- options.contentBase = argv['content-base'];
187
-
188
- if (Array.isArray(options.contentBase)) {
189
- options.contentBase = options.contentBase.map((p) => path.resolve(p));
190
- } else if (/^[0-9]$/.test(options.contentBase)) {
191
- options.contentBase = +options.contentBase;
192
- } else if (!/^(https?:)?\/\//.test(options.contentBase)) {
193
- options.contentBase = path.resolve(options.contentBase);
194
- }
195
- // It is possible to disable the contentBase by using
196
- // `--no-content-base`, which results in arg["content-base"] = false
197
- } else if (argv['content-base'] === false) {
198
- options.contentBase = false;
199
- }
200
- }
201
-
202
- if (argv['watch-content-base']) {
203
- options.watchContentBase = true;
204
- }
205
-
206
- if (!options.stats) {
207
- options.stats = {
208
- cached: false,
209
- cachedAssets: false
210
- };
211
- }
212
-
213
- if (
214
- typeof options.stats === 'object' &&
215
- typeof options.stats.colors === 'undefined'
216
- ) {
217
- options.stats = Object.assign(
218
- {},
219
- options.stats,
220
- { colors: argv.color }
221
- );
222
- }
223
-
224
- if (argv.lazy) {
225
- options.lazy = true;
226
- }
227
-
228
- if (!argv.info) {
229
- options.noInfo = true;
230
- }
231
-
232
- if (argv.quiet) {
233
- options.quiet = true;
234
- }
235
-
236
- if (argv.https) {
237
- options.https = true;
238
- }
239
-
240
- if (argv.cert) {
241
- options.cert = fs.readFileSync(
242
- path.resolve(argv.cert)
243
- );
244
- }
245
-
246
- if (argv.key) {
247
- options.key = fs.readFileSync(
248
- path.resolve(argv.key)
249
- );
250
- }
251
-
252
- if (argv.cacert) {
253
- options.ca = fs.readFileSync(
254
- path.resolve(argv.cacert)
255
- );
256
- }
257
-
258
- if (argv.pfx) {
259
- options.pfx = fs.readFileSync(
260
- path.resolve(argv.pfx)
261
- );
262
- }
263
-
264
- if (argv['pfx-passphrase']) {
265
- options.pfxPassphrase = argv['pfx-passphrase'];
266
- }
267
-
268
- if (argv.inline === false) {
269
- options.inline = false;
270
- }
271
-
272
- if (argv['history-api-fallback']) {
273
- options.historyApiFallback = true;
274
- }
275
-
276
- if (argv.compress) {
277
- options.compress = true;
278
- }
279
-
280
- if (argv['disable-host-check']) {
281
- options.disableHostCheck = true;
282
- }
283
-
284
- if (argv['open-page']) {
285
- options.open = true;
286
- options.openPage = argv['open-page'];
287
- }
288
-
289
- if (typeof argv.open !== 'undefined') {
290
- options.open = argv.open !== '' ? argv.open : true;
291
- }
292
-
293
- if (options.open && !options.openPage) {
294
- options.openPage = '';
295
- }
296
-
297
- if (argv.useLocalIp) {
298
- options.useLocalIp = true;
299
- }
300
- // Kind of weird, but ensures prior behavior isn't broken in cases
301
- // that wouldn't throw errors. E.g. both argv.port and options.port
302
- // were specified, but since argv.port is 8080, options.port will be
303
- // tried first instead.
304
- options.port = argv.port === DEFAULT_PORT
305
- ? defaultTo(options.port, argv.port)
306
- : defaultTo(argv.port, options.port);
307
-
308
- if (options.port != null) {
309
- startDevServer(config, options);
310
-
311
- return;
312
- }
313
-
314
- portfinder.basePort = DEFAULT_PORT;
315
-
316
- portfinder.getPort((err, port) => {
317
- if (err) {
318
- throw err;
319
- }
320
-
321
- options.port = port;
322
-
323
- startDevServer(config, options);
324
- });
135
+ const options = createConfig(config, argv, { port: DEFAULT_PORT });
136
+ startDevServer(config, options);
325
137
  }
326
138
 
327
139
  function startDevServer(config, options) {
328
140
  const log = createLogger(options);
329
141
 
330
- addEntries(config, options);
331
-
332
142
  let compiler;
333
143
 
334
144
  try {
@@ -345,11 +155,14 @@ function startDevServer(config, options) {
345
155
 
346
156
  if (options.progress) {
347
157
  new webpack.ProgressPlugin({
348
- profile: argv.profile
158
+ profile: argv.profile,
349
159
  }).apply(compiler);
350
160
  }
351
161
 
352
- const suffix = (options.inline !== false || options.lazy === true ? '/' : '/webpack-dev-server/');
162
+ const suffix =
163
+ options.inline !== false || options.lazy === true
164
+ ? '/'
165
+ : '/webpack-dev-server/';
353
166
 
354
167
  try {
355
168
  server = new Server(compiler, options, log);
@@ -404,21 +217,35 @@ function startDevServer(config, options) {
404
217
  status(uri, options, log, argv.color);
405
218
  });
406
219
  });
407
- } else {
220
+ return;
221
+ }
222
+
223
+ const startServer = () => {
408
224
  server.listen(options.port, options.host, (err) => {
409
225
  if (err) {
410
226
  throw err;
411
227
  }
412
-
413
228
  if (options.bonjour) {
414
- bonjour(options);
229
+ runBonjour(options);
415
230
  }
416
-
417
231
  const uri = createDomain(options, server.listeningApp) + suffix;
418
-
419
232
  status(uri, options, log, argv.color);
420
233
  });
234
+ };
235
+
236
+ if (options.port) {
237
+ startServer();
238
+ return;
421
239
  }
240
+
241
+ // only run port finder if no port as been specified
242
+ findPort(server, DEFAULT_PORT, defaultPortRetry, (err, port) => {
243
+ if (err) {
244
+ throw err;
245
+ }
246
+ options.port = port;
247
+ startServer();
248
+ });
422
249
  }
423
250
 
424
251
  processOptions(config);