index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* global __resourceQuery, __webpack_hash__ */
  2. /// <reference types="webpack/module" />
  3. import webpackHotLog from "webpack/hot/log.js";
  4. import stripAnsi from "./utils/stripAnsi.js";
  5. import parseURL from "./utils/parseURL.js";
  6. import socket from "./socket.js";
  7. import { formatProblem, show, hide } from "./overlay.js";
  8. import { log, setLogLevel } from "./utils/log.js";
  9. import sendMessage from "./utils/sendMessage.js";
  10. import reloadApp from "./utils/reloadApp.js";
  11. import createSocketURL from "./utils/createSocketURL.js";
  12. /**
  13. * @typedef {Object} Options
  14. * @property {boolean} hot
  15. * @property {boolean} liveReload
  16. * @property {boolean} progress
  17. * @property {boolean | { warnings?: boolean, errors?: boolean, trustedTypesPolicyName?: string }} overlay
  18. * @property {string} [logging]
  19. * @property {number} [reconnect]
  20. */
  21. /**
  22. * @typedef {Object} Status
  23. * @property {boolean} isUnloading
  24. * @property {string} currentHash
  25. * @property {string} [previousHash]
  26. */
  27. /**
  28. * @type {Status}
  29. */
  30. var status = {
  31. isUnloading: false,
  32. // TODO Workaround for webpack v4, `__webpack_hash__` is not replaced without HotModuleReplacement
  33. // eslint-disable-next-line camelcase
  34. currentHash: typeof __webpack_hash__ !== "undefined" ? __webpack_hash__ : ""
  35. };
  36. /** @type {Options} */
  37. var options = {
  38. hot: false,
  39. liveReload: false,
  40. progress: false,
  41. overlay: false
  42. };
  43. var parsedResourceQuery = parseURL(__resourceQuery);
  44. if (parsedResourceQuery.hot === "true") {
  45. options.hot = true;
  46. log.info("Hot Module Replacement enabled.");
  47. }
  48. if (parsedResourceQuery["live-reload"] === "true") {
  49. options.liveReload = true;
  50. log.info("Live Reloading enabled.");
  51. }
  52. if (parsedResourceQuery.logging) {
  53. options.logging = parsedResourceQuery.logging;
  54. }
  55. if (typeof parsedResourceQuery.reconnect !== "undefined") {
  56. options.reconnect = Number(parsedResourceQuery.reconnect);
  57. }
  58. /**
  59. * @param {string} level
  60. */
  61. function setAllLogLevel(level) {
  62. // This is needed because the HMR logger operate separately from dev server logger
  63. webpackHotLog.setLogLevel(level === "verbose" || level === "log" ? "info" : level);
  64. setLogLevel(level);
  65. }
  66. if (options.logging) {
  67. setAllLogLevel(options.logging);
  68. }
  69. self.addEventListener("beforeunload", function () {
  70. status.isUnloading = true;
  71. });
  72. var onSocketMessage = {
  73. hot: function hot() {
  74. if (parsedResourceQuery.hot === "false") {
  75. return;
  76. }
  77. options.hot = true;
  78. log.info("Hot Module Replacement enabled.");
  79. },
  80. liveReload: function liveReload() {
  81. if (parsedResourceQuery["live-reload"] === "false") {
  82. return;
  83. }
  84. options.liveReload = true;
  85. log.info("Live Reloading enabled.");
  86. },
  87. invalid: function invalid() {
  88. log.info("App updated. Recompiling..."); // Fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
  89. if (options.overlay) {
  90. hide();
  91. }
  92. sendMessage("Invalid");
  93. },
  94. /**
  95. * @param {string} hash
  96. */
  97. hash: function hash(_hash) {
  98. status.previousHash = status.currentHash;
  99. status.currentHash = _hash;
  100. },
  101. logging: setAllLogLevel,
  102. /**
  103. * @param {boolean} value
  104. */
  105. overlay: function overlay(value) {
  106. if (typeof document === "undefined") {
  107. return;
  108. }
  109. options.overlay = value;
  110. },
  111. /**
  112. * @param {number} value
  113. */
  114. reconnect: function reconnect(value) {
  115. if (parsedResourceQuery.reconnect === "false") {
  116. return;
  117. }
  118. options.reconnect = value;
  119. },
  120. /**
  121. * @param {boolean} value
  122. */
  123. progress: function progress(value) {
  124. options.progress = value;
  125. },
  126. /**
  127. * @param {{ pluginName?: string, percent: number, msg: string }} data
  128. */
  129. "progress-update": function progressUpdate(data) {
  130. if (options.progress) {
  131. log.info("".concat(data.pluginName ? "[".concat(data.pluginName, "] ") : "").concat(data.percent, "% - ").concat(data.msg, "."));
  132. }
  133. sendMessage("Progress", data);
  134. },
  135. "still-ok": function stillOk() {
  136. log.info("Nothing changed.");
  137. if (options.overlay) {
  138. hide();
  139. }
  140. sendMessage("StillOk");
  141. },
  142. ok: function ok() {
  143. sendMessage("Ok");
  144. if (options.overlay) {
  145. hide();
  146. }
  147. reloadApp(options, status);
  148. },
  149. // TODO: remove in v5 in favor of 'static-changed'
  150. /**
  151. * @param {string} file
  152. */
  153. "content-changed": function contentChanged(file) {
  154. log.info("".concat(file ? "\"".concat(file, "\"") : "Content", " from static directory was changed. Reloading..."));
  155. self.location.reload();
  156. },
  157. /**
  158. * @param {string} file
  159. */
  160. "static-changed": function staticChanged(file) {
  161. log.info("".concat(file ? "\"".concat(file, "\"") : "Content", " from static directory was changed. Reloading..."));
  162. self.location.reload();
  163. },
  164. /**
  165. * @param {Error[]} warnings
  166. * @param {any} params
  167. */
  168. warnings: function warnings(_warnings, params) {
  169. log.warn("Warnings while compiling.");
  170. var printableWarnings = _warnings.map(function (error) {
  171. var _formatProblem = formatProblem("warning", error),
  172. header = _formatProblem.header,
  173. body = _formatProblem.body;
  174. return "".concat(header, "\n").concat(stripAnsi(body));
  175. });
  176. sendMessage("Warnings", printableWarnings);
  177. for (var i = 0; i < printableWarnings.length; i++) {
  178. log.warn(printableWarnings[i]);
  179. }
  180. var needShowOverlayForWarnings = typeof options.overlay === "boolean" ? options.overlay : options.overlay && options.overlay.warnings;
  181. if (needShowOverlayForWarnings) {
  182. var trustedTypesPolicyName = typeof options.overlay === "object" && options.overlay.trustedTypesPolicyName;
  183. show("warning", _warnings, trustedTypesPolicyName || null);
  184. }
  185. if (params && params.preventReloading) {
  186. return;
  187. }
  188. reloadApp(options, status);
  189. },
  190. /**
  191. * @param {Error[]} errors
  192. */
  193. errors: function errors(_errors) {
  194. log.error("Errors while compiling. Reload prevented.");
  195. var printableErrors = _errors.map(function (error) {
  196. var _formatProblem2 = formatProblem("error", error),
  197. header = _formatProblem2.header,
  198. body = _formatProblem2.body;
  199. return "".concat(header, "\n").concat(stripAnsi(body));
  200. });
  201. sendMessage("Errors", printableErrors);
  202. for (var i = 0; i < printableErrors.length; i++) {
  203. log.error(printableErrors[i]);
  204. }
  205. var needShowOverlayForErrors = typeof options.overlay === "boolean" ? options.overlay : options.overlay && options.overlay.errors;
  206. if (needShowOverlayForErrors) {
  207. var trustedTypesPolicyName = typeof options.overlay === "object" && options.overlay.trustedTypesPolicyName;
  208. show("error", _errors, trustedTypesPolicyName || null);
  209. }
  210. },
  211. /**
  212. * @param {Error} error
  213. */
  214. error: function error(_error) {
  215. log.error(_error);
  216. },
  217. close: function close() {
  218. log.info("Disconnected!");
  219. if (options.overlay) {
  220. hide();
  221. }
  222. sendMessage("Close");
  223. }
  224. };
  225. var socketURL = createSocketURL(parsedResourceQuery);
  226. socket(socketURL, onSocketMessage, options.reconnect);