process-options.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. module.exports = function processOptions(yargs, argv) {
  2. // process Promise
  3. function ifArg(name, fn, init) {
  4. if (Array.isArray(argv[name])) {
  5. if (init) init();
  6. argv[name].forEach(fn);
  7. } else if (typeof argv[name] !== "undefined") {
  8. if (init) init();
  9. fn(argv[name], -1);
  10. }
  11. }
  12. const options = require("./convert-argv")(argv);
  13. if (typeof options.then === "function") {
  14. options.then(processOptions).catch(function(err) {
  15. console.error(err.stack || err);
  16. process.exitCode = 1;
  17. });
  18. return;
  19. }
  20. const firstOptions = Array.isArray(options) ? options[0] || {} : options;
  21. if (typeof options.stats === "boolean" || typeof options.stats === "string") {
  22. const statsPresetToOptions = require("webpack").Stats.presetToOptions;
  23. options.stats = statsPresetToOptions(options.stats);
  24. }
  25. const outputOptions = Object.create(
  26. options.stats || firstOptions.stats || {}
  27. );
  28. if (typeof outputOptions.context === "undefined")
  29. outputOptions.context = firstOptions.context;
  30. ifArg("json", function(bool) {
  31. if (bool) outputOptions.json = bool;
  32. });
  33. if (typeof outputOptions.colors === "undefined")
  34. outputOptions.colors = require("supports-color");
  35. ifArg("sort-modules-by", function(value) {
  36. outputOptions.modulesSort = value;
  37. });
  38. ifArg("sort-chunks-by", function(value) {
  39. outputOptions.chunksSort = value;
  40. });
  41. ifArg("sort-assets-by", function(value) {
  42. outputOptions.assetsSort = value;
  43. });
  44. ifArg("display-exclude", function(value) {
  45. outputOptions.exclude = value;
  46. });
  47. if (!outputOptions.json) {
  48. if (typeof outputOptions.cached === "undefined")
  49. outputOptions.cached = false;
  50. if (typeof outputOptions.cachedAssets === "undefined")
  51. outputOptions.cachedAssets = false;
  52. ifArg("display-chunks", function(bool) {
  53. outputOptions.modules = !bool;
  54. outputOptions.chunks = bool;
  55. });
  56. ifArg("display-entrypoints", function(bool) {
  57. outputOptions.entrypoints = bool;
  58. });
  59. ifArg("display-reasons", function(bool) {
  60. outputOptions.reasons = bool;
  61. });
  62. ifArg("display-used-exports", function(bool) {
  63. outputOptions.usedExports = bool;
  64. });
  65. ifArg("display-provided-exports", function(bool) {
  66. outputOptions.providedExports = bool;
  67. });
  68. ifArg("display-error-details", function(bool) {
  69. outputOptions.errorDetails = bool;
  70. });
  71. ifArg("display-origins", function(bool) {
  72. outputOptions.chunkOrigins = bool;
  73. });
  74. ifArg("display-cached", function(bool) {
  75. if (bool) outputOptions.cached = true;
  76. });
  77. ifArg("display-cached-assets", function(bool) {
  78. if (bool) outputOptions.cachedAssets = true;
  79. });
  80. ifArg("build-delimiter", function(value) {
  81. outputOptions.buildDelimiter = value;
  82. });
  83. if (!outputOptions.exclude && !argv["display-modules"])
  84. outputOptions.exclude = [
  85. "node_modules",
  86. "bower_components",
  87. "jam",
  88. "components"
  89. ];
  90. } else {
  91. if (typeof outputOptions.chunks === "undefined")
  92. outputOptions.chunks = true;
  93. if (typeof outputOptions.entrypoints === "undefined")
  94. outputOptions.entrypoints = true;
  95. if (typeof outputOptions.modules === "undefined")
  96. outputOptions.modules = true;
  97. if (typeof outputOptions.chunkModules === "undefined")
  98. outputOptions.chunkModules = true;
  99. if (typeof outputOptions.reasons === "undefined")
  100. outputOptions.reasons = true;
  101. if (typeof outputOptions.cached === "undefined")
  102. outputOptions.cached = true;
  103. if (typeof outputOptions.cachedAssets === "undefined")
  104. outputOptions.cachedAssets = true;
  105. }
  106. ifArg("hide-modules", function(bool) {
  107. if (bool) {
  108. outputOptions.modules = false;
  109. outputOptions.chunkModules = false;
  110. }
  111. });
  112. const webpack = require("webpack");
  113. Error.stackTraceLimit = 30;
  114. let lastHash = null;
  115. let compiler;
  116. try {
  117. compiler = webpack(options);
  118. } catch (e) {
  119. const WebpackOptionsValidationError = require("webpack")
  120. .WebpackOptionsValidationError;
  121. if (e instanceof WebpackOptionsValidationError) {
  122. if (argv.color)
  123. console.error(
  124. "\u001b[1m\u001b[31m" + e.message + "\u001b[39m\u001b[22m"
  125. );
  126. else console.error(e.message);
  127. process.exitCode = 1;
  128. }
  129. throw e;
  130. }
  131. if (argv.progress) {
  132. const ProgressPlugin = require("webpack").ProgressPlugin;
  133. new ProgressPlugin({
  134. profile: argv.profile
  135. }).apply(compiler);
  136. }
  137. function compilerCallback(err, stats) {
  138. if (!options.watch || err) {
  139. // Do not keep cache anymore
  140. compiler.purgeInputFileSystem();
  141. }
  142. if (err) {
  143. lastHash = null;
  144. console.error(err.stack || err);
  145. if (err.details) console.error(err.details);
  146. process.exitCode = 1;
  147. }
  148. if (outputOptions.json) {
  149. process.stdout.write(
  150. JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n"
  151. );
  152. } else if (stats.hash !== lastHash) {
  153. lastHash = stats.hash;
  154. const delimiter = outputOptions.buildDelimiter
  155. ? `${outputOptions.buildDelimiter}\n`
  156. : "";
  157. process.stdout.write("\n" + new Date() + "\n" + "\n");
  158. process.stdout.write(`${stats.toString(outputOptions)}\n${delimiter}`);
  159. if (argv.s) lastHash = null;
  160. }
  161. if (!options.watch && stats.hasErrors()) {
  162. process.on("exit", function(_) {
  163. process.exitCode = 2;
  164. });
  165. }
  166. }
  167. if (options.watch) {
  168. const primaryOptions = !Array.isArray(options) ? options : options[0];
  169. const watchOptions =
  170. primaryOptions.watchOptions || primaryOptions.watch || {};
  171. if (watchOptions.stdin) {
  172. process.stdin.on("end", function(_) {
  173. process.exitCode = 0;
  174. });
  175. process.stdin.resume();
  176. }
  177. compiler.watch(watchOptions, compilerCallback);
  178. console.log("\nWebpack is watching the files…\n");
  179. } else compiler.run(compilerCallback);
  180. };