createConfig.js
5.27 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
const path = require('path');
const isAbsoluteUrl = require('is-absolute-url');
const defaultTo = require('./defaultTo');
function createConfig(config, argv, { port }) {
const firstWpOpt = Array.isArray(config) ? config[0] : config;
const options = firstWpOpt.devServer || {};
// This updates both config and firstWpOpt
firstWpOpt.mode = defaultTo(firstWpOpt.mode, 'development');
if (argv.bonjour) {
options.bonjour = true;
}
if (argv.host && (argv.host !== 'localhost' || !options.host)) {
options.host = argv.host;
}
if (argv.allowedHosts) {
options.allowedHosts = argv.allowedHosts.split(',');
}
if (argv.public) {
options.public = argv.public;
}
if (argv.socket) {
options.socket = argv.socket;
}
if (argv.sockHost) {
options.sockHost = argv.sockHost;
}
if (argv.sockPath) {
options.sockPath = argv.sockPath;
}
if (argv.sockPort) {
options.sockPort = argv.sockPort;
}
if (argv.liveReload === false) {
options.liveReload = false;
}
if (argv.profile) {
options.profile = argv.profile;
}
if (argv.progress) {
options.progress = argv.progress;
}
if (argv.overlay) {
options.overlay = argv.overlay;
}
if (!options.publicPath) {
// eslint-disable-next-line
options.publicPath =
(firstWpOpt.output && firstWpOpt.output.publicPath) || '';
if (
!isAbsoluteUrl(String(options.publicPath)) &&
options.publicPath[0] !== '/'
) {
options.publicPath = `/${options.publicPath}`;
}
}
if (!options.filename && firstWpOpt.output && firstWpOpt.output.filename) {
options.filename = firstWpOpt.output && firstWpOpt.output.filename;
}
if (!options.watchOptions && firstWpOpt.watchOptions) {
options.watchOptions = firstWpOpt.watchOptions;
}
if (argv.stdin) {
process.stdin.on('end', () => {
// eslint-disable-next-line no-process-exit
process.exit(0);
});
process.stdin.resume();
}
// TODO https://github.com/webpack/webpack-dev-server/issues/616 (v4)
// We should prefer CLI arg under config, now we always prefer `hot` from `devServer`
if (!options.hot) {
options.hot = argv.hot;
}
// TODO https://github.com/webpack/webpack-dev-server/issues/616 (v4)
// We should prefer CLI arg under config, now we always prefer `hotOnly` from `devServer`
if (!options.hotOnly) {
options.hotOnly = argv.hotOnly;
}
// TODO https://github.com/webpack/webpack-dev-server/issues/616 (v4)
// We should prefer CLI arg under config, now we always prefer `clientLogLevel` from `devServer`
if (!options.clientLogLevel && argv.clientLogLevel) {
options.clientLogLevel = argv.clientLogLevel;
}
if (argv.contentBase) {
options.contentBase = argv.contentBase;
if (Array.isArray(options.contentBase)) {
options.contentBase = options.contentBase.map((p) => path.resolve(p));
} else if (/^[0-9]$/.test(options.contentBase)) {
options.contentBase = +options.contentBase;
} else if (!isAbsoluteUrl(String(options.contentBase))) {
options.contentBase = path.resolve(options.contentBase);
}
}
// It is possible to disable the contentBase by using
// `--no-content-base`, which results in arg["content-base"] = false
else if (argv.contentBase === false) {
options.contentBase = false;
}
if (argv.watchContentBase) {
options.watchContentBase = true;
}
if (!options.stats) {
options.stats = defaultTo(firstWpOpt.stats, {
cached: false,
cachedAssets: false,
});
}
if (
typeof options.stats === 'object' &&
typeof options.stats.colors === 'undefined' &&
argv.color
) {
options.stats = Object.assign({}, options.stats, { colors: argv.color });
}
if (argv.lazy) {
options.lazy = true;
}
// TODO remove in `v4`
if (!argv.info) {
options.noInfo = true;
}
// TODO remove in `v4`
if (argv.quiet) {
options.quiet = true;
}
if (argv.https) {
options.https = true;
}
if (argv.http2) {
options.http2 = true;
}
if (argv.key) {
options.key = argv.key;
}
if (argv.cert) {
options.cert = argv.cert;
}
if (argv.cacert) {
options.ca = argv.cacert;
}
if (argv.pfx) {
options.pfx = argv.pfx;
}
if (argv.pfxPassphrase) {
options.pfxPassphrase = argv.pfxPassphrase;
}
if (argv.inline === false) {
options.inline = false;
}
if (argv.historyApiFallback) {
options.historyApiFallback = true;
}
if (argv.compress) {
options.compress = true;
}
if (argv.disableHostCheck) {
options.disableHostCheck = true;
}
if (argv.openPage) {
options.open = true;
options.openPage = argv.openPage.split(',');
}
if (typeof argv.open !== 'undefined') {
options.open = argv.open !== '' ? argv.open : true;
}
if (options.open && !options.openPage) {
options.openPage = '';
}
if (argv.useLocalIp) {
options.useLocalIp = true;
}
// Kind of weird, but ensures prior behavior isn't broken in cases
// that wouldn't throw errors. E.g. both argv.port and options.port
// were specified, but since argv.port is 8080, options.port will be
// tried first instead.
options.port =
argv.port === port
? defaultTo(options.port, argv.port)
: defaultTo(argv.port, options.port);
return options;
}
module.exports = createConfig;