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