UNPKG

11.2 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
9
10'use strict';
11
12// This alternative WebpackDevServer combines the functionality of:
13// https://github.com/webpack/webpack-dev-server/blob/webpack-1/client/index.js
14// https://github.com/webpack/webpack/blob/webpack-1/hot/dev-server.js
15
16// It only supports their simplest configuration (hot updates on same server).
17// It makes some opinionated choices on top, like adding a syntax error overlay
18// that looks similar to our console output. The error overlay is inspired by:
19// https://github.com/glenjamin/webpack-hot-middleware
20
21//var SockJS = require('sockjs-client');
22
23var stripAnsi = require('strip-ansi');
24var url = require('url');
25var formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
26var Entities = require('html-entities').AllHtmlEntities;
27var ansiHTML = require('react-dev-utils/ansiHTML');
28var entities = new Entities();
29var querystring = require('querystring');
30var options = querystring.parse(__resourceQuery.slice(1));
31
32function createOverlayIframe(onIframeLoad) {
33 var iframe = document.createElement('iframe');
34 iframe.id = 'react-dev-utils-webpack-hot-dev-client-overlay';
35 iframe.src = 'about:blank';
36 iframe.style.position = 'fixed';
37 iframe.style.left = 0;
38 iframe.style.top = 0;
39 iframe.style.right = 0;
40 iframe.style.bottom = 0;
41 iframe.style.width = '100vw';
42 iframe.style.height = '100vh';
43 iframe.style.border = 'none';
44 iframe.style.zIndex = 2147483647;
45 iframe.onload = onIframeLoad;
46 return iframe;
47}
48
49function addOverlayDivTo(iframe) {
50 // TODO: unify these styles with react-error-overlay
51 iframe.contentDocument.body.style.margin = 0;
52 iframe.contentDocument.body.style.maxWidth = '100vw';
53
54 var outerDiv = iframe.contentDocument.createElement('div');
55 outerDiv.id = 'react-dev-utils-webpack-hot-dev-client-overlay-div';
56 outerDiv.style.width = '100%';
57 outerDiv.style.height = '100%';
58 outerDiv.style.boxSizing = 'border-box';
59 outerDiv.style.textAlign = 'center';
60 outerDiv.style.backgroundColor = 'rgb(255, 255, 255)';
61
62 var div = iframe.contentDocument.createElement('div');
63 div.style.position = 'relative';
64 div.style.display = 'inline-flex';
65 div.style.flexDirection = 'column';
66 div.style.height = '100%';
67 div.style.width = '1024px';
68 div.style.maxWidth = '100%';
69 div.style.overflowX = 'hidden';
70 div.style.overflowY = 'auto';
71 div.style.padding = '0.5rem';
72 div.style.boxSizing = 'border-box';
73 div.style.textAlign = 'left';
74 div.style.fontFamily = 'Consolas, Menlo, monospace';
75 div.style.fontSize = '11px';
76 div.style.whiteSpace = 'pre-wrap';
77 div.style.wordBreak = 'break-word';
78 div.style.lineHeight = '1.5';
79 div.style.color = 'rgb(41, 50, 56)';
80
81 outerDiv.appendChild(div);
82 iframe.contentDocument.body.appendChild(outerDiv);
83 return div;
84}
85
86function overlayHeaderStyle() {
87 return 'font-size: 2em;' + 'font-family: sans-serif;' + 'color: rgb(206, 17, 38);' + 'white-space: pre-wrap;' + 'margin: 0 2rem 0.75rem 0px;' + 'flex: 0 0 auto;' + 'max-height: 35%;' + 'overflow: auto;';
88}
89
90var overlayIframe = null;
91var overlayDiv = null;
92var lastOnOverlayDivReady = null;
93
94function ensureOverlayDivExists(onOverlayDivReady) {
95 if (overlayDiv) {
96 // Everything is ready, call the callback right away.
97 onOverlayDivReady(overlayDiv);
98 return;
99 }
100
101 // Creating an iframe may be asynchronous so we'll schedule the callback.
102 // In case of multiple calls, last callback wins.
103 lastOnOverlayDivReady = onOverlayDivReady;
104
105 if (overlayIframe) {
106 // We're already creating it.
107 return;
108 }
109
110 // Create iframe and, when it is ready, a div inside it.
111 overlayIframe = createOverlayIframe(function onIframeLoad() {
112 overlayDiv = addOverlayDivTo(overlayIframe);
113 // Now we can talk!
114 lastOnOverlayDivReady(overlayDiv);
115 });
116
117 // Zalgo alert: onIframeLoad() will be called either synchronously
118 // or asynchronously depending on the browser.
119 // We delay adding it so `overlayIframe` is set when `onIframeLoad` fires.
120 document.body.appendChild(overlayIframe);
121}
122
123function showErrorOverlay(message) {
124 ensureOverlayDivExists(function onOverlayDivReady(overlayDiv) {
125 // TODO: unify this with our runtime overlay
126 overlayDiv.innerHTML = '<div style="' + overlayHeaderStyle() + '">Failed to compile</div>' + '<pre style="' + 'display: block; padding: 0.5em; margin-top: 0; ' + 'margin-bottom: 0.5em; overflow-x: auto; white-space: pre-wrap; ' + 'border-radius: 0.25rem; background-color: rgba(206, 17, 38, 0.05)">' + '<code style="font-family: Consolas, Menlo, monospace;">' + ansiHTML(entities.encode(message)) + '</code></pre>' + '<div style="' + 'font-family: sans-serif; color: rgb(135, 142, 145); margin-top: 0.5rem; ' + 'flex: 0 0 auto">' + 'This error occurred during the build time and cannot be dismissed.</div>';
127 });
128}
129
130function destroyErrorOverlay() {
131 if (!overlayDiv) {
132 // It is not there in the first place.
133 return;
134 }
135
136 // Clean up and reset internal state.
137 document.body.removeChild(overlayIframe);
138 overlayDiv = null;
139 overlayIframe = null;
140 lastOnOverlayDivReady = null;
141}
142
143// Connect to WebpackDevServer via a socket.
144/*var connection = new SockJS(
145 url.format({
146 protocol: window.location.protocol,
147 hostname: window.location.hostname,
148 port: window.location.port,
149 // Hardcoded in WebpackDevServer
150 pathname: '/sockjs-node',
151 })
152);*/
153var connection = new window.EventSource(options.hmrPath + '/sockjs-node/info');
154// Unlike WebpackDevServer client, we won't try to reconnect
155// to avoid spamming the console. Disconnect usually happens
156// when developer stops the server.
157connection.onclose = function () {
158 if (typeof console !== 'undefined' && typeof console.info === 'function') {
159 console.info('The development server has disconnected.\nRefresh the page if necessary.');
160 }
161};
162
163// Remember some state related to hot module replacement.
164var isFirstCompilation = true;
165var mostRecentCompilationHash = null;
166var hasCompileErrors = false;
167
168function clearOutdatedErrors() {
169 // Clean up outdated compile errors, if any.
170 if (typeof console !== 'undefined' && typeof console.clear === 'function') {
171 if (hasCompileErrors) {
172 console.clear();
173 }
174 }
175}
176
177// Successful compilation.
178function handleSuccess() {
179 clearOutdatedErrors();
180
181 var isHotUpdate = !isFirstCompilation;
182 isFirstCompilation = false;
183 hasCompileErrors = false;
184
185 // Attempt to apply hot updates or reload.
186 if (isHotUpdate) {
187 tryApplyUpdates(function onHotUpdateSuccess() {
188 // Only destroy it when we're sure it's a hot update.
189 // Otherwise it would flicker right before the reload.
190 destroyErrorOverlay();
191 });
192 }
193}
194
195// Compilation with warnings (e.g. ESLint).
196function handleWarnings(warnings) {
197 clearOutdatedErrors();
198
199 var isHotUpdate = !isFirstCompilation;
200 isFirstCompilation = false;
201 hasCompileErrors = false;
202
203 function printWarnings() {
204 // Print warnings to the console.
205 var formatted = formatWebpackMessages({
206 warnings: warnings,
207 errors: []
208 });
209
210 if (typeof console !== 'undefined' && typeof console.warn === 'function') {
211 for (var i = 0; i < formatted.warnings.length; i++) {
212 if (i === 5) {
213 console.warn('There were more warnings in other files.\n' + 'You can find a complete log in the terminal.');
214 break;
215 }
216 console.warn(stripAnsi(formatted.warnings[i]));
217 }
218 }
219 }
220
221 // Attempt to apply hot updates or reload.
222 if (isHotUpdate) {
223 tryApplyUpdates(function onSuccessfulHotUpdate() {
224 // Only print warnings if we aren't refreshing the page.
225 // Otherwise they'll disappear right away anyway.
226 printWarnings();
227 // Only destroy it when we're sure it's a hot update.
228 // Otherwise it would flicker right before the reload.
229 destroyErrorOverlay();
230 });
231 } else {
232 // Print initial warnings immediately.
233 printWarnings();
234 }
235}
236
237// Compilation with errors (e.g. syntax error or missing modules).
238function handleErrors(errors) {
239 clearOutdatedErrors();
240
241 isFirstCompilation = false;
242 hasCompileErrors = true;
243
244 // "Massage" webpack messages.
245 var formatted = formatWebpackMessages({
246 errors: errors,
247 warnings: []
248 });
249
250 // Only show the first error.
251 showErrorOverlay(formatted.errors[0]);
252
253 // Also log them to the console.
254 if (typeof console !== 'undefined' && typeof console.error === 'function') {
255 for (var i = 0; i < formatted.errors.length; i++) {
256 console.error(stripAnsi(formatted.errors[i]));
257 }
258 }
259
260 // Do not attempt to reload now.
261 // We will reload on next success instead.
262}
263
264// There is a newer version of the code available.
265function handleAvailableHash(hash) {
266 // Update last known compilation hash.
267 mostRecentCompilationHash = hash;
268}
269
270// Handle messages from the server.
271connection.onmessage = function (e) {
272 var message = JSON.parse(e.data);
273 switch (message.type) {
274 case 'hash':
275 handleAvailableHash(message.data);
276 break;
277 case 'still-ok':
278 case 'ok':
279 handleSuccess();
280 break;
281 case 'content-changed':
282 // Triggered when a file from `contentBase` changed.
283 window.location.reload();
284 break;
285 case 'warnings':
286 handleWarnings(message.data);
287 break;
288 case 'errors':
289 handleErrors(message.data);
290 break;
291 default:
292 // Do nothing.
293 }
294};
295
296// Is there a newer version of this code available?
297function isUpdateAvailable() {
298 /* globals __webpack_hash__ */
299 // __webpack_hash__ is the hash of the current compilation.
300 // It's a global variable injected by Webpack.
301 return mostRecentCompilationHash !== __webpack_hash__;
302}
303
304// Webpack disallows updates in other states.
305function canApplyUpdates() {
306 return module.hot.status() === 'idle';
307}
308
309// Attempt to update code on the fly, fall back to a hard reload.
310function tryApplyUpdates(onHotUpdateSuccess) {
311 if (!module.hot) {
312 // HotModuleReplacementPlugin is not in Webpack configuration.
313 window.location.reload();
314 return;
315 }
316
317 if (!isUpdateAvailable() || !canApplyUpdates()) {
318 return;
319 }
320
321 function handleApplyUpdates(err, updatedModules) {
322 if (err || !updatedModules) {
323 window.location.reload();
324 return;
325 }
326
327 if (typeof onHotUpdateSuccess === 'function') {
328 // Maybe we want to do something.
329 onHotUpdateSuccess();
330 }
331
332 if (isUpdateAvailable()) {
333 // While we were updating, there was a new update! Do it again.
334 tryApplyUpdates();
335 }
336 }
337
338 // https://webpack.github.io/docs/hot-module-replacement.html#check
339 var result = module.hot.check( /* autoApply */true, handleApplyUpdates);
340
341 // // Webpack 2 returns a Promise instead of invoking a callback
342 if (result && result.then) {
343 result.then(function (updatedModules) {
344 handleApplyUpdates(null, updatedModules);
345 }, function (err) {
346 handleApplyUpdates(err, null);
347 });
348 }
349}
\No newline at end of file