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