Skip to content

Commit 3e809bf

Browse files
authored
Convert React Native builds to named exports (#18136)
These don't need any forks because we always export the same things atm.
1 parent 869dbda commit 3e809bf

File tree

5 files changed

+162
-153
lines changed

5 files changed

+162
-153
lines changed

packages/react-native-renderer/fabric.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
* @flow
88
*/
99

10-
'use strict';
10+
import type {ReactFabricType} from './src/ReactNativeTypes';
11+
import * as ReactFabric from './src/ReactFabric';
12+
// Assert that the exports line up with the type we're going to expose.
13+
// eslint-disable-next-line no-unused-expressions
14+
(ReactFabric: ReactFabricType);
1115

12-
const ReactFabric = require('./src/ReactFabric');
13-
14-
// TODO: decide on the top-level export form.
15-
// This is hacky but makes it work with both Rollup and Jest.
16-
module.exports = ReactFabric.default || ReactFabric;
16+
export * from './src/ReactFabric';

packages/react-native-renderer/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
* @flow
88
*/
99

10-
'use strict';
10+
import type {ReactNativeType} from './src/ReactNativeTypes';
11+
import * as ReactNative from './src/ReactNativeRenderer';
12+
// Assert that the exports line up with the type we're going to expose.
13+
// eslint-disable-next-line no-unused-expressions
14+
(ReactNative: ReactNativeType);
1115

12-
const ReactNativeRenderer = require('./src/ReactNativeRenderer');
13-
14-
// TODO: decide on the top-level export form.
15-
// This is hacky but makes it work with both Rollup and Jest.
16-
module.exports = ReactNativeRenderer.default || ReactNativeRenderer;
16+
export * from './src/ReactNativeRenderer';

packages/react-native-renderer/src/ReactFabric.js

Lines changed: 71 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @flow
88
*/
99

10-
import type {ReactFabricType, HostComponent} from './ReactNativeTypes';
10+
import type {HostComponent} from './ReactNativeTypes';
1111
import type {ReactNodeList} from 'shared/ReactTypes';
1212
import type {ElementRef} from 'react';
1313

@@ -26,7 +26,7 @@ import {
2626
getPublicRootInstance,
2727
} from 'react-reconciler/inline.fabric';
2828

29-
import {createPortal} from 'shared/ReactPortal';
29+
import {createPortal as createPortalImpl} from 'shared/ReactPortal';
3030
import {setBatchingImplementation} from 'legacy-events/ReactGenericBatching';
3131
import ReactVersion from 'shared/ReactVersion';
3232

@@ -144,6 +144,69 @@ function findNodeHandle(componentOrHandle: any): ?number {
144144
return hostInstance._nativeTag;
145145
}
146146

147+
function dispatchCommand(handle: any, command: string, args: Array<any>) {
148+
if (handle._nativeTag == null) {
149+
if (__DEV__) {
150+
console.error(
151+
"dispatchCommand was called with a ref that isn't a " +
152+
'native component. Use React.forwardRef to get access to the underlying native component',
153+
);
154+
}
155+
156+
return;
157+
}
158+
159+
if (handle._internalInstanceHandle) {
160+
nativeFabricUIManager.dispatchCommand(
161+
handle._internalInstanceHandle.stateNode.node,
162+
command,
163+
args,
164+
);
165+
} else {
166+
UIManager.dispatchViewManagerCommand(handle._nativeTag, command, args);
167+
}
168+
}
169+
170+
function render(
171+
element: React$Element<any>,
172+
containerTag: any,
173+
callback: ?Function,
174+
) {
175+
let root = roots.get(containerTag);
176+
177+
if (!root) {
178+
// TODO (bvaughn): If we decide to keep the wrapper component,
179+
// We could create a wrapper for containerTag as well to reduce special casing.
180+
root = createContainer(containerTag, LegacyRoot, false, null);
181+
roots.set(containerTag, root);
182+
}
183+
updateContainer(element, root, null, callback);
184+
185+
return getPublicRootInstance(root);
186+
}
187+
188+
function unmountComponentAtNode(containerTag: number) {
189+
this.stopSurface(containerTag);
190+
}
191+
192+
function stopSurface(containerTag: number) {
193+
const root = roots.get(containerTag);
194+
if (root) {
195+
// TODO: Is it safe to reset this now or should I wait since this unmount could be deferred?
196+
updateContainer(null, root, null, () => {
197+
roots.delete(containerTag);
198+
});
199+
}
200+
}
201+
202+
function createPortal(
203+
children: ReactNodeList,
204+
containerTag: number,
205+
key: ?string = null,
206+
) {
207+
return createPortalImpl(children, containerTag, null, key);
208+
}
209+
147210
setBatchingImplementation(
148211
batchedUpdatesImpl,
149212
discreteUpdates,
@@ -153,74 +216,18 @@ setBatchingImplementation(
153216

154217
const roots = new Map();
155218

156-
const ReactFabric: ReactFabricType = {
219+
export {
157220
// This is needed for implementation details of TouchableNativeFeedback
158221
// Remove this once TouchableNativeFeedback doesn't use cloneElement
159222
findHostInstance_DEPRECATED,
160223
findNodeHandle,
161-
162-
dispatchCommand(handle: any, command: string, args: Array<any>) {
163-
if (handle._nativeTag == null) {
164-
if (__DEV__) {
165-
console.error(
166-
"dispatchCommand was called with a ref that isn't a " +
167-
'native component. Use React.forwardRef to get access to the underlying native component',
168-
);
169-
}
170-
171-
return;
172-
}
173-
174-
if (handle._internalInstanceHandle) {
175-
nativeFabricUIManager.dispatchCommand(
176-
handle._internalInstanceHandle.stateNode.node,
177-
command,
178-
args,
179-
);
180-
} else {
181-
UIManager.dispatchViewManagerCommand(handle._nativeTag, command, args);
182-
}
183-
},
184-
185-
render(element: React$Element<any>, containerTag: any, callback: ?Function) {
186-
let root = roots.get(containerTag);
187-
188-
if (!root) {
189-
// TODO (bvaughn): If we decide to keep the wrapper component,
190-
// We could create a wrapper for containerTag as well to reduce special casing.
191-
root = createContainer(containerTag, LegacyRoot, false, null);
192-
roots.set(containerTag, root);
193-
}
194-
updateContainer(element, root, null, callback);
195-
196-
return getPublicRootInstance(root);
197-
},
198-
224+
dispatchCommand,
225+
render,
199226
// Deprecated - this function is being renamed to stopSurface, use that instead.
200227
// TODO (T47576999): Delete this once it's no longer called from native code.
201-
unmountComponentAtNode(containerTag: number) {
202-
this.stopSurface(containerTag);
203-
},
204-
205-
stopSurface(containerTag: number) {
206-
const root = roots.get(containerTag);
207-
if (root) {
208-
// TODO: Is it safe to reset this now or should I wait since this unmount could be deferred?
209-
updateContainer(null, root, null, () => {
210-
roots.delete(containerTag);
211-
});
212-
}
213-
},
214-
215-
createPortal(
216-
children: ReactNodeList,
217-
containerTag: number,
218-
key: ?string = null,
219-
) {
220-
return createPortal(children, containerTag, null, key);
221-
},
222-
223-
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: {},
228+
unmountComponentAtNode,
229+
stopSurface,
230+
createPortal,
224231
};
225232

226233
injectIntoDevTools({
@@ -230,5 +237,3 @@ injectIntoDevTools({
230237
version: ReactVersion,
231238
rendererPackageName: 'react-native-renderer',
232239
});
233-
234-
export default ReactFabric;

packages/react-native-renderer/src/ReactNativeRenderer.js

Lines changed: 79 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @flow
88
*/
99

10-
import type {ReactNativeType, HostComponent} from './ReactNativeTypes';
10+
import type {HostComponent} from './ReactNativeTypes';
1111
import type {ReactNodeList} from 'shared/ReactTypes';
1212

1313
import './ReactNativeInjection';
@@ -26,7 +26,7 @@ import {
2626
} from 'react-reconciler/inline.native';
2727
// TODO: direct imports like some-package/src/* are bad. Fix me.
2828
import {getStackByFiberInDevAndProd} from 'react-reconciler/src/ReactCurrentFiber';
29-
import {createPortal} from 'shared/ReactPortal';
29+
import {createPortal as createPortalImpl} from 'shared/ReactPortal';
3030
import {
3131
setBatchingImplementation,
3232
batchedUpdates,
@@ -144,6 +144,71 @@ function findNodeHandle(componentOrHandle: any): ?number {
144144
return hostInstance._nativeTag;
145145
}
146146

147+
function dispatchCommand(handle: any, command: string, args: Array<any>) {
148+
if (handle._nativeTag == null) {
149+
if (__DEV__) {
150+
console.error(
151+
"dispatchCommand was called with a ref that isn't a " +
152+
'native component. Use React.forwardRef to get access to the underlying native component',
153+
);
154+
}
155+
return;
156+
}
157+
158+
if (handle._internalInstanceHandle) {
159+
nativeFabricUIManager.dispatchCommand(
160+
handle._internalInstanceHandle.stateNode.node,
161+
command,
162+
args,
163+
);
164+
} else {
165+
UIManager.dispatchViewManagerCommand(handle._nativeTag, command, args);
166+
}
167+
}
168+
169+
function render(
170+
element: React$Element<any>,
171+
containerTag: any,
172+
callback: ?Function,
173+
) {
174+
let root = roots.get(containerTag);
175+
176+
if (!root) {
177+
// TODO (bvaughn): If we decide to keep the wrapper component,
178+
// We could create a wrapper for containerTag as well to reduce special casing.
179+
root = createContainer(containerTag, LegacyRoot, false, null);
180+
roots.set(containerTag, root);
181+
}
182+
updateContainer(element, root, null, callback);
183+
184+
return getPublicRootInstance(root);
185+
}
186+
187+
function unmountComponentAtNode(containerTag: number) {
188+
const root = roots.get(containerTag);
189+
if (root) {
190+
// TODO: Is it safe to reset this now or should I wait since this unmount could be deferred?
191+
updateContainer(null, root, null, () => {
192+
roots.delete(containerTag);
193+
});
194+
}
195+
}
196+
197+
function unmountComponentAtNodeAndRemoveContainer(containerTag: number) {
198+
unmountComponentAtNode(containerTag);
199+
200+
// Call back into native to remove all of the subviews from this container
201+
UIManager.removeRootView(containerTag);
202+
}
203+
204+
function createPortal(
205+
children: ReactNodeList,
206+
containerTag: number,
207+
key: ?string = null,
208+
) {
209+
return createPortalImpl(children, containerTag, null, key);
210+
}
211+
147212
setBatchingImplementation(
148213
batchedUpdatesImpl,
149214
discreteUpdates,
@@ -161,78 +226,22 @@ function computeComponentStackForErrorReporting(reactTag: number): string {
161226

162227
const roots = new Map();
163228

164-
const ReactNativeRenderer: ReactNativeType = {
229+
const Internals = {
230+
computeComponentStackForErrorReporting,
231+
};
232+
233+
export {
165234
// This is needed for implementation details of TouchableNativeFeedback
166235
// Remove this once TouchableNativeFeedback doesn't use cloneElement
167236
findHostInstance_DEPRECATED,
168237
findNodeHandle,
169-
170-
dispatchCommand(handle: any, command: string, args: Array<any>) {
171-
if (handle._nativeTag == null) {
172-
if (__DEV__) {
173-
console.error(
174-
"dispatchCommand was called with a ref that isn't a " +
175-
'native component. Use React.forwardRef to get access to the underlying native component',
176-
);
177-
}
178-
return;
179-
}
180-
181-
if (handle._internalInstanceHandle) {
182-
nativeFabricUIManager.dispatchCommand(
183-
handle._internalInstanceHandle.stateNode.node,
184-
command,
185-
args,
186-
);
187-
} else {
188-
UIManager.dispatchViewManagerCommand(handle._nativeTag, command, args);
189-
}
190-
},
191-
192-
render(element: React$Element<any>, containerTag: any, callback: ?Function) {
193-
let root = roots.get(containerTag);
194-
195-
if (!root) {
196-
// TODO (bvaughn): If we decide to keep the wrapper component,
197-
// We could create a wrapper for containerTag as well to reduce special casing.
198-
root = createContainer(containerTag, LegacyRoot, false, null);
199-
roots.set(containerTag, root);
200-
}
201-
updateContainer(element, root, null, callback);
202-
203-
return getPublicRootInstance(root);
204-
},
205-
206-
unmountComponentAtNode(containerTag: number) {
207-
const root = roots.get(containerTag);
208-
if (root) {
209-
// TODO: Is it safe to reset this now or should I wait since this unmount could be deferred?
210-
updateContainer(null, root, null, () => {
211-
roots.delete(containerTag);
212-
});
213-
}
214-
},
215-
216-
unmountComponentAtNodeAndRemoveContainer(containerTag: number) {
217-
ReactNativeRenderer.unmountComponentAtNode(containerTag);
218-
219-
// Call back into native to remove all of the subviews from this container
220-
UIManager.removeRootView(containerTag);
221-
},
222-
223-
createPortal(
224-
children: ReactNodeList,
225-
containerTag: number,
226-
key: ?string = null,
227-
) {
228-
return createPortal(children, containerTag, null, key);
229-
},
230-
231-
unstable_batchedUpdates: batchedUpdates,
232-
233-
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: {
234-
computeComponentStackForErrorReporting,
235-
},
238+
dispatchCommand,
239+
render,
240+
unmountComponentAtNode,
241+
unmountComponentAtNodeAndRemoveContainer,
242+
createPortal,
243+
batchedUpdates as unstable_batchedUpdates,
244+
Internals as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
236245
};
237246

238247
injectIntoDevTools({
@@ -242,5 +251,3 @@ injectIntoDevTools({
242251
version: ReactVersion,
243252
rendererPackageName: 'react-native-renderer',
244253
});
245-
246-
export default ReactNativeRenderer;

0 commit comments

Comments
 (0)