Skip to content

Commit 2b05786

Browse files
authored
refactor: normalize util (#3469)
1 parent 614451e commit 2b05786

File tree

1 file changed

+105
-104
lines changed

1 file changed

+105
-104
lines changed

lib/utils/normalizeOptions.js

Lines changed: 105 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -25,92 +25,18 @@ function normalizeOptions(compiler, options, logger, cacheDir) {
2525
watch: watchOptions,
2626
};
2727

28-
if (typeof options.static === 'undefined') {
29-
options.static = [defaultOptionsForStatic];
30-
} else if (typeof options.static === 'boolean') {
31-
options.static = options.static ? [defaultOptionsForStatic] : false;
32-
} else if (typeof options.static === 'string') {
33-
options.static = [
34-
{ ...defaultOptionsForStatic, directory: options.static },
35-
];
36-
} else if (Array.isArray(options.static)) {
37-
options.static = options.static.map((item) => {
38-
if (typeof item === 'string') {
39-
return { ...defaultOptionsForStatic, directory: item };
40-
}
41-
42-
return { ...defaultOptionsForStatic, ...item };
43-
});
44-
} else {
45-
options.static = [{ ...defaultOptionsForStatic, ...options.static }];
46-
}
47-
48-
if (options.static) {
49-
const isAbsoluteUrl = require('is-absolute-url');
50-
51-
options.static.forEach((staticOption) => {
52-
if (isAbsoluteUrl(staticOption.directory)) {
53-
throw new Error('Using a URL as static.directory is not supported');
54-
}
55-
56-
// ensure that publicPath is an array
57-
if (typeof staticOption.publicPath === 'string') {
58-
staticOption.publicPath = [staticOption.publicPath];
59-
}
60-
61-
// ensure that watch is an object if true
62-
if (staticOption.watch === true) {
63-
staticOption.watch = defaultOptionsForStatic.watch;
64-
}
65-
66-
// ensure that serveIndex is an object if true
67-
if (staticOption.serveIndex === true) {
68-
staticOption.serveIndex = defaultOptionsForStatic.serveIndex;
69-
}
70-
});
71-
}
72-
73-
options.hot =
74-
typeof options.hot === 'boolean' || options.hot === 'only'
75-
? options.hot
76-
: true;
77-
options.liveReload =
78-
typeof options.liveReload !== 'undefined' ? options.liveReload : true;
79-
80-
if (typeof options.port === 'string' && options.port !== 'auto') {
81-
options.port = Number(options.port);
28+
if (typeof options.allowedHosts === 'undefined') {
29+
// allowedHosts allows some default hosts picked from
30+
// `options.host` or `webSocketURL.hostname` and `localhost`
31+
options.allowedHosts = 'auto';
8232
}
83-
84-
const defaultWebSocketServerType = 'ws';
85-
const defaultWebSocketServerOptions = { path: '/ws' };
86-
87-
if (typeof options.webSocketServer === 'undefined') {
88-
options.webSocketServer = {
89-
type: defaultWebSocketServerType,
90-
options: defaultWebSocketServerOptions,
91-
};
92-
} else if (
93-
typeof options.webSocketServer === 'string' ||
94-
typeof options.webSocketServer === 'function'
33+
if (
34+
typeof options.allowedHosts === 'string' &&
35+
options.allowedHosts !== 'auto' &&
36+
options.allowedHosts !== 'all'
9537
) {
96-
options.webSocketServer = {
97-
type: options.webSocketServer,
98-
options: defaultWebSocketServerOptions,
99-
};
100-
} else {
101-
options.webSocketServer = {
102-
type: options.webSocketServer.type || defaultWebSocketServerType,
103-
options: {
104-
...defaultWebSocketServerOptions,
105-
...options.webSocketServer.options,
106-
},
107-
};
108-
109-
if (typeof options.webSocketServer.options.port === 'string') {
110-
options.webSocketServer.options.port = Number(
111-
options.webSocketServer.options.port
112-
);
113-
}
38+
// we store allowedHosts as array when supplied as string
39+
options.allowedHosts = [options.allowedHosts];
11440
}
11541

11642
if (!options.client) {
@@ -150,30 +76,17 @@ function normalizeOptions(compiler, options, logger, cacheDir) {
15076
options.client.hotEntry = options.hot;
15177
}
15278

153-
options.devMiddleware = options.devMiddleware || {};
154-
155-
if (typeof options.allowedHosts === 'undefined') {
156-
// allowedHosts allows some default hosts picked from
157-
// `options.host` or `webSocketURL.hostname` and `localhost`
158-
options.allowedHosts = 'auto';
159-
}
160-
if (
161-
typeof options.allowedHosts === 'string' &&
162-
options.allowedHosts !== 'auto' &&
163-
options.allowedHosts !== 'all'
164-
) {
165-
// we store allowedHosts as array when supplied as string
166-
options.allowedHosts = [options.allowedHosts];
167-
}
168-
169-
if (typeof options.setupExitSignals === 'undefined') {
170-
options.setupExitSignals = true;
171-
}
172-
17379
if (typeof options.compress === 'undefined') {
17480
options.compress = true;
17581
}
17682

83+
options.devMiddleware = options.devMiddleware || {};
84+
85+
options.hot =
86+
typeof options.hot === 'boolean' || options.hot === 'only'
87+
? options.hot
88+
: true;
89+
17790
// if the user enables http2, we can safely enable https
17891
if ((options.http2 && !options.https) || options.https === true) {
17992
options.https = {
@@ -306,6 +219,13 @@ function normalizeOptions(compiler, options, logger, cacheDir) {
306219
options.https.cert = options.https.cert || fakeCert;
307220
}
308221

222+
options.liveReload =
223+
typeof options.liveReload !== 'undefined' ? options.liveReload : true;
224+
225+
if (typeof options.port === 'string' && options.port !== 'auto') {
226+
options.port = Number(options.port);
227+
}
228+
309229
/**
310230
* Assume a proxy configuration specified as:
311231
* proxy: {
@@ -373,6 +293,87 @@ function normalizeOptions(compiler, options, logger, cacheDir) {
373293
}
374294
}
375295
}
296+
297+
if (typeof options.setupExitSignals === 'undefined') {
298+
options.setupExitSignals = true;
299+
}
300+
301+
if (typeof options.static === 'undefined') {
302+
options.static = [defaultOptionsForStatic];
303+
} else if (typeof options.static === 'boolean') {
304+
options.static = options.static ? [defaultOptionsForStatic] : false;
305+
} else if (typeof options.static === 'string') {
306+
options.static = [
307+
{ ...defaultOptionsForStatic, directory: options.static },
308+
];
309+
} else if (Array.isArray(options.static)) {
310+
options.static = options.static.map((item) => {
311+
if (typeof item === 'string') {
312+
return { ...defaultOptionsForStatic, directory: item };
313+
}
314+
315+
return { ...defaultOptionsForStatic, ...item };
316+
});
317+
} else {
318+
options.static = [{ ...defaultOptionsForStatic, ...options.static }];
319+
}
320+
321+
if (options.static) {
322+
const isAbsoluteUrl = require('is-absolute-url');
323+
324+
options.static.forEach((staticOption) => {
325+
if (isAbsoluteUrl(staticOption.directory)) {
326+
throw new Error('Using a URL as static.directory is not supported');
327+
}
328+
329+
// ensure that publicPath is an array
330+
if (typeof staticOption.publicPath === 'string') {
331+
staticOption.publicPath = [staticOption.publicPath];
332+
}
333+
334+
// ensure that watch is an object if true
335+
if (staticOption.watch === true) {
336+
staticOption.watch = defaultOptionsForStatic.watch;
337+
}
338+
339+
// ensure that serveIndex is an object if true
340+
if (staticOption.serveIndex === true) {
341+
staticOption.serveIndex = defaultOptionsForStatic.serveIndex;
342+
}
343+
});
344+
}
345+
346+
const defaultWebSocketServerType = 'ws';
347+
const defaultWebSocketServerOptions = { path: '/ws' };
348+
349+
if (typeof options.webSocketServer === 'undefined') {
350+
options.webSocketServer = {
351+
type: defaultWebSocketServerType,
352+
options: defaultWebSocketServerOptions,
353+
};
354+
} else if (
355+
typeof options.webSocketServer === 'string' ||
356+
typeof options.webSocketServer === 'function'
357+
) {
358+
options.webSocketServer = {
359+
type: options.webSocketServer,
360+
options: defaultWebSocketServerOptions,
361+
};
362+
} else {
363+
options.webSocketServer = {
364+
type: options.webSocketServer.type || defaultWebSocketServerType,
365+
options: {
366+
...defaultWebSocketServerOptions,
367+
...options.webSocketServer.options,
368+
},
369+
};
370+
371+
if (typeof options.webSocketServer.options.port === 'string') {
372+
options.webSocketServer.options.port = Number(
373+
options.webSocketServer.options.port
374+
);
375+
}
376+
}
376377
}
377378

378379
module.exports = normalizeOptions;

0 commit comments

Comments
 (0)