setupHooks.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = setupHooks;
  6. var _webpack = _interopRequireDefault(require("webpack"));
  7. var _colorette = _interopRequireDefault(require("colorette"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function setupHooks(context) {
  10. function invalid() {
  11. if (context.state) {
  12. context.logger.log("Compilation starting...");
  13. } // We are now in invalid state
  14. // eslint-disable-next-line no-param-reassign
  15. context.state = false; // eslint-disable-next-line no-param-reassign, no-undefined
  16. context.stats = undefined;
  17. }
  18. const statsForWebpack4 = _webpack.default.Stats && _webpack.default.Stats.presetToOptions;
  19. function normalizeStatsOptions(statsOptions) {
  20. if (statsForWebpack4) {
  21. if (typeof statsOptions === "undefined") {
  22. // eslint-disable-next-line no-param-reassign
  23. statsOptions = {};
  24. } else if (typeof statsOptions === "boolean" || typeof statsOptions === "string") {
  25. // eslint-disable-next-line no-param-reassign
  26. statsOptions = _webpack.default.Stats.presetToOptions(statsOptions);
  27. }
  28. return statsOptions;
  29. }
  30. if (typeof statsOptions === "undefined") {
  31. // eslint-disable-next-line no-param-reassign
  32. statsOptions = {
  33. preset: "normal"
  34. };
  35. } else if (typeof statsOptions === "boolean") {
  36. // eslint-disable-next-line no-param-reassign
  37. statsOptions = statsOptions ? {
  38. preset: "normal"
  39. } : {
  40. preset: "none"
  41. };
  42. } else if (typeof statsOptions === "string") {
  43. // eslint-disable-next-line no-param-reassign
  44. statsOptions = {
  45. preset: statsOptions
  46. };
  47. }
  48. return statsOptions;
  49. }
  50. function done(stats) {
  51. // We are now on valid state
  52. // eslint-disable-next-line no-param-reassign
  53. context.state = true; // eslint-disable-next-line no-param-reassign
  54. context.stats = stats; // Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
  55. process.nextTick(() => {
  56. const {
  57. compiler,
  58. logger,
  59. options,
  60. state,
  61. callbacks
  62. } = context; // Check if still in valid state
  63. if (!state) {
  64. return;
  65. }
  66. logger.log("Compilation finished");
  67. const isMultiCompilerMode = Boolean(compiler.compilers);
  68. let statsOptions;
  69. if (typeof options.stats !== "undefined") {
  70. statsOptions = isMultiCompilerMode ? {
  71. children: compiler.compilers.map(() => options.stats)
  72. } : options.stats;
  73. } else {
  74. statsOptions = isMultiCompilerMode ? {
  75. children: compiler.compilers.map(child => child.options.stats)
  76. } : compiler.options.stats;
  77. }
  78. if (isMultiCompilerMode) {
  79. statsOptions.children = statsOptions.children.map(childStatsOptions => {
  80. // eslint-disable-next-line no-param-reassign
  81. childStatsOptions = normalizeStatsOptions(childStatsOptions);
  82. if (typeof childStatsOptions.colors === "undefined") {
  83. // eslint-disable-next-line no-param-reassign
  84. childStatsOptions.colors = Boolean(_colorette.default.options.enabled);
  85. }
  86. return childStatsOptions;
  87. });
  88. } else {
  89. statsOptions = normalizeStatsOptions(statsOptions);
  90. if (typeof statsOptions.colors === "undefined") {
  91. statsOptions.colors = Boolean(_colorette.default.options.enabled);
  92. }
  93. } // TODO webpack@4 doesn't support `{ children: [{ colors: true }, { colors: true }] }` for stats
  94. if (compiler.compilers && statsForWebpack4) {
  95. statsOptions.colors = statsOptions.children.some(child => child.colors);
  96. }
  97. const printedStats = stats.toString(statsOptions); // Avoid extra empty line when `stats: 'none'`
  98. if (printedStats) {
  99. // eslint-disable-next-line no-console
  100. console.log(printedStats);
  101. } // eslint-disable-next-line no-param-reassign
  102. context.callbacks = []; // Execute callback that are delayed
  103. callbacks.forEach(callback => {
  104. callback(stats);
  105. });
  106. });
  107. }
  108. context.compiler.hooks.watchRun.tap("webpack-dev-middleware", invalid);
  109. context.compiler.hooks.invalid.tap("webpack-dev-middleware", invalid);
  110. (context.compiler.webpack ? context.compiler.hooks.afterDone : context.compiler.hooks.done).tap("webpack-dev-middleware", done);
  111. }