Skip to content

Commit 5ea376b

Browse files
hiroppyevilebottnawi
authored andcommitted
feat: set development mode by default when unspecified (#1653)
1 parent a52f2d7 commit 5ea376b

File tree

5 files changed

+340
-170
lines changed

5 files changed

+340
-170
lines changed

bin/webpack-dev-server.js

Lines changed: 5 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const debug = require('debug')('webpack-dev-server');
1717

1818
const fs = require('fs');
1919
const net = require('net');
20-
const path = require('path');
2120

2221
const portfinder = require('portfinder');
2322
const importLocal = require('import-local');
@@ -27,13 +26,14 @@ const webpack = require('webpack');
2726

2827
const options = require('./options');
2928

30-
const { colors, status, version, bonjour, defaultTo } = require('./utils');
29+
const { colors, status, version, bonjour } = require('./utils');
3130

3231
const Server = require('../lib/Server');
3332

3433
const addEntries = require('../lib/utils/addEntries');
3534
const createDomain = require('../lib/utils/createDomain');
3635
const createLogger = require('../lib/utils/createLogger');
36+
const createConfig = require('../lib/utils/createConfig');
3737

3838
let server;
3939

@@ -88,6 +88,7 @@ const argv = yargs.argv;
8888
const config = require('webpack-cli/bin/convert-argv')(yargs, argv, {
8989
outputFilename: '/bundle.js',
9090
});
91+
9192
// Taken out of yargs because we must know if
9293
// it wasn't given by the user, in which case
9394
// we should use portfinder.
@@ -105,182 +106,16 @@ function processOptions(config) {
105106
return;
106107
}
107108

108-
const firstWpOpt = Array.isArray(config) ? config[0] : config;
109-
110-
const options = config.devServer || firstWpOpt.devServer || {};
111-
112-
if (argv.bonjour) {
113-
options.bonjour = true;
114-
}
115-
116-
if (argv.host !== 'localhost' || !options.host) {
117-
options.host = argv.host;
118-
}
119-
120-
if (argv['allowed-hosts']) {
121-
options.allowedHosts = argv['allowed-hosts'].split(',');
122-
}
123-
124-
if (argv.public) {
125-
options.public = argv.public;
126-
}
127-
128-
if (argv.socket) {
129-
options.socket = argv.socket;
130-
}
131-
132-
if (argv.progress) {
133-
options.progress = argv.progress;
134-
}
135-
136-
if (!options.publicPath) {
137-
// eslint-disable-next-line
138-
options.publicPath =
139-
(firstWpOpt.output && firstWpOpt.output.publicPath) || '';
140-
141-
if (
142-
!/^(https?:)?\/\//.test(options.publicPath) &&
143-
options.publicPath[0] !== '/'
144-
) {
145-
options.publicPath = `/${options.publicPath}`;
146-
}
147-
}
148-
149-
if (!options.filename) {
150-
options.filename = firstWpOpt.output && firstWpOpt.output.filename;
151-
}
152-
153-
if (!options.watchOptions) {
154-
options.watchOptions = firstWpOpt.watchOptions;
155-
}
156-
157-
if (argv.stdin) {
158-
process.stdin.on('end', () => {
159-
// eslint-disable-next-line no-process-exit
160-
process.exit(0);
161-
});
162-
163-
process.stdin.resume();
164-
}
165-
166-
if (!options.hot) {
167-
options.hot = argv.hot;
168-
}
109+
const options = createConfig(config, argv, { port: DEFAULT_PORT });
169110

170-
if (!options.hotOnly) {
171-
options.hotOnly = argv['hot-only'];
172-
}
173-
174-
if (!options.clientLogLevel) {
175-
options.clientLogLevel = argv['client-log-level'];
176-
}
177-
178-
// eslint-disable-next-line
179-
if (options.contentBase === undefined) {
180-
if (argv['content-base']) {
181-
options.contentBase = argv['content-base'];
182-
183-
if (Array.isArray(options.contentBase)) {
184-
options.contentBase = options.contentBase.map((p) => path.resolve(p));
185-
} else if (/^[0-9]$/.test(options.contentBase)) {
186-
options.contentBase = +options.contentBase;
187-
} else if (!/^(https?:)?\/\//.test(options.contentBase)) {
188-
options.contentBase = path.resolve(options.contentBase);
189-
}
190-
// It is possible to disable the contentBase by using
191-
// `--no-content-base`, which results in arg["content-base"] = false
192-
} else if (argv['content-base'] === false) {
193-
options.contentBase = false;
194-
}
195-
}
196-
197-
if (argv['watch-content-base']) {
198-
options.watchContentBase = true;
199-
}
200-
201-
if (!options.stats) {
202-
options.stats = {
203-
cached: false,
204-
cachedAssets: false,
205-
};
206-
}
207-
208-
if (
209-
typeof options.stats === 'object' &&
210-
typeof options.stats.colors === 'undefined'
211-
) {
212-
options.stats = Object.assign({}, options.stats, { colors: argv.color });
213-
}
214-
215-
if (argv.lazy) {
216-
options.lazy = true;
217-
}
218-
219-
if (!argv.info) {
220-
options.noInfo = true;
221-
}
222-
223-
if (argv.quiet) {
224-
options.quiet = true;
225-
}
226-
227-
if (argv.https) {
228-
options.https = true;
229-
}
230-
231-
if (argv['pfx-passphrase']) {
232-
options.pfxPassphrase = argv['pfx-passphrase'];
233-
}
234-
235-
if (argv.inline === false) {
236-
options.inline = false;
237-
}
238-
239-
if (argv['history-api-fallback']) {
240-
options.historyApiFallback = true;
241-
}
242-
243-
if (argv.compress) {
244-
options.compress = true;
245-
}
246-
247-
if (argv['disable-host-check']) {
248-
options.disableHostCheck = true;
249-
}
250-
251-
if (argv['open-page']) {
252-
options.open = true;
253-
options.openPage = argv['open-page'];
254-
}
255-
256-
if (typeof argv.open !== 'undefined') {
257-
options.open = argv.open !== '' ? argv.open : true;
258-
}
259-
260-
if (options.open && !options.openPage) {
261-
options.openPage = '';
262-
}
263-
264-
if (argv.useLocalIp) {
265-
options.useLocalIp = true;
266-
}
267-
// Kind of weird, but ensures prior behavior isn't broken in cases
268-
// that wouldn't throw errors. E.g. both argv.port and options.port
269-
// were specified, but since argv.port is 8080, options.port will be
270-
// tried first instead.
271-
options.port =
272-
argv.port === DEFAULT_PORT
273-
? defaultTo(options.port, argv.port)
274-
: defaultTo(argv.port, options.port);
111+
portfinder.basePort = DEFAULT_PORT;
275112

276113
if (options.port != null) {
277114
startDevServer(config, options);
278115

279116
return;
280117
}
281118

282-
portfinder.basePort = DEFAULT_PORT;
283-
284119
portfinder.getPort((err, port) => {
285120
if (err) {
286121
throw err;

lib/utils/createConfig.js

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const { defaultTo } = require('../../bin/utils');
5+
6+
function createConfig(config, argv, { port }) {
7+
const firstWpOpt = Array.isArray(config) ? config[0] : config;
8+
const options = firstWpOpt.devServer || {};
9+
10+
// This updates both config and firstWpOpt
11+
firstWpOpt.mode = defaultTo(firstWpOpt.mode, 'development');
12+
13+
if (argv.bonjour) {
14+
options.bonjour = true;
15+
}
16+
17+
if (argv.host !== 'localhost' || !options.host) {
18+
options.host = argv.host;
19+
}
20+
21+
if (argv['allowed-hosts']) {
22+
options.allowedHosts = argv['allowed-hosts'].split(',');
23+
}
24+
25+
if (argv.public) {
26+
options.public = argv.public;
27+
}
28+
29+
if (argv.socket) {
30+
options.socket = argv.socket;
31+
}
32+
33+
if (argv.progress) {
34+
options.progress = argv.progress;
35+
}
36+
37+
if (!options.publicPath) {
38+
// eslint-disable-next-line
39+
options.publicPath =
40+
(firstWpOpt.output && firstWpOpt.output.publicPath) || '';
41+
42+
if (
43+
!/^(https?:)?\/\//.test(options.publicPath) &&
44+
options.publicPath[0] !== '/'
45+
) {
46+
options.publicPath = `/${options.publicPath}`;
47+
}
48+
}
49+
50+
if (!options.filename) {
51+
options.filename = firstWpOpt.output && firstWpOpt.output.filename;
52+
}
53+
54+
if (!options.watchOptions) {
55+
options.watchOptions = firstWpOpt.watchOptions;
56+
}
57+
58+
if (argv.stdin) {
59+
process.stdin.on('end', () => {
60+
// eslint-disable-next-line no-process-exit
61+
process.exit(0);
62+
});
63+
64+
process.stdin.resume();
65+
}
66+
67+
if (!options.hot) {
68+
options.hot = argv.hot;
69+
}
70+
71+
if (!options.hotOnly) {
72+
options.hotOnly = argv['hot-only'];
73+
}
74+
75+
if (!options.clientLogLevel) {
76+
options.clientLogLevel = argv['client-log-level'];
77+
}
78+
79+
// eslint-disable-next-line
80+
if (options.contentBase === undefined) {
81+
if (argv['content-base']) {
82+
options.contentBase = argv['content-base'];
83+
84+
if (Array.isArray(options.contentBase)) {
85+
options.contentBase = options.contentBase.map((p) => path.resolve(p));
86+
} else if (/^[0-9]$/.test(options.contentBase)) {
87+
options.contentBase = +options.contentBase;
88+
} else if (!/^(https?:)?\/\//.test(options.contentBase)) {
89+
options.contentBase = path.resolve(options.contentBase);
90+
}
91+
// It is possible to disable the contentBase by using
92+
// `--no-content-base`, which results in arg["content-base"] = false
93+
} else if (argv['content-base'] === false) {
94+
options.contentBase = false;
95+
}
96+
}
97+
98+
if (argv['watch-content-base']) {
99+
options.watchContentBase = true;
100+
}
101+
102+
if (!options.stats) {
103+
options.stats = {
104+
cached: false,
105+
cachedAssets: false,
106+
};
107+
}
108+
109+
if (
110+
typeof options.stats === 'object' &&
111+
typeof options.stats.colors === 'undefined'
112+
) {
113+
options.stats = Object.assign({}, options.stats, { colors: argv.color });
114+
}
115+
116+
if (argv.lazy) {
117+
options.lazy = true;
118+
}
119+
120+
if (!argv.info) {
121+
options.noInfo = true;
122+
}
123+
124+
if (argv.quiet) {
125+
options.quiet = true;
126+
}
127+
128+
if (argv.https) {
129+
options.https = true;
130+
}
131+
132+
if (argv['pfx-passphrase']) {
133+
options.pfxPassphrase = argv['pfx-passphrase'];
134+
}
135+
136+
if (argv.inline === false) {
137+
options.inline = false;
138+
}
139+
140+
if (argv['history-api-fallback']) {
141+
options.historyApiFallback = true;
142+
}
143+
144+
if (argv.compress) {
145+
options.compress = true;
146+
}
147+
148+
if (argv['disable-host-check']) {
149+
options.disableHostCheck = true;
150+
}
151+
152+
if (argv['open-page']) {
153+
options.open = true;
154+
options.openPage = argv['open-page'];
155+
}
156+
157+
if (typeof argv.open !== 'undefined') {
158+
options.open = argv.open !== '' ? argv.open : true;
159+
}
160+
161+
if (options.open && !options.openPage) {
162+
options.openPage = '';
163+
}
164+
165+
if (argv.useLocalIp) {
166+
options.useLocalIp = true;
167+
}
168+
169+
// Kind of weird, but ensures prior behavior isn't broken in cases
170+
// that wouldn't throw errors. E.g. both argv.port and options.port
171+
// were specified, but since argv.port is 8080, options.port will be
172+
// tried first instead.
173+
options.port =
174+
argv.port === port
175+
? defaultTo(options.port, argv.port)
176+
: defaultTo(argv.port, options.port);
177+
178+
return options;
179+
}
180+
181+
module.exports = createConfig;

0 commit comments

Comments
 (0)