prepare a new demo environment from react-native 0.8.0

This commit is contained in:
yuji
2015-07-29 17:30:04 +08:00
parent 7b973f3d90
commit c623553629
899 changed files with 25020 additions and 39588 deletions

View File

@@ -11,11 +11,11 @@
*/
'use strict';
var Platform = require('Platform');
var RCTExceptionsManager = require('NativeModules').ExceptionsManager;
var loadSourceMap = require('loadSourceMap');
var parseErrorStack = require('parseErrorStack');
var stringifySafe = require('stringifySafe');
var sourceMapPromise;
@@ -25,31 +25,72 @@ type Exception = {
message: string;
}
function handleException(e: Exception) {
var stack = parseErrorStack(e);
console.error(
'Err0r: ' +
'\n stack: \n' + stackToString(stack) +
'\n URL: ' + e.sourceURL +
'\n line: ' + e.line +
'\n message: ' + e.message
);
function reportException(e: Exception, isFatal: bool, stack?: any) {
if (RCTExceptionsManager) {
RCTExceptionsManager.reportUnhandledException(e.message, stack);
if (!stack) {
stack = parseErrorStack(e);
}
if (isFatal) {
RCTExceptionsManager.reportFatalException(e.message, stack);
} else {
RCTExceptionsManager.reportSoftException(e.message, stack);
}
if (__DEV__) {
(sourceMapPromise = sourceMapPromise || loadSourceMap())
.then(map => {
var prettyStack = parseErrorStack(e, map);
RCTExceptionsManager.updateExceptionMessage(e.message, prettyStack);
})
.then(null, error => {
console.error('#CLOWNTOWN (error while displaying error): ' + error.message);
.catch(error => {
// This can happen in a variety of normal situations, such as
// Network module not being available, or when running locally
console.warn('Unable to load source map: ' + error.message);
});
}
}
}
function handleException(e: Exception, isFatal: boolean) {
var stack = parseErrorStack(e);
var msg =
'Error: ' + e.message +
'\n stack: \n' + stackToString(stack) +
'\n URL: ' + e.sourceURL +
'\n line: ' + e.line +
'\n message: ' + e.message;
if (console.errorOriginal) {
console.errorOriginal(msg);
} else {
console.error(msg);
}
reportException(e, isFatal, stack);
}
/**
* Shows a redbox with stacktrace for all console.error messages. Disable by
* setting `console.reportErrorsAsExceptions = false;` in your app.
*/
function installConsoleErrorReporter() {
if (console.reportException) {
return; // already installed
}
console.reportException = reportException;
console.errorOriginal = console.error.bind(console);
console.error = function reactConsoleError() {
console.errorOriginal.apply(null, arguments);
if (!console.reportErrorsAsExceptions) {
return;
}
var str = Array.prototype.map.call(arguments, stringifySafe).join(', ');
var error: any = new Error('console.error: ' + str);
error.framesToPop = 1;
reportException(error, /* isFatal */ false);
};
if (console.reportErrorsAsExceptions === undefined) {
console.reportErrorsAsExceptions = true; // Individual apps can disable this
}
}
function stackToString(stack) {
var maxLength = Math.max.apply(null, stack.map(frame => frame.methodName.length));
return stack.map(frame => stackFrameToString(frame, maxLength)).join('\n');
@@ -71,4 +112,4 @@ function fillSpaces(n) {
return new Array(n + 1).join(' ');
}
module.exports = { handleException };
module.exports = { handleException, installConsoleErrorReporter };

View File

@@ -23,7 +23,9 @@
/* globals GLOBAL: true, window: true */
// Just to make sure the JS gets packaged up.
require('RCTDebugComponentOwnership');
require('RCTDeviceEventEmitter');
require('PerformanceLogger');
if (typeof GLOBAL === 'undefined') {
GLOBAL = this;
@@ -33,50 +35,27 @@ if (typeof window === 'undefined') {
window = GLOBAL;
}
/**
* The document must be shimmed before anything else that might define the
* `ExecutionEnvironment` module (which checks for `document.createElement`).
*/
function setupDocumentShim() {
// The browser defines Text and Image globals by default. If you forget to
// require them, then the error message is very confusing.
function getInvalidGlobalUseError(name) {
return new Error(
'You are trying to render the global ' + name + ' variable as a ' +
'React element. You probably forgot to require ' + name + '.'
);
}
GLOBAL.Text = {
get defaultProps() {
throw getInvalidGlobalUseError('Text');
}
};
GLOBAL.Image = {
get defaultProps() {
throw getInvalidGlobalUseError('Image');
}
};
// Force `ExecutionEnvironment.canUseDOM` to be false.
if (GLOBAL.document) {
GLOBAL.document.createElement = null;
}
// There is no DOM so MutationObserver doesn't make sense. It is used
// as feature detection in Bluebird Promise implementation
GLOBAL.MutationObserver = undefined;
}
function handleErrorWithRedBox(e) {
function handleError(e, isFatal) {
try {
require('ExceptionsManager').handleException(e);
require('ExceptionsManager').handleException(e, isFatal);
} catch(ee) {
console.log('Failed to print error: ', ee.message);
}
}
function setupRedBoxErrorHandler() {
function setUpRedBoxErrorHandler() {
var ErrorUtils = require('ErrorUtils');
ErrorUtils.setGlobalHandler(handleErrorWithRedBox);
ErrorUtils.setGlobalHandler(handleError);
}
function setUpRedBoxConsoleErrorHandler() {
// ExceptionsManager transitively requires Promise so we install it after
var ExceptionsManager = require('ExceptionsManager');
var Platform = require('Platform');
// TODO (#6925182): Enable console.error redbox on Android
if (__DEV__ && Platform.OS === 'ios') {
ExceptionsManager.installConsoleErrorReporter();
}
}
/**
@@ -86,7 +65,7 @@ function setupRedBoxErrorHandler() {
* implement our own custom timing bridge that should be immune to
* unexplainably dropped timing signals.
*/
function setupTimers() {
function setUpTimers() {
var JSTimers = require('JSTimers');
GLOBAL.setTimeout = JSTimers.setTimeout;
GLOBAL.setInterval = JSTimers.setInterval;
@@ -101,42 +80,60 @@ function setupTimers() {
};
}
function setupAlert() {
function setUpAlert() {
var RCTAlertManager = require('NativeModules').AlertManager;
if (!GLOBAL.alert) {
GLOBAL.alert = function(text) {
var alertOpts = {
title: 'Alert',
message: '' + text,
buttons: [{'cancel': 'Okay'}],
buttons: [{'cancel': 'OK'}],
};
RCTAlertManager.alertWithArgs(alertOpts, null);
};
}
}
function setupPromise() {
function setUpPromise() {
// The native Promise implementation throws the following error:
// ERROR: Event loop not supported.
GLOBAL.Promise = require('Promise');
}
function setupXHR() {
function setUpXHR() {
// The native XMLHttpRequest in Chrome dev tools is CORS aware and won't
// let you fetch anything from the internet
GLOBAL.XMLHttpRequest = require('XMLHttpRequest');
GLOBAL.fetch = require('fetch');
GLOBAL.FormData = require('FormData');
var fetchPolyfill = require('fetch');
GLOBAL.fetch = fetchPolyfill.fetch;
GLOBAL.Headers = fetchPolyfill.Headers;
GLOBAL.Request = fetchPolyfill.Request;
GLOBAL.Response = fetchPolyfill.Response;
}
function setupGeolocation() {
function setUpGeolocation() {
GLOBAL.navigator = GLOBAL.navigator || {};
GLOBAL.navigator.geolocation = require('Geolocation');
}
setupDocumentShim();
setupRedBoxErrorHandler();
setupTimers();
setupAlert();
setupPromise();
setupXHR();
setupGeolocation();
function setUpWebSockets() {
GLOBAL.WebSocket = require('WebSocket');
}
function setupProfile() {
console.profile = console.profile || GLOBAL.consoleProfile || function () {};
console.profileEnd = console.profileEnd || GLOBAL.consoleProfileEnd || function () {};
require('BridgeProfiling').swizzleReactPerf();
}
setUpRedBoxErrorHandler();
setUpTimers();
setUpAlert();
setUpPromise();
setUpXHR();
setUpRedBoxConsoleErrorHandler();
setUpGeolocation();
setUpWebSockets();
setupProfile();

View File

@@ -1,3 +1,4 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
@@ -8,6 +9,7 @@
*
* @providesModule SourceMap
* @generated
* @extern
*
* This module was generated from `node_modules/source-map` by running
*
@@ -18,6 +20,7 @@
* and wrapping resulting file into `wrapper` function.
*
*/
/*eslint-disable */
var scope = {};
wrapper.call(scope);

View File

@@ -13,11 +13,12 @@
'use strict';
var Promise = require('Promise');
var RCTSourceCode = require('NativeModules').SourceCode;
var NativeModules = require('NativeModules');
var SourceMapConsumer = require('SourceMap').SourceMapConsumer;
var SourceMapURL = require('./source-map-url');
var fetch = require('fetch');
var RCTSourceCode = NativeModules.SourceCode;
var RCTNetworking = NativeModules.Networking;
function loadSourceMap(): Promise {
return fetchSourceMap()
@@ -33,17 +34,31 @@ function fetchSourceMap(): Promise {
return Promise.reject(new Error('RCTSourceCode module is not available'));
}
if (!RCTNetworking) {
// Used internally by fetch
return Promise.reject(new Error('RCTNetworking module is not available'));
}
return new Promise(RCTSourceCode.getScriptText)
.then(extractSourceMapURL)
.then((url) => {
if (url === null) {
return Promise.reject(new Error('No source map URL found. May be running from bundled file.'));
}
return Promise.resolve(url);
})
.then(fetch)
.then(response => response.text())
}
function extractSourceMapURL({url, text, fullSourceMappingURL}): string {
function extractSourceMapURL({url, text, fullSourceMappingURL}): ?string {
if (fullSourceMappingURL) {
return fullSourceMappingURL;
}
var mapURL = SourceMapURL.getFrom(text);
if (!mapURL) {
return null;
}
var baseURL = url.match(/(.+:\/\/.*?)\//)[1];
return baseURL + mapURL;
}

View File

@@ -28,6 +28,10 @@ function resolveSourceMaps(sourceMapInstance, stackFrame) {
}
function parseErrorStack(e, sourceMapInstance) {
if (!e || !e.stack) {
return [];
}
var stack = stacktraceParser.parse(e.stack);
var framesToPop = e.framesToPop || 0;

View File

@@ -11,6 +11,7 @@
*
* @nolint
*/
/* eslint-disable */
(function() {
var define = null; // Hack to make it work with our packager

View File

@@ -43,7 +43,6 @@ var JSTimers = {
var newID = JSTimersExecution.GUID++;
var freeIndex = JSTimers._getFreeIndex();
JSTimersExecution.timerIDs[freeIndex] = newID;
JSTimersExecution.callbacks[freeIndex] = func;
JSTimersExecution.callbacks[freeIndex] = function() {
return func.apply(undefined, args);
};
@@ -60,7 +59,6 @@ var JSTimers = {
var newID = JSTimersExecution.GUID++;
var freeIndex = JSTimers._getFreeIndex();
JSTimersExecution.timerIDs[freeIndex] = newID;
JSTimersExecution.callbacks[freeIndex] = func;
JSTimersExecution.callbacks[freeIndex] = function() {
return func.apply(undefined, args);
};
@@ -77,7 +75,6 @@ var JSTimers = {
var newID = JSTimersExecution.GUID++;
var freeIndex = JSTimers._getFreeIndex();
JSTimersExecution.timerIDs[freeIndex] = newID;
JSTimersExecution.callbacks[freeIndex] = func;
JSTimersExecution.callbacks[freeIndex] = function() {
return func.apply(undefined, args);
};