[react-native] Make document.js into a polyfill. Fixes #1149

Summary:
@public
document shimming must run before anything else. However, we don't currently guarantee that. This moves the document shimming into `document.js` which is used as a polyfill.

Test Plan:
* start server
* go to playground app
* require `NativeModules` as the first thing
* open chrome debugger
* no error
This commit is contained in:
Amjad Masad 2015-05-13 17:45:36 -07:00
parent 5429b5f9cc
commit 9fde7d2828
3 changed files with 40 additions and 35 deletions

View File

@ -33,39 +33,6 @@ if (typeof window === 'undefined') {
window = GLOBAL; 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, isFatal) { function handleErrorWithRedBox(e, isFatal) {
try { try {
require('ExceptionsManager').handleException(e, isFatal); require('ExceptionsManager').handleException(e, isFatal);
@ -148,7 +115,6 @@ function setupGeolocation() {
GLOBAL.navigator.geolocation = require('Geolocation'); GLOBAL.navigator.geolocation = require('Geolocation');
} }
setupDocumentShim();
setupRedBoxErrorHandler(); setupRedBoxErrorHandler();
setupTimers(); setupTimers();
setupAlert(); setupAlert();

View File

@ -0,0 +1,34 @@
/* eslint global-strict: 0 */
(function(GLOBAL) {
/**
* The document must be shimmed before anything else that might define the
* `ExecutionEnvironment` module (which checks for `document.createElement`).
*/
// 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;
})(this);

View File

@ -200,7 +200,12 @@ function getAppMiddleware(options) {
cacheVersion: '2', cacheVersion: '2',
transformModulePath: require.resolve('./transformer.js'), transformModulePath: require.resolve('./transformer.js'),
assetRoots: options.assetRoots, assetRoots: options.assetRoots,
assetExts: ['png', 'jpeg', 'jpg'] assetExts: ['png', 'jpeg', 'jpg'],
polyfillModuleNames: [
require.resolve(
'../Libraries/JavaScriptAppEngine/polyfills/document.js'
),
],
}); });
} }