UNPKG

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