Skip to content

Commit

Permalink
Require the isomorphic React instead of internals from renderers (#7473)
Browse files Browse the repository at this point in the history
This is needed for flat builds. It also lets us get rid of a bunch
of special cases in the build scripts.

It also allow us to just copy the source files into React Native
instead of having to build first to resolve the special cases.
(cherry picked from commit 1c5a639)
  • Loading branch information
sebmarkbage authored and zpao committed Oct 3, 2016
1 parent 5c2a88a commit cc2e5cc
Show file tree
Hide file tree
Showing 23 changed files with 79 additions and 89 deletions.
4 changes: 3 additions & 1 deletion flow/react-native-host-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ declare module 'UIManager' {
declare function setChildren() : void;
declare function updateView() : void;
}
declare module 'View' { }
declare module 'View' {
declare var exports : typeof ReactComponent;
}
7 changes: 1 addition & 6 deletions grunt/config/browserify.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ var SECRET_INTERNALS_NAME = 'React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_
var shimSharedModulesFiles = {
'./ReactCurrentOwner': SECRET_INTERNALS_NAME + '.ReactCurrentOwner',
'./ReactComponentTreeHook': SECRET_INTERNALS_NAME + '.ReactComponentTreeHook',
// The methods we used here are exposed on the main React export.
// TODO: Change all renderer code to require the isomorphic React directly
// instead of these internals.
// Use the global, anywhere we require React
'./React': 'React',
'./ReactElement': 'React',
'./ReactPropTypes': 'React.PropTypes',
'./ReactChildren': 'React.Children',
};

// We can access these as absolute or relative. We need to shim both.
Expand Down
13 changes: 2 additions & 11 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,11 @@ var moduleMapReact = Object.assign(
);

var rendererSharedState = {
// Alias
React: 'react/lib/React',
// Shared state
ReactCurrentOwner: 'react/lib/ReactCurrentOwner',
ReactComponentTreeHook: 'react/lib/ReactComponentTreeHook',

// TODO: Move to shared since these are actually shared and can be copied.
ReactPropTypeLocations: 'react/lib/ReactPropTypeLocations',
ReactPropTypesSecret: 'react/lib/ReactPropTypesSecret',
checkReactTypeSpec: 'react/lib/checkReactTypeSpec',

// TODO: Update the source to just use the React module.
React: 'react/lib/React',
ReactElement: 'react/lib/ReactElement',
ReactPropTypes: 'react/lib/ReactPropTypes',
ReactChildren: 'react/lib/ReactChildren',
};

var moduleMapReactDOM = Object.assign(
Expand Down
8 changes: 1 addition & 7 deletions src/isomorphic/classic/element/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ var warning = require('warning');
var canDefineProperty = require('canDefineProperty');
var hasOwnProperty = Object.prototype.hasOwnProperty;

// The Symbol used to tag the ReactElement type. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
var REACT_ELEMENT_TYPE =
(typeof Symbol === 'function' && Symbol.for && Symbol.for('react.element')) ||
0xeac7;
var REACT_ELEMENT_TYPE = require('ReactElementSymbol');

var RESERVED_PROPS = {
key: true,
Expand Down Expand Up @@ -390,6 +386,4 @@ ReactElement.isValidElement = function(object) {
);
};

ReactElement.REACT_ELEMENT_TYPE = REACT_ELEMENT_TYPE;

module.exports = ReactElement;
6 changes: 3 additions & 3 deletions src/renderers/dom/client/ReactMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

var DOMLazyTree = require('DOMLazyTree');
var DOMProperty = require('DOMProperty');
var React = require('React');
var ReactBrowserEventEmitter = require('ReactBrowserEventEmitter');
var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactDOMComponentTree = require('ReactDOMComponentTree');
var ReactDOMContainerInfo = require('ReactDOMContainerInfo');
var ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
var ReactElement = require('ReactElement');
var ReactFeatureFlags = require('ReactFeatureFlags');
var ReactInstanceMap = require('ReactInstanceMap');
var ReactInstrumentation = require('ReactInstrumentation');
Expand Down Expand Up @@ -423,7 +423,7 @@ var ReactMount = {
_renderSubtreeIntoContainer: function(parentComponent, nextElement, container, callback) {
ReactUpdateQueue.validateCallback(callback, 'ReactDOM.render');
invariant(
ReactElement.isValidElement(nextElement),
React.isValidElement(nextElement),
'ReactDOM.render(): Invalid component element.%s',
(
typeof nextElement === 'string' ?
Expand All @@ -450,7 +450,7 @@ var ReactMount = {
'for your app.'
);

var nextWrappedElement = ReactElement.createElement(
var nextWrappedElement = React.createElement(
TopLevelWrapper,
{ child: nextElement }
);
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/dom/client/wrappers/LinkedValueUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

'use strict';

var ReactPropTypes = require('ReactPropTypes');
var React = require('React');
var ReactPropTypeLocations = require('ReactPropTypeLocations');
var ReactPropTypesSecret = require('ReactPropTypesSecret');

Expand Down Expand Up @@ -84,7 +84,7 @@ var propTypes = {
'set either `onChange` or `readOnly`.'
);
},
onChange: ReactPropTypes.func,
onChange: React.PropTypes.func,
};

var loggedTypeFailures = {};
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/dom/client/wrappers/ReactDOMOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

'use strict';

var ReactChildren = require('ReactChildren');
var React = require('React');
var ReactDOMComponentTree = require('ReactDOMComponentTree');
var ReactDOMSelect = require('ReactDOMSelect');

Expand All @@ -23,7 +23,7 @@ function flattenChildren(children) {

// Flatten children and warn if they aren't strings or numbers;
// invalid types are ignored.
ReactChildren.forEach(children, function(child) {
React.Children.forEach(children, function(child) {
if (child == null) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/renderers/dom/server/ReactServerRendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*/
'use strict';

var React = require('React');
var ReactDOMContainerInfo = require('ReactDOMContainerInfo');
var ReactDefaultBatchingStrategy = require('ReactDefaultBatchingStrategy');
var ReactElement = require('ReactElement');
var ReactInstrumentation = require('ReactInstrumentation');
var ReactMarkupChecksum = require('ReactMarkupChecksum');
var ReactReconciler = require('ReactReconciler');
Expand Down Expand Up @@ -80,7 +80,7 @@ function renderToStringImpl(element, makeStaticMarkup) {
*/
function renderToString(element) {
invariant(
ReactElement.isValidElement(element),
React.isValidElement(element),
'renderToString(): You must pass a valid ReactElement.'
);
return renderToStringImpl(element, false);
Expand All @@ -93,7 +93,7 @@ function renderToString(element) {
*/
function renderToStaticMarkup(element) {
invariant(
ReactElement.isValidElement(element),
React.isValidElement(element),
'renderToStaticMarkup(): You must pass a valid ReactElement.'
);
return renderToStringImpl(element, true);
Expand Down
3 changes: 1 addition & 2 deletions src/renderers/native/ReactNative.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
var ReactNativeComponentTree = require('ReactNativeComponentTree');
var ReactNativeDefaultInjection = require('ReactNativeDefaultInjection');

var ReactElement = require('ReactElement');
var ReactNativeMount = require('ReactNativeMount');
var ReactUpdates = require('ReactUpdates');

Expand All @@ -25,7 +24,7 @@ var findNodeHandle = require('findNodeHandle');
ReactNativeDefaultInjection.inject();

var render = function(
element: ReactElement,
element: ReactElement<any>,
mountInto: number,
callback?: ?(() => void)
): ?ReactComponent<any, any, any> {
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/native/ReactNativeDefaultInjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ require('InitializeJavaScriptAppEngine');
var EventPluginHub = require('EventPluginHub');
var EventPluginUtils = require('EventPluginUtils');
var RCTEventEmitter = require('RCTEventEmitter');
var React = require('React');
var ReactComponentEnvironment = require('ReactComponentEnvironment');
var ReactDefaultBatchingStrategy = require('ReactDefaultBatchingStrategy');
var ReactElement = require('ReactElement');
var ReactEmptyComponent = require('ReactEmptyComponent');
var ReactNativeBridgeEventPlugin = require('ReactNativeBridgeEventPlugin');
var ReactHostComponent = require('ReactHostComponent');
Expand Down Expand Up @@ -83,7 +83,7 @@ function inject() {
// Can't import View at the top because it depends on React to make its composite
var View = require('View');
return new ReactSimpleEmptyComponent(
ReactElement.createElement(View, {
React.createElement(View, {
collapsable: true,
style: { position: 'absolute' },
}),
Expand Down
19 changes: 7 additions & 12 deletions src/renderers/native/ReactNativeMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
'use strict';

var ReactElement = require('ReactElement');
var React = require('React');
var ReactInstrumentation = require('ReactInstrumentation');
var ReactNativeContainerInfo = require('ReactNativeContainerInfo');
var ReactNativeTagHandles = require('ReactNativeTagHandles');
Expand All @@ -35,9 +35,9 @@ if (__DEV__) {
TopLevelWrapper.displayName = 'TopLevelWrapper';
}
TopLevelWrapper.prototype.render = function() {
// this.props is actually a ReactElement
return this.props;
return this.props.child;
};
TopLevelWrapper.isReactTopLevelWrapper = true;

/**
* Mounts this component and inserts it into the DOM.
Expand Down Expand Up @@ -99,25 +99,20 @@ var ReactNativeMount = {
* @param {containerTag} containerView Handle to native view tag
*/
renderComponent: function(
nextElement: ReactElement,
nextElement: ReactElement<*>,
containerTag: number,
callback?: ?(() => void)
): ?ReactComponent<any, any, any> {
var nextWrappedElement = new ReactElement(
var nextWrappedElement = React.createElement(
TopLevelWrapper,
null,
null,
null,
null,
null,
nextElement
{ child: nextElement }
);

var topRootNodeID = containerTag;
var prevComponent = ReactNativeMount._instancesByContainerID[topRootNodeID];
if (prevComponent) {
var prevWrappedElement = prevComponent._currentElement;
var prevElement = prevWrappedElement.props;
var prevElement = prevWrappedElement.props.child;
if (shouldUpdateReactComponent(prevElement, nextElement)) {
ReactUpdateQueue.enqueueElementInternal(prevComponent, nextWrappedElement, emptyObject);
if (callback) {
Expand Down
4 changes: 1 addition & 3 deletions src/renderers/shared/fiber/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import type { PriorityLevel } from 'ReactPriorityLevel';

import type { ReactNodeList } from 'ReactTypes';

var {
REACT_ELEMENT_TYPE,
} = require('ReactElement');
var REACT_ELEMENT_TYPE = require('ReactElementSymbol');
var {
REACT_COROUTINE_TYPE,
REACT_YIELD_TYPE,
Expand Down
5 changes: 2 additions & 3 deletions src/renderers/shared/fiber/ReactFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ var {
YieldComponent,
} = ReactTypeOfWork;

var ReactElement = require('ReactElement');

var {
NoWork,
} = require('ReactPriorityLevel');
Expand Down Expand Up @@ -201,7 +199,8 @@ exports.createHostContainerFiber = function() {
return fiber;
};

exports.createFiberFromElement = function(element : ReactElement, priorityLevel : PriorityLevel) {
exports.createFiberFromElement = function(element : ReactElement<*>, priorityLevel : PriorityLevel) {
// $FlowFixMe: ReactElement.key is currently defined as ?string but should be defined as null | string in Flow.
const fiber = createFiberFromElementType(element.type, element.key);
fiber.pendingProps = element.props;
fiber.pendingWorkPriority = priorityLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

'use strict';

var React = require('React');
var ReactComponentEnvironment = require('ReactComponentEnvironment');
var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactElement = require('ReactElement');
var ReactErrorUtils = require('ReactErrorUtils');
var ReactInstanceMap = require('ReactInstanceMap');
var ReactInstrumentation = require('ReactInstrumentation');
Expand Down Expand Up @@ -49,7 +49,7 @@ StatelessComponent.prototype.render = function() {
function warnIfInvalidElement(Component, element) {
if (__DEV__) {
warning(
element === null || element === false || ReactElement.isValidElement(element),
element === null || element === false || React.isValidElement(element),
'%s(...): A valid React element (or null) must be returned. You may have ' +
'returned undefined, an array or some other invalid object.',
Component.displayName || Component.name || 'Component'
Expand Down Expand Up @@ -212,7 +212,7 @@ var ReactCompositeComponentMixin = {
invariant(
inst === null ||
inst === false ||
ReactElement.isValidElement(inst),
React.isValidElement(inst),
'%s(...): A valid React element (or null) must be returned. You may have ' +
'returned undefined, an array or some other invalid object.',
Component.displayName || Component.name || 'Component'
Expand Down Expand Up @@ -1078,7 +1078,7 @@ var ReactCompositeComponentMixin = {
invariant(
// TODO: An `isValidNode` function would probably be more appropriate
renderedComponent === null || renderedComponent === false ||
ReactElement.isValidElement(renderedComponent),
React.isValidElement(renderedComponent),
'%s.render(): A valid React element (or null) must be returned. You may have ' +
'returned undefined, an array or some other invalid object.',
this.getName() || 'ReactCompositeComponent'
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/shared/stack/reconciler/ReactNodeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

type ReactNodeType = 0 | 1 | 2;

var ReactElement = require('ReactElement');
var React = require('React');

var invariant = require('invariant');

Expand All @@ -26,7 +26,7 @@ var ReactNodeTypes = {
getType: function(node: ReactElement<any>): ReactNodeType {
if (node === null || node === false) {
return ReactNodeTypes.EMPTY;
} else if (ReactElement.isValidElement(node)) {
} else if (React.isValidElement(node)) {
if (typeof node.type === 'function') {
return ReactNodeTypes.COMPOSITE;
} else {
Expand Down
24 changes: 7 additions & 17 deletions src/renderers/testing/ReactTestMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
'use strict';

var ReactElement = require('ReactElement');
var React = require('React');
var ReactReconciler = require('ReactReconciler');
var ReactUpdates = require('ReactUpdates');

Expand All @@ -31,9 +31,9 @@ if (__DEV__) {
TopLevelWrapper.displayName = 'TopLevelWrapper';
}
TopLevelWrapper.prototype.render = function() {
// this.props is actually a ReactElement
return this.props;
return this.props.child;
};
TopLevelWrapper.isReactTopLevelWrapper = true;

/**
* Mounts this component and inserts it into the DOM.
Expand Down Expand Up @@ -88,14 +88,9 @@ ReactTestInstance.prototype.update = function(nextElement) {
this._component,
"ReactTestRenderer: .update() can't be called after unmount."
);
var nextWrappedElement = new ReactElement(
var nextWrappedElement = React.createElement(
TopLevelWrapper,
null,
null,
null,
null,
null,
nextElement
{ child: nextElement }
);
var component = this._component;
ReactUpdates.batchedUpdates(function() {
Expand Down Expand Up @@ -142,14 +137,9 @@ var ReactTestMount = {
render: function(
nextElement: ReactElement<any>
): ReactTestInstance {
var nextWrappedElement = new ReactElement(
var nextWrappedElement = React.createElement(
TopLevelWrapper,
null,
null,
null,
null,
null,
nextElement
{ child: nextElement }
);

var instance = instantiateReactComponent(nextWrappedElement, false);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit cc2e5cc

Please sign in to comment.