index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. 'use strict';
  2. /* global __resourceQuery WorkerGlobalScope self */
  3. /* eslint prefer-destructuring: off */
  4. var stripAnsi = require('strip-ansi');
  5. var socket = require('./socket');
  6. var overlay = require('./overlay');
  7. var _require = require('./utils/log'),
  8. log = _require.log,
  9. setLogLevel = _require.setLogLevel;
  10. var sendMessage = require('./utils/sendMessage');
  11. var reloadApp = require('./utils/reloadApp');
  12. var createSocketUrl = require('./utils/createSocketUrl');
  13. var status = {
  14. isUnloading: false,
  15. currentHash: ''
  16. };
  17. var options = {
  18. hot: false,
  19. hotReload: true,
  20. liveReload: false,
  21. initial: true,
  22. useWarningOverlay: false,
  23. useErrorOverlay: false,
  24. useProgress: false
  25. };
  26. var socketUrl = createSocketUrl(__resourceQuery);
  27. self.addEventListener('beforeunload', function () {
  28. status.isUnloading = true;
  29. });
  30. if (typeof window !== 'undefined') {
  31. var qs = window.location.search.toLowerCase();
  32. options.hotReload = qs.indexOf('hotreload=false') === -1;
  33. }
  34. var onSocketMessage = {
  35. hot: function hot() {
  36. options.hot = true;
  37. log.info('[WDS] Hot Module Replacement enabled.');
  38. },
  39. liveReload: function liveReload() {
  40. options.liveReload = true;
  41. log.info('[WDS] Live Reloading enabled.');
  42. },
  43. invalid: function invalid() {
  44. log.info('[WDS] App updated. Recompiling...'); // fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
  45. if (options.useWarningOverlay || options.useErrorOverlay) {
  46. overlay.clear();
  47. }
  48. sendMessage('Invalid');
  49. },
  50. hash: function hash(_hash) {
  51. status.currentHash = _hash;
  52. },
  53. 'still-ok': function stillOk() {
  54. log.info('[WDS] Nothing changed.');
  55. if (options.useWarningOverlay || options.useErrorOverlay) {
  56. overlay.clear();
  57. }
  58. sendMessage('StillOk');
  59. },
  60. 'log-level': function logLevel(level) {
  61. var hotCtx = require.context('webpack/hot', false, /^\.\/log$/);
  62. if (hotCtx.keys().indexOf('./log') !== -1) {
  63. hotCtx('./log').setLogLevel(level);
  64. }
  65. setLogLevel(level);
  66. },
  67. overlay: function overlay(value) {
  68. if (typeof document !== 'undefined') {
  69. if (typeof value === 'boolean') {
  70. options.useWarningOverlay = false;
  71. options.useErrorOverlay = value;
  72. } else if (value) {
  73. options.useWarningOverlay = value.warnings;
  74. options.useErrorOverlay = value.errors;
  75. }
  76. }
  77. },
  78. progress: function progress(_progress) {
  79. if (typeof document !== 'undefined') {
  80. options.useProgress = _progress;
  81. }
  82. },
  83. 'progress-update': function progressUpdate(data) {
  84. if (options.useProgress) {
  85. log.info("[WDS] ".concat(data.percent, "% - ").concat(data.msg, "."));
  86. }
  87. sendMessage('Progress', data);
  88. },
  89. ok: function ok() {
  90. sendMessage('Ok');
  91. if (options.useWarningOverlay || options.useErrorOverlay) {
  92. overlay.clear();
  93. }
  94. if (options.initial) {
  95. return options.initial = false;
  96. } // eslint-disable-line no-return-assign
  97. reloadApp(options, status);
  98. },
  99. 'content-changed': function contentChanged() {
  100. log.info('[WDS] Content base changed. Reloading...');
  101. self.location.reload();
  102. },
  103. warnings: function warnings(_warnings) {
  104. log.warn('[WDS] Warnings while compiling.');
  105. var strippedWarnings = _warnings.map(function (warning) {
  106. return stripAnsi(warning);
  107. });
  108. sendMessage('Warnings', strippedWarnings);
  109. for (var i = 0; i < strippedWarnings.length; i++) {
  110. log.warn(strippedWarnings[i]);
  111. }
  112. if (options.useWarningOverlay) {
  113. overlay.showMessage(_warnings);
  114. }
  115. if (options.initial) {
  116. return options.initial = false;
  117. } // eslint-disable-line no-return-assign
  118. reloadApp(options, status);
  119. },
  120. errors: function errors(_errors) {
  121. log.error('[WDS] Errors while compiling. Reload prevented.');
  122. var strippedErrors = _errors.map(function (error) {
  123. return stripAnsi(error);
  124. });
  125. sendMessage('Errors', strippedErrors);
  126. for (var i = 0; i < strippedErrors.length; i++) {
  127. log.error(strippedErrors[i]);
  128. }
  129. if (options.useErrorOverlay) {
  130. overlay.showMessage(_errors);
  131. }
  132. options.initial = false;
  133. },
  134. error: function error(_error) {
  135. log.error(_error);
  136. },
  137. close: function close() {
  138. log.error('[WDS] Disconnected!');
  139. sendMessage('Close');
  140. }
  141. };
  142. socket(socketUrl, onSocketMessage);