1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | var stripAnsi = require('strip-ansi');
|
20 | var url = require('url');
|
21 | var launchEditorEndpoint = require('./launchEditorEndpoint');
|
22 | var formatWebpackMessages = require('./formatWebpackMessages');
|
23 | var ErrorOverlay = require('react-error-overlay');
|
24 |
|
25 | ErrorOverlay.setEditorHandler(function editorHandler(errorLocation) {
|
26 |
|
27 | fetch(
|
28 | launchEditorEndpoint +
|
29 | '?fileName=' +
|
30 | window.encodeURIComponent(errorLocation.fileName) +
|
31 | '&lineNumber=' +
|
32 | window.encodeURIComponent(errorLocation.lineNumber || 1) +
|
33 | '&colNumber=' +
|
34 | window.encodeURIComponent(errorLocation.colNumber || 1)
|
35 | );
|
36 | });
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | var hadRuntimeError = false;
|
45 | ErrorOverlay.startReportingRuntimeErrors({
|
46 | onError: function () {
|
47 | hadRuntimeError = true;
|
48 | },
|
49 | filename: '/static/js/bundle.js',
|
50 | });
|
51 |
|
52 | if (module.hot && typeof module.hot.dispose === 'function') {
|
53 | module.hot.dispose(function () {
|
54 |
|
55 | ErrorOverlay.stopReportingRuntimeErrors();
|
56 | });
|
57 | }
|
58 |
|
59 |
|
60 | var connection = new WebSocket(
|
61 | url.format({
|
62 | protocol: window.location.protocol === 'https:' ? 'wss' : 'ws',
|
63 | hostname: process.env.WDS_SOCKET_HOST || window.location.hostname,
|
64 | port: process.env.WDS_SOCKET_PORT || window.location.port,
|
65 |
|
66 | pathname: process.env.WDS_SOCKET_PATH || '/sockjs-node',
|
67 | slashes: true,
|
68 | })
|
69 | );
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | connection.onclose = function () {
|
75 | if (typeof console !== 'undefined' && typeof console.info === 'function') {
|
76 | console.info(
|
77 | 'The development server has disconnected.\nRefresh the page if necessary.'
|
78 | );
|
79 | }
|
80 | };
|
81 |
|
82 |
|
83 | var isFirstCompilation = true;
|
84 | var mostRecentCompilationHash = null;
|
85 | var hasCompileErrors = false;
|
86 |
|
87 | function clearOutdatedErrors() {
|
88 |
|
89 | if (typeof console !== 'undefined' && typeof console.clear === 'function') {
|
90 | if (hasCompileErrors) {
|
91 | console.clear();
|
92 | }
|
93 | }
|
94 | }
|
95 |
|
96 |
|
97 | function handleSuccess() {
|
98 | clearOutdatedErrors();
|
99 |
|
100 | var isHotUpdate = !isFirstCompilation;
|
101 | isFirstCompilation = false;
|
102 | hasCompileErrors = false;
|
103 |
|
104 |
|
105 | if (isHotUpdate) {
|
106 | tryApplyUpdates(function onHotUpdateSuccess() {
|
107 |
|
108 |
|
109 | tryDismissErrorOverlay();
|
110 | });
|
111 | }
|
112 | }
|
113 |
|
114 |
|
115 | function handleWarnings(warnings) {
|
116 | clearOutdatedErrors();
|
117 |
|
118 | var isHotUpdate = !isFirstCompilation;
|
119 | isFirstCompilation = false;
|
120 | hasCompileErrors = false;
|
121 |
|
122 | function printWarnings() {
|
123 |
|
124 | var formatted = formatWebpackMessages({
|
125 | warnings: warnings,
|
126 | errors: [],
|
127 | });
|
128 |
|
129 | if (typeof console !== 'undefined' && typeof console.warn === 'function') {
|
130 | for (var i = 0; i < formatted.warnings.length; i++) {
|
131 | if (i === 5) {
|
132 | console.warn(
|
133 | 'There were more warnings in other files.\n' +
|
134 | 'You can find a complete log in the terminal.'
|
135 | );
|
136 | break;
|
137 | }
|
138 | console.warn(stripAnsi(formatted.warnings[i]));
|
139 | }
|
140 | }
|
141 | }
|
142 |
|
143 | printWarnings();
|
144 |
|
145 |
|
146 | if (isHotUpdate) {
|
147 | tryApplyUpdates(function onSuccessfulHotUpdate() {
|
148 |
|
149 |
|
150 | tryDismissErrorOverlay();
|
151 | });
|
152 | }
|
153 | }
|
154 |
|
155 |
|
156 | function handleErrors(errors) {
|
157 | clearOutdatedErrors();
|
158 |
|
159 | isFirstCompilation = false;
|
160 | hasCompileErrors = true;
|
161 |
|
162 |
|
163 | var formatted = formatWebpackMessages({
|
164 | errors: errors,
|
165 | warnings: [],
|
166 | });
|
167 |
|
168 |
|
169 | ErrorOverlay.reportBuildError(formatted.errors[0]);
|
170 |
|
171 |
|
172 | if (typeof console !== 'undefined' && typeof console.error === 'function') {
|
173 | for (var i = 0; i < formatted.errors.length; i++) {
|
174 | console.error(stripAnsi(formatted.errors[i]));
|
175 | }
|
176 | }
|
177 |
|
178 |
|
179 |
|
180 | }
|
181 |
|
182 | function tryDismissErrorOverlay() {
|
183 | if (!hasCompileErrors) {
|
184 | ErrorOverlay.dismissBuildError();
|
185 | }
|
186 | }
|
187 |
|
188 |
|
189 | function handleAvailableHash(hash) {
|
190 |
|
191 | mostRecentCompilationHash = hash;
|
192 | }
|
193 |
|
194 |
|
195 | connection.onmessage = function (e) {
|
196 | var message = JSON.parse(e.data);
|
197 | switch (message.type) {
|
198 | case 'hash':
|
199 | handleAvailableHash(message.data);
|
200 | break;
|
201 | case 'still-ok':
|
202 | case 'ok':
|
203 | handleSuccess();
|
204 | break;
|
205 | case 'content-changed':
|
206 |
|
207 | window.location.reload();
|
208 | break;
|
209 | case 'warnings':
|
210 | handleWarnings(message.data);
|
211 | break;
|
212 | case 'errors':
|
213 | handleErrors(message.data);
|
214 | break;
|
215 | default:
|
216 |
|
217 | }
|
218 | };
|
219 |
|
220 |
|
221 | function isUpdateAvailable() {
|
222 |
|
223 |
|
224 |
|
225 | return mostRecentCompilationHash !== __webpack_hash__;
|
226 | }
|
227 |
|
228 |
|
229 | function canApplyUpdates() {
|
230 | return module.hot.status() === 'idle';
|
231 | }
|
232 |
|
233 |
|
234 | function tryApplyUpdates(onHotUpdateSuccess) {
|
235 | if (!module.hot) {
|
236 |
|
237 | window.location.reload();
|
238 | return;
|
239 | }
|
240 |
|
241 | if (!isUpdateAvailable() || !canApplyUpdates()) {
|
242 | return;
|
243 | }
|
244 |
|
245 | function handleApplyUpdates(err, updatedModules) {
|
246 |
|
247 | const hasReactRefresh = process.env.FAST_REFRESH;
|
248 | const wantsForcedReload = err || !updatedModules || hadRuntimeError;
|
249 |
|
250 | if (!hasReactRefresh && wantsForcedReload) {
|
251 | window.location.reload();
|
252 | return;
|
253 | }
|
254 |
|
255 | if (typeof onHotUpdateSuccess === 'function') {
|
256 |
|
257 | onHotUpdateSuccess();
|
258 | }
|
259 |
|
260 | if (isUpdateAvailable()) {
|
261 |
|
262 | tryApplyUpdates();
|
263 | }
|
264 | }
|
265 |
|
266 |
|
267 | var result = module.hot.check( true, handleApplyUpdates);
|
268 |
|
269 |
|
270 | if (result && result.then) {
|
271 | result.then(
|
272 | function (updatedModules) {
|
273 | handleApplyUpdates(null, updatedModules);
|
274 | },
|
275 | function (err) {
|
276 | handleApplyUpdates(err, null);
|
277 | }
|
278 | );
|
279 | }
|
280 | }
|