-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathnormalizeOptions.js
188 lines (157 loc) · 5.39 KB
/
normalizeOptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'use strict';
const path = require('path');
const fs = require('graceful-fs');
const isAbsoluteUrl = require('is-absolute-url');
const getCompilerConfigArray = require('./getCompilerConfigArray');
function normalizeOptions(compiler, options, logger) {
// TODO: improve this to not use .find for compiler watchOptions
const configArr = getCompilerConfigArray(compiler);
const watchOptionsConfig = configArr.find(
(config) => config.watch !== false && config.watchOptions
);
const watchOptions = watchOptionsConfig
? watchOptionsConfig.watchOptions
: {};
const defaultOptionsForStatic = {
directory: path.join(process.cwd(), 'public'),
staticOptions: {},
publicPath: ['/'],
serveIndex: { icons: true },
// Respect options from compiler watchOptions
watch: watchOptions,
};
if (typeof options.static === 'undefined') {
options.static = [defaultOptionsForStatic];
} else if (typeof options.static === 'boolean') {
options.static = options.static ? [defaultOptionsForStatic] : false;
} else if (typeof options.static === 'string') {
options.static = [
{ ...defaultOptionsForStatic, directory: options.static },
];
} else if (Array.isArray(options.static)) {
options.static = options.static.map((item) => {
if (typeof item === 'string') {
return { ...defaultOptionsForStatic, directory: item };
}
return { ...defaultOptionsForStatic, ...item };
});
} else {
options.static = [{ ...defaultOptionsForStatic, ...options.static }];
}
if (options.static) {
options.static.forEach((staticOption) => {
if (isAbsoluteUrl(staticOption.directory)) {
throw new Error('Using a URL as static.directory is not supported');
}
// ensure that publicPath is an array
if (typeof staticOption.publicPath === 'string') {
staticOption.publicPath = [staticOption.publicPath];
}
// ensure that watch is an object if true
if (staticOption.watch === true) {
staticOption.watch = defaultOptionsForStatic.watch;
}
// ensure that serveIndex is an object if true
if (staticOption.serveIndex === true) {
staticOption.serveIndex = defaultOptionsForStatic.serveIndex;
}
});
}
options.hot =
typeof options.hot === 'boolean' || options.hot === 'only'
? options.hot
: true;
options.liveReload =
typeof options.liveReload !== 'undefined' ? options.liveReload : true;
const defaultWebSocketServerType = 'ws';
const defaultWebSocketServerOptions = { path: '/ws' };
if (typeof options.webSocketServer === 'undefined') {
options.webSocketServer = {
type: defaultWebSocketServerType,
options: defaultWebSocketServerOptions,
};
} else if (
typeof options.webSocketServer === 'string' ||
typeof options.webSocketServer === 'function'
) {
options.webSocketServer = {
type: options.webSocketServer,
options: defaultWebSocketServerOptions,
};
} else {
options.webSocketServer = {
type: options.webSocketServer.type || defaultWebSocketServerType,
options: {
...defaultWebSocketServerOptions,
...options.webSocketServer.options,
},
};
}
if (!options.client) {
options.client = {};
}
if (typeof options.client.webSocketURL === 'undefined') {
options.client.webSocketURL = {};
} else if (typeof options.client.webSocketURL === 'string') {
const parsedURL = new URL(options.client.webSocketURL);
options.client.webSocketURL = {
host: parsedURL.hostname,
port: parsedURL.port,
path: parsedURL.pathname,
};
}
// Enable client overlay by default
if (typeof options.client.overlay === 'undefined') {
options.client.overlay = true;
}
// client.hotEntry
if (typeof options.client.hotEntry === 'undefined') {
options.client.hotEntry = options.hot;
}
options.devMiddleware = options.devMiddleware || {};
if (typeof options.allowedHosts === 'undefined') {
// allowedHosts is disabled by default
options.allowedHosts = false;
} else if (typeof options.allowedHosts === 'string') {
// we store allowedHosts as array when supplied as string
options.allowedHosts = [options.allowedHosts];
}
if (typeof options.setupExitSignals === 'undefined') {
options.setupExitSignals = true;
}
if (typeof options.compress === 'undefined') {
options.compress = true;
}
// if the user enables http2, we can safely enable https
if ((options.http2 && !options.https) || options.https === true) {
options.https = {
requestCert: false,
};
}
if (options.https) {
const getCertificate = require('./getCertificate');
for (const property of ['cacert', 'pfx', 'key', 'cert']) {
const value = options.https[property];
const isBuffer = value instanceof Buffer;
if (value && !isBuffer) {
let stats = null;
try {
stats = fs.lstatSync(fs.realpathSync(value)).isFile();
} catch (error) {
// ignore error
}
// It is file
options.https[property] = stats
? fs.readFileSync(path.resolve(value))
: value;
}
}
let fakeCert;
if (!options.https.key || !options.https.cert) {
fakeCert = getCertificate(logger);
}
options.https.key = options.https.key || fakeCert;
options.https.cert = options.https.cert || fakeCert;
}
}
module.exports = normalizeOptions;