createConfig.js 5.3 KB

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