createConfig.js 4.8 KB

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