@samitouri / QOS-React / commits / 659a29cecf

Reorganize how shared internals are accessed (#13201)

* Reorganize how shared internals are accessed * Update forks.js

Dan Abramov committed Jul 13, 2018 at 02:45 UTC 659a29cecf74301532354261369e9048aac6e20f
17 files changed +91 -44
packages/react-dom/src/client/ReactDOM.js
+3 -1
@@ -29,7 +29,7 @@ import * as EventPluginRegistry from 'events/EventPluginRegistry';
29 import * as EventPropagators from 'events/EventPropagators';
30 import * as ReactInstanceMap from 'shared/ReactInstanceMap';
31 import ReactVersion from 'shared/ReactVersion';
32 -import {ReactCurrentOwner} from 'shared/ReactGlobalSharedState';
32 +import ReactSharedInternals from 'shared/ReactSharedInternals';
33 import getComponentName from 'shared/getComponentName';
34 import invariant from 'shared/invariant';
35 import lowPriorityWarning from 'shared/lowPriorityWarning';
@@ -46,6 +46,8 @@ import {
46 } from '../shared/HTMLNodeType';
47 import {ROOT_ATTRIBUTE_NAME} from '../shared/DOMProperty';
48
49 +const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
50 +
51 let topLevelUpdateWarnings;
52 let warnOnInvalidCallback;
53 let didWarnAboutUnstableCreatePortal = false;
packages/react-dom/src/server/ReactPartialRenderer.js
+4 -1
@@ -21,7 +21,7 @@ import lowPriorityWarning from 'shared/lowPriorityWarning';
21 import warning from 'shared/warning';
22 import checkPropTypes from 'prop-types/checkPropTypes';
23 import describeComponentFrame from 'shared/describeComponentFrame';
24 -import {ReactDebugCurrentFrame} from 'shared/ReactGlobalSharedState';
24 +import ReactSharedInternals from 'shared/ReactSharedInternals';
25 import {warnAboutDeprecatedLifecycles} from 'shared/ReactFeatureFlags';
26 import {
27 REACT_FORWARD_REF_TYPE,
@@ -68,6 +68,7 @@ const toArray = ((React.Children.toArray: any): toArrayType);
68 // Each stack is an array of frames which may contain nested stacks of elements.
69 let currentDebugStacks = [];
70
71 +let ReactDebugCurrentFrame;
72 let prevGetCurrentStackImpl = null;
73 let getCurrentServerStackImpl = () => '';
74 let describeStackFrame = element => '';
@@ -78,6 +79,8 @@ let pushElementToDebugStack = (element: ReactElement) => {};
79 let popCurrentDebugStack = () => {};
80
81 if (__DEV__) {
82 + ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
83 +
84 validatePropertiesInDevelopment = function(type, props) {
85 validateARIAProperties(type, props);
86 validateInputProperties(type, props);
packages/react-dom/src/shared/ReactDOMInvalidARIAHook.js
+3 -1
@@ -6,12 +6,14 @@
6 */
7
8 import warning from 'shared/warning';
9 -import {ReactDebugCurrentFrame} from 'shared/ReactGlobalSharedState';
9 +import ReactSharedInternals from 'shared/ReactSharedInternals';
10
11 import {ATTRIBUTE_NAME_CHAR} from './DOMProperty';
12 import isCustomComponent from './isCustomComponent';
13 import validAriaProperties from './validAriaProperties';
14
15 +const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
16 +
17 const warnedProperties = {};
18 const rARIA = new RegExp('^(aria)-[' + ATTRIBUTE_NAME_CHAR + ']*$');
19 const rARIACamel = new RegExp('^(aria)[A-Z][' + ATTRIBUTE_NAME_CHAR + ']*$');
packages/react-dom/src/shared/ReactDOMNullInputValuePropHook.js
+3 -1
@@ -5,9 +5,11 @@
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 -import {ReactDebugCurrentFrame} from 'shared/ReactGlobalSharedState';
8 +import ReactSharedInternals from 'shared/ReactSharedInternals';
9 import warning from 'shared/warning';
10
11 +let ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
12 +
13 let didWarnValueNull = false;
14
15 export function validateProperties(type, props) {
packages/react-dom/src/shared/ReactDOMUnknownPropertyHook.js
+5 -1
@@ -9,7 +9,7 @@ import {
9 registrationNameModules,
10 possibleRegistrationNames,
11 } from 'events/EventPluginRegistry';
12 -import {ReactDebugCurrentFrame} from 'shared/ReactGlobalSharedState';
12 +import ReactSharedInternals from 'shared/ReactSharedInternals';
13 import warning from 'shared/warning';
14
15 import {
@@ -21,9 +21,13 @@ import {
21 import isCustomComponent from './isCustomComponent';
22 import possibleStandardNames from './possibleStandardNames';
23
24 +let ReactDebugCurrentFrame = null;
25 +
26 let validateProperty = () => {};
27
28 if (__DEV__) {
29 + ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
30 +
31 const warnedProperties = {};
32 const hasOwnProperty = Object.prototype.hasOwnProperty;
33 const EVENT_NAME_REGEX = /^on./;
packages/react-native-renderer/src/ReactFabric.js
+2 -1
@@ -23,10 +23,11 @@ import ReactNativeComponent from './ReactNativeComponent';
23 import * as ReactFabricComponentTree from './ReactFabricComponentTree';
24 import {getInspectorDataForViewTag} from './ReactNativeFiberInspector';
25
26 -import {ReactCurrentOwner} from 'shared/ReactGlobalSharedState';
26 +import ReactSharedInternals from 'shared/ReactSharedInternals';
27 import getComponentName from 'shared/getComponentName';
28 import warning from 'shared/warning';
29
30 +const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
31 const findHostInstance = ReactFabricRenderer.findHostInstance;
32
33 function findNodeHandle(componentOrHandle: any): ?number {
packages/react-native-renderer/src/ReactNativeRenderer.js
+2 -1
@@ -26,10 +26,11 @@ import ReactNativeComponent from './ReactNativeComponent';
26 import * as ReactNativeComponentTree from './ReactNativeComponentTree';
27 import {getInspectorDataForViewTag} from './ReactNativeFiberInspector';
28
29 -import {ReactCurrentOwner} from 'shared/ReactGlobalSharedState';
29 +import ReactSharedInternals from 'shared/ReactSharedInternals';
30 import getComponentName from 'shared/getComponentName';
31 import warning from 'shared/warning';
32
33 +const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
34 const findHostInstance = ReactNativeFiberRenderer.findHostInstance;
35
36 function findNodeHandle(componentOrHandle: any): ?number {
packages/react-reconciler/src/ReactCurrentFiber.js
+3 -1
@@ -7,7 +7,7 @@
7 * @flow
8 */
9
10 -import {ReactDebugCurrentFrame} from 'shared/ReactGlobalSharedState';
10 +import ReactSharedInternals from 'shared/ReactSharedInternals';
11 import {
12 IndeterminateComponent,
13 FunctionalComponent,
@@ -17,6 +17,8 @@ import {
17 import describeComponentFrame from 'shared/describeComponentFrame';
18 import getComponentName from 'shared/getComponentName';
19
20 +const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
21 +
22 import type {Fiber} from './ReactFiber';
23
24 type LifeCyclePhase = 'render' | 'getChildContext';
packages/react-reconciler/src/ReactFiberBeginWork.js
+3 -1
@@ -38,7 +38,7 @@ import {
38 Update,
39 Ref,
40 } from 'shared/ReactTypeOfSideEffect';
41 -import {ReactCurrentOwner} from 'shared/ReactGlobalSharedState';
41 +import ReactSharedInternals from 'shared/ReactSharedInternals';
42 import {
43 enableGetDerivedStateFromCatch,
44 enableSuspense,
@@ -98,6 +98,8 @@ import {
98 } from './ReactFiberClassComponent';
99 import MAX_SIGNED_31_BIT_INT from './maxSigned31BitInt';
100
101 +const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
102 +
103 let didWarnAboutBadClass;
104 let didWarnAboutGetDerivedStateOnFunctionalComponent;
105 let didWarnAboutStatelessRefs;
packages/react-reconciler/src/ReactFiberScheduler.js
+2 -1
@@ -12,7 +12,7 @@ import type {FiberRoot, Batch} from './ReactFiberRoot';
12 import type {ExpirationTime} from './ReactFiberExpirationTime';
13
14 import ReactErrorUtils from 'shared/ReactErrorUtils';
15 -import {ReactCurrentOwner} from 'shared/ReactGlobalSharedState';
15 +import ReactSharedInternals from 'shared/ReactSharedInternals';
16 import ReactStrictModeWarnings from './ReactStrictModeWarnings';
17 import {
18 NoEffect,
@@ -155,6 +155,7 @@ export type Thenable = {
155 then(resolve: () => mixed, reject?: () => mixed): mixed,
156 };
157
158 +const {ReactCurrentOwner} = ReactSharedInternals;
159 const {
160 invokeGuardedCallback,
161 hasCaughtError,
packages/react-reconciler/src/ReactFiberTreeReflection.js
+3 -1
@@ -13,7 +13,7 @@ import invariant from 'shared/invariant';
13 import warning from 'shared/warning';
14
15 import * as ReactInstanceMap from 'shared/ReactInstanceMap';
16 -import {ReactCurrentOwner} from 'shared/ReactGlobalSharedState';
16 +import ReactSharedInternals from 'shared/ReactSharedInternals';
17 import getComponentName from 'shared/getComponentName';
18 import {
19 ClassComponent,
@@ -24,6 +24,8 @@ import {
24 } from 'shared/ReactTypeOfWork';
25 import {NoEffect, Placement} from 'shared/ReactTypeOfSideEffect';
26
27 +const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
28 +
29 const MOUNTING = 1;
30 const MOUNTED = 2;
31 const UNMOUNTED = 3;
packages/react/src/React.js
+2 -18
@@ -5,7 +5,6 @@
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 -import assign from 'object-assign';
8 import ReactVersion from 'shared/ReactVersion';
9 import {
10 REACT_ASYNC_MODE_TYPE,
@@ -19,7 +18,6 @@ import {enableSuspense} from 'shared/ReactFeatureFlags';
18 import {Component, PureComponent} from './ReactBaseClasses';
19 import {createRef} from './ReactCreateRef';
20 import {forEach, map, count, toArray, only} from './ReactChildren';
22 -import ReactCurrentOwner from './ReactCurrentOwner';
21 import {
22 createElement,
23 createFactory,
@@ -33,7 +31,7 @@ import {
31 createFactoryWithValidation,
32 cloneElementWithValidation,
33 } from './ReactElementValidator';
36 -import ReactDebugCurrentFrame from './ReactDebugCurrentFrame';
34 +import ReactSharedInternals from './ReactSharedInternals';
35
36 const React = {
37 Children: {
@@ -63,25 +61,11 @@ const React = {
61
62 version: ReactVersion,
63
66 - __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: {
67 - ReactCurrentOwner,
68 - // Used by renderers to avoid bundling object-assign twice in UMD bundles:
69 - assign,
70 - },
64 + __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: ReactSharedInternals,
65 };
66
67 if (enableSuspense) {
68 React.Placeholder = REACT_PLACEHOLDER_TYPE;
69 }
70
77 -if (__DEV__) {
78 - Object.assign(React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, {
79 - // These should not be included in production.
80 - ReactDebugCurrentFrame,
81 - // Shim for React DOM 16.0.0 which still destructured (but not used) this.
82 - // TODO: remove in React 17.0.
83 - ReactComponentTreeHook: {},
84 - });
85 -}
86 -
71 export default React;
packages/react/src/ReactSharedInternals.js new
+28
@@ -0,0 +1,28 @@
1 +/**
2 + * Copyright (c) 2013-present, Facebook, Inc.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + */
7 +
8 +import assign from 'object-assign';
9 +import ReactCurrentOwner from './ReactCurrentOwner';
10 +import ReactDebugCurrentFrame from './ReactDebugCurrentFrame';
11 +
12 +const ReactSharedInternals = {
13 + ReactCurrentOwner,
14 + // Used by renderers to avoid bundling object-assign twice in UMD bundles:
15 + assign,
16 +};
17 +
18 +if (__DEV__) {
19 + Object.assign(ReactSharedInternals, {
20 + // These should not be included in production.
21 + ReactDebugCurrentFrame,
22 + // Shim for React DOM 16.0.0 which still destructured (but not used) this.
23 + // TODO: remove in React 17.0.
24 + ReactComponentTreeHook: {},
25 + });
26 +}
27 +
28 +export default ReactSharedInternals;
packages/shared/ReactGlobalSharedState.js deleted
-15
@@ -1,15 +0,0 @@
1 -/**
2 - * Copyright (c) 2013-present, Facebook, Inc.
3 - *
4 - * This source code is licensed under the MIT license found in the
5 - * LICENSE file in the root directory of this source tree.
6 - */
7 -
8 -import React from 'react';
9 -
10 -const ReactInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
11 -
12 -export const ReactCurrentOwner = ReactInternals.ReactCurrentOwner;
13 -export const ReactDebugCurrentFrame = __DEV__
14 - ? ReactInternals.ReactDebugCurrentFrame
15 - : null;
packages/shared/ReactSharedInternals.js new
+13
@@ -0,0 +1,13 @@
1 +/**
2 + * Copyright (c) 2013-present, Facebook, Inc.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + */
7 +
8 +import React from 'react';
9 +
10 +const ReactSharedInternals =
11 + React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
12 +
13 +export default ReactSharedInternals;
scripts/jest/setupHostConfigs.js
+6
@@ -56,3 +56,9 @@ inlinedHostConfigs.forEach(rendererInfo => {
56 return renderer;
57 });
58 });
59 +
60 +// Make it possible to import this module inside
61 +// the React package itself.
62 +jest.mock('shared/ReactSharedInternals', () =>
63 + require.requireActual('react/src/ReactSharedInternals')
64 +);
scripts/rollup/forks.js
+9
@@ -38,6 +38,15 @@ const forks = Object.freeze({
38 return 'shared/forks/object-assign.umd.js';
39 },
40
41 + // Without this fork, importing `shared/ReactSharedInternals` inside
42 + // the `react` package itself would not work due to a cyclical dependency.
43 + 'shared/ReactSharedInternals': (bundleType, entry) => {
44 + if (entry === 'react') {
45 + return 'react/src/ReactSharedInternals';
46 + }
47 + return null;
48 + },
49 +
50 // We have a few forks for different environments.
51 'shared/ReactFeatureFlags': (bundleType, entry) => {
52 switch (entry) {