1 |
|
2 |
|
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 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | var status = {
|
34 | isUnloading: false,
|
35 |
|
36 |
|
37 | currentHash: typeof __webpack_hash__ !== "undefined" ? __webpack_hash__ : ""
|
38 | };
|
39 |
|
40 |
|
41 | var options = {
|
42 | hot: false,
|
43 | liveReload: false,
|
44 | progress: false,
|
45 | overlay: false
|
46 | };
|
47 | var parsedResourceQuery = parseURL(__resourceQuery);
|
48 |
|
49 | if (parsedResourceQuery.hot === "true") {
|
50 | options.hot = true;
|
51 | log.info("Hot Module Replacement enabled.");
|
52 | }
|
53 |
|
54 | if (parsedResourceQuery["live-reload"] === "true") {
|
55 | options.liveReload = true;
|
56 | log.info("Live Reloading enabled.");
|
57 | }
|
58 |
|
59 | if (parsedResourceQuery.logging) {
|
60 | options.logging = parsedResourceQuery.logging;
|
61 | }
|
62 |
|
63 | if (typeof parsedResourceQuery.reconnect !== "undefined") {
|
64 | options.reconnect = Number(parsedResourceQuery.reconnect);
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | function setAllLogLevel(level) {
|
72 |
|
73 | webpackHotLog.setLogLevel(level === "verbose" || level === "log" ? "info" : level);
|
74 | setLogLevel(level);
|
75 | }
|
76 |
|
77 | if (options.logging) {
|
78 | setAllLogLevel(options.logging);
|
79 | }
|
80 |
|
81 | self.addEventListener("beforeunload", function () {
|
82 | status.isUnloading = true;
|
83 | });
|
84 | var 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...");
|
103 |
|
104 | if (options.overlay) {
|
105 | hide();
|
106 | }
|
107 |
|
108 | sendMessage("Invalid");
|
109 | },
|
110 |
|
111 | |
112 |
|
113 |
|
114 | hash: function hash(_hash) {
|
115 | status.previousHash = status.currentHash;
|
116 | status.currentHash = _hash;
|
117 | },
|
118 | logging: setAllLogLevel,
|
119 |
|
120 | |
121 |
|
122 |
|
123 | overlay: function overlay(value) {
|
124 | if (typeof document === "undefined") {
|
125 | return;
|
126 | }
|
127 |
|
128 | options.overlay = value;
|
129 | },
|
130 |
|
131 | |
132 |
|
133 |
|
134 | reconnect: function reconnect(value) {
|
135 | if (parsedResourceQuery.reconnect === "false") {
|
136 | return;
|
137 | }
|
138 |
|
139 | options.reconnect = value;
|
140 | },
|
141 |
|
142 | |
143 |
|
144 |
|
145 | progress: function progress(value) {
|
146 | options.progress = value;
|
147 | },
|
148 |
|
149 | |
150 |
|
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 |
|
178 |
|
179 | |
180 |
|
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 |
|
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 |
|
197 |
|
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 |
|
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 |
|
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 | };
|
274 | var socketURL = createSocketURL(parsedResourceQuery);
|
275 | socket(socketURL, onSocketMessage, options.reconnect); |
\ | No newline at end of file |