1 | 'use strict';
|
---|
2 |
|
---|
3 | const path = require('path');
|
---|
4 | const isAbsoluteUrl = require('is-absolute-url');
|
---|
5 | const defaultTo = require('./defaultTo');
|
---|
6 |
|
---|
7 | function createConfig(config, argv, { port }) {
|
---|
8 | const firstWpOpt = Array.isArray(config) ? config[0] : config;
|
---|
9 | const options = firstWpOpt.devServer || {};
|
---|
10 |
|
---|
11 | // This updates both config and firstWpOpt
|
---|
12 | firstWpOpt.mode = defaultTo(firstWpOpt.mode, 'development');
|
---|
13 |
|
---|
14 | if (argv.bonjour) {
|
---|
15 | options.bonjour = true;
|
---|
16 | }
|
---|
17 |
|
---|
18 | if (argv.host && (argv.host !== 'localhost' || !options.host)) {
|
---|
19 | options.host = argv.host;
|
---|
20 | }
|
---|
21 |
|
---|
22 | if (argv.allowedHosts) {
|
---|
23 | options.allowedHosts = argv.allowedHosts.split(',');
|
---|
24 | }
|
---|
25 |
|
---|
26 | if (argv.public) {
|
---|
27 | options.public = argv.public;
|
---|
28 | }
|
---|
29 |
|
---|
30 | if (argv.socket) {
|
---|
31 | options.socket = argv.socket;
|
---|
32 | }
|
---|
33 |
|
---|
34 | if (argv.sockHost) {
|
---|
35 | options.sockHost = argv.sockHost;
|
---|
36 | }
|
---|
37 |
|
---|
38 | if (argv.sockPath) {
|
---|
39 | options.sockPath = argv.sockPath;
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (argv.sockPort) {
|
---|
43 | options.sockPort = argv.sockPort;
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (argv.liveReload === false) {
|
---|
47 | options.liveReload = false;
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (argv.profile) {
|
---|
51 | options.profile = argv.profile;
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (argv.progress) {
|
---|
55 | options.progress = argv.progress;
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (argv.overlay) {
|
---|
59 | options.overlay = argv.overlay;
|
---|
60 | }
|
---|
61 |
|
---|
62 | if (!options.publicPath) {
|
---|
63 | // eslint-disable-next-line
|
---|
64 | options.publicPath =
|
---|
65 | (firstWpOpt.output && firstWpOpt.output.publicPath) || '';
|
---|
66 |
|
---|
67 | if (
|
---|
68 | !isAbsoluteUrl(String(options.publicPath)) &&
|
---|
69 | options.publicPath[0] !== '/'
|
---|
70 | ) {
|
---|
71 | options.publicPath = `/${options.publicPath}`;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (!options.filename && firstWpOpt.output && firstWpOpt.output.filename) {
|
---|
76 | options.filename = firstWpOpt.output && firstWpOpt.output.filename;
|
---|
77 | }
|
---|
78 |
|
---|
79 | if (!options.watchOptions && firstWpOpt.watchOptions) {
|
---|
80 | options.watchOptions = firstWpOpt.watchOptions;
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (argv.stdin) {
|
---|
84 | process.stdin.on('end', () => {
|
---|
85 | // eslint-disable-next-line no-process-exit
|
---|
86 | process.exit(0);
|
---|
87 | });
|
---|
88 |
|
---|
89 | process.stdin.resume();
|
---|
90 | }
|
---|
91 |
|
---|
92 | // TODO https://github.com/webpack/webpack-dev-server/issues/616 (v4)
|
---|
93 | // We should prefer CLI arg under config, now we always prefer `hot` from `devServer`
|
---|
94 | if (!options.hot) {
|
---|
95 | options.hot = argv.hot;
|
---|
96 | }
|
---|
97 |
|
---|
98 | // TODO https://github.com/webpack/webpack-dev-server/issues/616 (v4)
|
---|
99 | // We should prefer CLI arg under config, now we always prefer `hotOnly` from `devServer`
|
---|
100 | if (!options.hotOnly) {
|
---|
101 | options.hotOnly = argv.hotOnly;
|
---|
102 | }
|
---|
103 |
|
---|
104 | // TODO https://github.com/webpack/webpack-dev-server/issues/616 (v4)
|
---|
105 | // We should prefer CLI arg under config, now we always prefer `clientLogLevel` from `devServer`
|
---|
106 | if (!options.clientLogLevel && argv.clientLogLevel) {
|
---|
107 | options.clientLogLevel = argv.clientLogLevel;
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (argv.contentBase) {
|
---|
111 | options.contentBase = argv.contentBase;
|
---|
112 |
|
---|
113 | if (Array.isArray(options.contentBase)) {
|
---|
114 | options.contentBase = options.contentBase.map((p) => path.resolve(p));
|
---|
115 | } else if (/^[0-9]$/.test(options.contentBase)) {
|
---|
116 | options.contentBase = +options.contentBase;
|
---|
117 | } else if (!isAbsoluteUrl(String(options.contentBase))) {
|
---|
118 | options.contentBase = path.resolve(options.contentBase);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | // It is possible to disable the contentBase by using
|
---|
122 | // `--no-content-base`, which results in arg["content-base"] = false
|
---|
123 | else if (argv.contentBase === false) {
|
---|
124 | options.contentBase = false;
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (argv.watchContentBase) {
|
---|
128 | options.watchContentBase = true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (!options.stats) {
|
---|
132 | options.stats = defaultTo(firstWpOpt.stats, {
|
---|
133 | cached: false,
|
---|
134 | cachedAssets: false,
|
---|
135 | });
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (
|
---|
139 | typeof options.stats === 'object' &&
|
---|
140 | typeof options.stats.colors === 'undefined' &&
|
---|
141 | argv.color
|
---|
142 | ) {
|
---|
143 | options.stats = Object.assign({}, options.stats, { colors: argv.color });
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (argv.lazy) {
|
---|
147 | options.lazy = true;
|
---|
148 | }
|
---|
149 |
|
---|
150 | // TODO remove in `v4`
|
---|
151 | if (!argv.info) {
|
---|
152 | options.noInfo = true;
|
---|
153 | }
|
---|
154 |
|
---|
155 | // TODO remove in `v4`
|
---|
156 | if (argv.quiet) {
|
---|
157 | options.quiet = true;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (argv.https) {
|
---|
161 | options.https = true;
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (argv.http2) {
|
---|
165 | options.http2 = true;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (argv.key) {
|
---|
169 | options.key = argv.key;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (argv.cert) {
|
---|
173 | options.cert = argv.cert;
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (argv.cacert) {
|
---|
177 | options.ca = argv.cacert;
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (argv.pfx) {
|
---|
181 | options.pfx = argv.pfx;
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (argv.pfxPassphrase) {
|
---|
185 | options.pfxPassphrase = argv.pfxPassphrase;
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (argv.inline === false) {
|
---|
189 | options.inline = false;
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (argv.historyApiFallback) {
|
---|
193 | options.historyApiFallback = true;
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (argv.compress) {
|
---|
197 | options.compress = true;
|
---|
198 | }
|
---|
199 |
|
---|
200 | if (argv.disableHostCheck) {
|
---|
201 | options.disableHostCheck = true;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (argv.openPage) {
|
---|
205 | options.open = true;
|
---|
206 | options.openPage = argv.openPage.split(',');
|
---|
207 | }
|
---|
208 |
|
---|
209 | if (typeof argv.open !== 'undefined') {
|
---|
210 | options.open = argv.open !== '' ? argv.open : true;
|
---|
211 | }
|
---|
212 |
|
---|
213 | if (options.open && !options.openPage) {
|
---|
214 | options.openPage = '';
|
---|
215 | }
|
---|
216 |
|
---|
217 | if (argv.useLocalIp) {
|
---|
218 | options.useLocalIp = true;
|
---|
219 | }
|
---|
220 |
|
---|
221 | // Kind of weird, but ensures prior behavior isn't broken in cases
|
---|
222 | // that wouldn't throw errors. E.g. both argv.port and options.port
|
---|
223 | // were specified, but since argv.port is 8080, options.port will be
|
---|
224 | // tried first instead.
|
---|
225 | options.port =
|
---|
226 | argv.port === port
|
---|
227 | ? defaultTo(options.port, argv.port)
|
---|
228 | : defaultTo(argv.port, options.port);
|
---|
229 |
|
---|
230 | return options;
|
---|
231 | }
|
---|
232 |
|
---|
233 | module.exports = createConfig;
|
---|