createConfig.js 5.2 KB

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