You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.3 KiB

4 years ago
  1. 'use strict';
  2. const weblog = require('webpack-log');
  3. module.exports = function ctx(compiler, options) {
  4. const context = {
  5. state: false,
  6. webpackStats: null,
  7. callbacks: [],
  8. options,
  9. compiler,
  10. watching: null,
  11. forceRebuild: false,
  12. };
  13. if (options.logger) {
  14. context.log = options.logger;
  15. } else {
  16. context.log = weblog({
  17. level: options.logLevel || 'info',
  18. name: 'wdm',
  19. timestamp: options.logTime,
  20. });
  21. }
  22. const { log } = context;
  23. function done(stats) {
  24. // We are now on valid state
  25. context.state = true;
  26. context.webpackStats = stats;
  27. // Do the stuff in nextTick, because bundle may be invalidated
  28. // if a change happened while compiling
  29. process.nextTick(() => {
  30. // check if still in valid state
  31. if (!context.state) {
  32. return;
  33. }
  34. // print webpack output
  35. context.options.reporter(context.options, {
  36. log,
  37. state: true,
  38. stats,
  39. });
  40. // execute callback that are delayed
  41. const cbs = context.callbacks;
  42. context.callbacks = [];
  43. cbs.forEach((cb) => {
  44. cb(stats);
  45. });
  46. });
  47. // In lazy mode, we may issue another rebuild
  48. if (context.forceRebuild) {
  49. context.forceRebuild = false;
  50. rebuild();
  51. }
  52. }
  53. function invalid(callback) {
  54. if (context.state) {
  55. context.options.reporter(context.options, {
  56. log,
  57. state: false,
  58. });
  59. }
  60. // We are now in invalid state
  61. context.state = false;
  62. if (typeof callback === 'function') {
  63. callback();
  64. }
  65. }
  66. function rebuild() {
  67. if (context.state) {
  68. context.state = false;
  69. context.compiler.run((err) => {
  70. if (err) {
  71. log.error(err.stack || err);
  72. if (err.details) {
  73. log.error(err.details);
  74. }
  75. }
  76. });
  77. } else {
  78. context.forceRebuild = true;
  79. }
  80. }
  81. context.rebuild = rebuild;
  82. context.compiler.hooks.invalid.tap('WebpackDevMiddleware', invalid);
  83. context.compiler.hooks.run.tap('WebpackDevMiddleware', invalid);
  84. context.compiler.hooks.done.tap('WebpackDevMiddleware', done);
  85. context.compiler.hooks.watchRun.tap(
  86. 'WebpackDevMiddleware',
  87. (comp, callback) => {
  88. invalid(callback);
  89. }
  90. );
  91. return context;
  92. };