UNPKG

4.15 kBJavaScriptView Raw
1'use strict';
2
3const path = require('path');
4const { defaultTo } = require('../../bin/utils');
5
6function 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
181module.exports = createConfig;