mirror of
https://github.com/zhigang1992/create-react-app.git
synced 2026-01-12 22:46:30 +08:00
* Add red box prototype * Unmount fail node when hot-reloaded (future proofing) * Slightly improve error reason * Add Chrome unhandledrejection listener * Close red box when Escape is pressed * Construct our own source if not available * Resolve sourcemaps for friendlier traces * Allow error to be scrolled * Only utilize sourcemaps, not the magic. :( * Make view similar to React Native * Fix an uncaught syntax error * Add workaround for Firefox 50 * Add a hint for closing the prompt * Multiple error support When there's multiple errors, simply state that there are more instead of re-rendering * Log any renderer error * Dim node_modules in display * Override chrome stack trace limit * Magic: show me some source! * Add ability to toggle internal calls * Switch text from show -> hide * Change color scheme to something easier on the eyes * Change UI to be friendlier (thanks @nicinabox) https://github.com/facebookincubator/create-react-app/pull/1101#issuecomment-263621057 https://github.com/facebookincubator/create-react-app/pull/1101#issuecomment-263636171 * Update styles * Add container inside overlay * Apply pre style * Add line highlight * Add omitted frames ui * Move yellow to var * Do all function names in black * Adapt container based on screen size * Extract ansiHTML Use base16-github theme * Linting * Add syntax highlighting of errors * Linting * Remove white background * Append new files to package.json * Target exact version * White is a little harsh, use same color as red box. * Fix a bug where omitted frames would never be rendered (under certain circumstances) * Show local file path instead of confusing webpack:// * Do not require the entire file * Remove css file * Use context size, not a magic variable. * Fix title regression * Update dependency * Do not center text for internal calls * Temporarily disable links * Switch internal toggle to 'i' * Remove unnecessary rules from container style * Reduce omitted frames font size * Fix font family in pre > code * Re-introduce line highlighting * Object.<anonymous> -> (anonymous function) * Add ability to see script source * Add missing ansi mappings * Remove SIAF * Skip native functions * Extract hints div function * Extract renderers * Refacor var names * If source is disabled, don't use the line. * Allow toggle between source * Allow bundles to be expanded * Wow, how did I let this one slip? * Toggle text for UX/DX * Make it so clicking Close unmounts the error component * Do not allow hot reloading once an error is thrown * Do not wrap lines for small displays * Fix toggle when additional errors happen * Make the close a button * Capture and store errors * Dispose on render; move additional logic * Only make code not wrap * Switch to a view-by-index method * Allow user to switch between errors with arrow keys * Fix text while switching between errors * Update close button style * Render additional errors at top * Add left and right arrows * Make frames pointy * UTF-8 arrows * Don't mount unneeded DOM node * Switch to single changing text for compiled <-> source * Don't display arrows with only one error. * Collapsed and expanded * Make sure the last collapse toggle is appended * Do not show the stack trace if it doesn't resolve to a file we wrote * Style container with media queries * Do not allow x overflow; break words by default. * Trim off whitespace of lines * Remove padding since it's not outer-most * Add footer message * Extract css file to JS * Only inject the css when the overlay is up * Extract red variable * Remove env * Update babel-code-frame * Set force color to true * Extract out collapse div logic * Remove arrow field * Insert a top collapse * Make browser friendlier * Absolutify ^ * Make arrows buttons * Accessify * Let there be ES5 * Pretty css * Use forEach where we can ... * Remove extracted loop * Fix IE compatibility * Capture React warnings * Add source override via React output parsing * Whoops, fix arguments to crash. * es5-ify * Re-enable e2e-install directory test * Only rewrite line number if it was resolved and leaves a line at bottom * Rename failFast to crashOverlay * Disable console proxy * Appease linter * Appease more
314 lines
8.6 KiB
JavaScript
314 lines
8.6 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
// This alternative WebpackDevServer combines the functionality of:
|
|
// https://github.com/webpack/webpack-dev-server/blob/webpack-1/client/index.js
|
|
// https://github.com/webpack/webpack/blob/webpack-1/hot/dev-server.js
|
|
|
|
// It only supports their simplest configuration (hot updates on same server).
|
|
// It makes some opinionated choices on top, like adding a syntax error overlay
|
|
// that looks similar to our console output. The error overlay is inspired by:
|
|
// https://github.com/glenjamin/webpack-hot-middleware
|
|
|
|
var SockJS = require('sockjs-client');
|
|
var stripAnsi = require('strip-ansi');
|
|
var url = require('url');
|
|
var formatWebpackMessages = require('./formatWebpackMessages');
|
|
var Entities = require('html-entities').AllHtmlEntities;
|
|
var ansiHTML = require('./ansiHTML');
|
|
var entities = new Entities();
|
|
|
|
var red = '#E36049';
|
|
|
|
function createOverlayIframe(onIframeLoad) {
|
|
var iframe = document.createElement('iframe');
|
|
iframe.id = 'react-dev-utils-webpack-hot-dev-client-overlay';
|
|
iframe.src = 'about:blank';
|
|
iframe.style.position = 'fixed';
|
|
iframe.style.left = 0;
|
|
iframe.style.top = 0;
|
|
iframe.style.right = 0;
|
|
iframe.style.bottom = 0;
|
|
iframe.style.width = '100vw';
|
|
iframe.style.height = '100vh';
|
|
iframe.style.border = 'none';
|
|
iframe.style.zIndex = 9999999999;
|
|
iframe.onload = onIframeLoad;
|
|
return iframe;
|
|
}
|
|
|
|
function addOverlayDivTo(iframe) {
|
|
var div = iframe.contentDocument.createElement('div');
|
|
div.id = 'react-dev-utils-webpack-hot-dev-client-overlay-div';
|
|
div.style.position = 'fixed';
|
|
div.style.boxSizing = 'border-box';
|
|
div.style.left = 0;
|
|
div.style.top = 0;
|
|
div.style.right = 0;
|
|
div.style.bottom = 0;
|
|
div.style.width = '100vw';
|
|
div.style.height = '100vh';
|
|
div.style.backgroundColor = '#fafafa';
|
|
div.style.color = '#333';
|
|
div.style.fontFamily = 'Menlo, Consolas, monospace';
|
|
div.style.fontSize = 'large';
|
|
div.style.padding = '2rem';
|
|
div.style.lineHeight = '1.2';
|
|
div.style.whiteSpace = 'pre-wrap';
|
|
div.style.overflow = 'auto';
|
|
iframe.contentDocument.body.appendChild(div);
|
|
return div;
|
|
}
|
|
|
|
var overlayIframe = null;
|
|
var overlayDiv = null;
|
|
var lastOnOverlayDivReady = null;
|
|
|
|
function ensureOverlayDivExists(onOverlayDivReady) {
|
|
if (overlayDiv) {
|
|
// Everything is ready, call the callback right away.
|
|
onOverlayDivReady(overlayDiv);
|
|
return;
|
|
}
|
|
|
|
// Creating an iframe may be asynchronous so we'll schedule the callback.
|
|
// In case of multiple calls, last callback wins.
|
|
lastOnOverlayDivReady = onOverlayDivReady;
|
|
|
|
if (overlayIframe) {
|
|
// We're already creating it.
|
|
return;
|
|
}
|
|
|
|
// Create iframe and, when it is ready, a div inside it.
|
|
overlayIframe = createOverlayIframe(function onIframeLoad() {
|
|
overlayDiv = addOverlayDivTo(overlayIframe);
|
|
// Now we can talk!
|
|
lastOnOverlayDivReady(overlayDiv);
|
|
});
|
|
|
|
// Zalgo alert: onIframeLoad() will be called either synchronously
|
|
// or asynchronously depending on the browser.
|
|
// We delay adding it so `overlayIframe` is set when `onIframeLoad` fires.
|
|
document.body.appendChild(overlayIframe);
|
|
}
|
|
|
|
function showErrorOverlay(message) {
|
|
ensureOverlayDivExists(function onOverlayDivReady(overlayDiv) {
|
|
// Make it look similar to our terminal.
|
|
overlayDiv.innerHTML =
|
|
'<span style="color: ' + red + '">Failed to compile.</span><br><br>' +
|
|
ansiHTML(entities.encode(message));
|
|
});
|
|
}
|
|
|
|
function destroyErrorOverlay() {
|
|
if (!overlayDiv) {
|
|
// It is not there in the first place.
|
|
return;
|
|
}
|
|
|
|
// Clean up and reset internal state.
|
|
document.body.removeChild(overlayIframe);
|
|
overlayDiv = null;
|
|
overlayIframe = null;
|
|
lastOnOverlayDivReady = null;
|
|
}
|
|
|
|
// Connect to WebpackDevServer via a socket.
|
|
var connection = new SockJS(url.format({
|
|
protocol: window.location.protocol,
|
|
hostname: window.location.hostname,
|
|
port: window.location.port,
|
|
// Hardcoded in WebpackDevServer
|
|
pathname: '/sockjs-node'
|
|
}));
|
|
|
|
// Unlike WebpackDevServer client, we won't try to reconnect
|
|
// to avoid spamming the console. Disconnect usually happens
|
|
// when developer stops the server.
|
|
connection.onclose = function() {
|
|
console.info(
|
|
'The development server has disconnected.\nRefresh the page if necessary.'
|
|
);
|
|
};
|
|
|
|
// Remember some state related to hot module replacement.
|
|
var isFirstCompilation = true;
|
|
var mostRecentCompilationHash = null;
|
|
var hasCompileErrors = false;
|
|
|
|
function clearOutdatedErrors() {
|
|
// Clean up outdated compile errors, if any.
|
|
if (hasCompileErrors && typeof console.clear === 'function') {
|
|
console.clear();
|
|
}
|
|
}
|
|
|
|
// Successful compilation.
|
|
function handleSuccess() {
|
|
clearOutdatedErrors();
|
|
destroyErrorOverlay();
|
|
|
|
var isHotUpdate = !isFirstCompilation;
|
|
isFirstCompilation = false;
|
|
hasCompileErrors = false;
|
|
|
|
// Attempt to apply hot updates or reload.
|
|
if (isHotUpdate) {
|
|
tryApplyUpdates();
|
|
}
|
|
}
|
|
|
|
// Compilation with warnings (e.g. ESLint).
|
|
function handleWarnings(warnings) {
|
|
clearOutdatedErrors();
|
|
destroyErrorOverlay();
|
|
|
|
var isHotUpdate = !isFirstCompilation;
|
|
isFirstCompilation = false;
|
|
hasCompileErrors = false;
|
|
|
|
function printWarnings() {
|
|
// Print warnings to the console.
|
|
for (var i = 0; i < warnings.length; i++) {
|
|
console.warn(stripAnsi(warnings[i]));
|
|
}
|
|
}
|
|
|
|
// Attempt to apply hot updates or reload.
|
|
if (isHotUpdate) {
|
|
tryApplyUpdates(function onSuccessfulHotUpdate() {
|
|
// Only print warnings if we aren't refreshing the page.
|
|
// Otherwise they'll disappear right away anyway.
|
|
printWarnings();
|
|
});
|
|
} else {
|
|
// Print initial warnings immediately.
|
|
printWarnings();
|
|
}
|
|
}
|
|
|
|
// Compilation with errors (e.g. syntax error or missing modules).
|
|
function handleErrors(errors) {
|
|
clearOutdatedErrors();
|
|
|
|
isFirstCompilation = false;
|
|
hasCompileErrors = true;
|
|
|
|
// "Massage" webpack messages.
|
|
var formatted = formatWebpackMessages({
|
|
errors: errors,
|
|
warnings: []
|
|
});
|
|
|
|
// Only show the first error.
|
|
showErrorOverlay(formatted.errors[0]);
|
|
|
|
// Also log them to the console.
|
|
for (var i = 0; i < formatted.errors.length; i++) {
|
|
console.error(stripAnsi(formatted.errors[i]));
|
|
}
|
|
|
|
// Do not attempt to reload now.
|
|
// We will reload on next success instead.
|
|
}
|
|
|
|
// There is a newer version of the code available.
|
|
function handleAvailableHash(hash) {
|
|
// Update last known compilation hash.
|
|
mostRecentCompilationHash = hash;
|
|
}
|
|
|
|
// Handle messages from the server.
|
|
connection.onmessage = function(e) {
|
|
var message = JSON.parse(e.data);
|
|
switch (message.type) {
|
|
case 'hash':
|
|
handleAvailableHash(message.data);
|
|
break;
|
|
case 'still-ok':
|
|
case 'ok':
|
|
handleSuccess();
|
|
break;
|
|
case 'content-changed':
|
|
// Triggered when a file from `contentBase` changed.
|
|
window.location.reload();
|
|
break;
|
|
case 'warnings':
|
|
handleWarnings(message.data);
|
|
break;
|
|
case 'errors':
|
|
handleErrors(message.data);
|
|
break;
|
|
default:
|
|
// Do nothing.
|
|
}
|
|
}
|
|
|
|
// Is there a newer version of this code available?
|
|
function isUpdateAvailable() {
|
|
/* globals __webpack_hash__ */
|
|
// __webpack_hash__ is the hash of the current compilation.
|
|
// It's a global variable injected by Webpack.
|
|
return mostRecentCompilationHash !== __webpack_hash__;
|
|
}
|
|
|
|
// Webpack disallows updates in other states.
|
|
function canApplyUpdates() {
|
|
return module.hot.status() === 'idle';
|
|
}
|
|
|
|
// Attempt to update code on the fly, fall back to a hard reload.
|
|
function tryApplyUpdates(onHotUpdateSuccess) {
|
|
if (!module.hot) {
|
|
// HotModuleReplacementPlugin is not in Webpack configuration.
|
|
window.location.reload();
|
|
return;
|
|
}
|
|
|
|
if (!isUpdateAvailable() || !canApplyUpdates()) {
|
|
return;
|
|
}
|
|
|
|
function handleApplyUpdates(err, updatedModules) {
|
|
if (err || !updatedModules) {
|
|
window.location.reload();
|
|
return;
|
|
}
|
|
|
|
if (typeof onHotUpdateSuccess === 'function') {
|
|
// Maybe we want to do something.
|
|
onHotUpdateSuccess();
|
|
}
|
|
|
|
if (isUpdateAvailable()) {
|
|
// While we were updating, there was a new update! Do it again.
|
|
tryApplyUpdates();
|
|
}
|
|
}
|
|
|
|
// https://webpack.github.io/docs/hot-module-replacement.html#check
|
|
var result = module.hot.check(/* autoApply */true, handleApplyUpdates);
|
|
|
|
// // Webpack 2 returns a Promise instead of invoking a callback
|
|
if (result && result.then) {
|
|
result.then(
|
|
function(updatedModules) {
|
|
handleApplyUpdates(null, updatedModules);
|
|
},
|
|
function(err) {
|
|
handleApplyUpdates(err, null);
|
|
}
|
|
);
|
|
}
|
|
}
|