Skip to content

Commit ea8a861

Browse files
authored
refactor: use ESM exports in ReactNativeViewConfigRegistry (#27508)
## Summary When transpiling `react-native` with `swc` this file caused some trouble as it mixes ESM and CJS import/export syntax. This PR addresses this by converting CJS exports to ESM exports. As `ReactNativeViewConfigRegistry` is synced from `react` to `react-native` repository, it's required to make the change here. I've also aligned the mock of `ReactNativeViewConfigRegistry` to reflect current implementation. Related PR in `react-native`: facebook/react-native#40787
1 parent e61a60f commit ea8a861

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/ReactNativeViewConfigRegistry.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,16 @@
99

1010
'use strict';
1111

12-
import type {
13-
ReactNativeBaseComponentViewConfig,
14-
ViewConfigGetter,
15-
} from './ReactNativeTypes';
12+
import {type ViewConfig} from './ReactNativeTypes';
1613

1714
// Event configs
18-
const customBubblingEventTypes = {};
19-
const customDirectEventTypes = {};
20-
const eventTypes = {};
21-
22-
exports.customBubblingEventTypes = customBubblingEventTypes;
23-
exports.customDirectEventTypes = customDirectEventTypes;
24-
exports.eventTypes = eventTypes;
15+
export const customBubblingEventTypes = {};
16+
export const customDirectEventTypes = {};
2517

2618
const viewConfigCallbacks = new Map();
2719
const viewConfigs = new Map();
2820

29-
function processEventTypes(
30-
viewConfig: ReactNativeBaseComponentViewConfig<>,
31-
): void {
21+
function processEventTypes(viewConfig: ViewConfig): void {
3222
const {bubblingEventTypes, directEventTypes} = viewConfig;
3323

3424
if (__DEV__) {
@@ -46,7 +36,7 @@ function processEventTypes(
4636
if (bubblingEventTypes != null) {
4737
for (const topLevelType in bubblingEventTypes) {
4838
if (customBubblingEventTypes[topLevelType] == null) {
49-
eventTypes[topLevelType] = customBubblingEventTypes[topLevelType] =
39+
customBubblingEventTypes[topLevelType] =
5040
bubblingEventTypes[topLevelType];
5141
}
5242
}
@@ -55,8 +45,7 @@ function processEventTypes(
5545
if (directEventTypes != null) {
5646
for (const topLevelType in directEventTypes) {
5747
if (customDirectEventTypes[topLevelType] == null) {
58-
eventTypes[topLevelType] = customDirectEventTypes[topLevelType] =
59-
directEventTypes[topLevelType];
48+
customDirectEventTypes[topLevelType] = directEventTypes[topLevelType];
6049
}
6150
}
6251
}
@@ -66,9 +55,8 @@ function processEventTypes(
6655
* Registers a native view/component by name.
6756
* A callback is provided to load the view config from UIManager.
6857
* The callback is deferred until the view is actually rendered.
69-
* This is done to avoid causing Prepack deopts.
7058
*/
71-
exports.register = function (name: string, callback: ViewConfigGetter): string {
59+
export function register(name: string, callback: () => ViewConfig): string {
7260
if (viewConfigCallbacks.has(name)) {
7361
throw new Error(`Tried to register two views with the same name ${name}`);
7462
}
@@ -83,14 +71,14 @@ exports.register = function (name: string, callback: ViewConfigGetter): string {
8371

8472
viewConfigCallbacks.set(name, callback);
8573
return name;
86-
};
74+
}
8775

8876
/**
8977
* Retrieves a config for the specified view.
9078
* If this is the first time the view has been used,
9179
* This configuration will be lazy-loaded from UIManager.
9280
*/
93-
exports.get = function (name: string): ReactNativeBaseComponentViewConfig<> {
81+
export function get(name: string): ViewConfig {
9482
let viewConfig;
9583
if (!viewConfigs.has(name)) {
9684
const callback = viewConfigCallbacks.get(name);
@@ -121,4 +109,4 @@ exports.get = function (name: string): ReactNativeBaseComponentViewConfig<> {
121109
}
122110

123111
return viewConfig;
124-
};
112+
}

scripts/rollup/shims/react-native/ReactNativeViewConfigRegistry.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {type ViewConfig} from './ReactNativeTypes';
1414
import invariant from 'invariant';
1515

1616
// Event configs
17-
const customBubblingEventTypes: {
17+
export const customBubblingEventTypes: {
1818
[eventName: string]: $ReadOnly<{
1919
phasedRegistrationNames: $ReadOnly<{
2020
captured: string,
@@ -24,16 +24,13 @@ const customBubblingEventTypes: {
2424
}>,
2525
...
2626
} = {};
27-
const customDirectEventTypes: {
27+
export const customDirectEventTypes: {
2828
[eventName: string]: $ReadOnly<{
2929
registrationName: string,
3030
}>,
3131
...
3232
} = {};
3333

34-
exports.customBubblingEventTypes = customBubblingEventTypes;
35-
exports.customDirectEventTypes = customDirectEventTypes;
36-
3734
const viewConfigCallbacks = new Map<string, ?() => ViewConfig>();
3835
const viewConfigs = new Map<string, ViewConfig>();
3936

@@ -75,7 +72,7 @@ function processEventTypes(viewConfig: ViewConfig): void {
7572
* A callback is provided to load the view config from UIManager.
7673
* The callback is deferred until the view is actually rendered.
7774
*/
78-
exports.register = function (name: string, callback: () => ViewConfig): string {
75+
export function register(name: string, callback: () => ViewConfig): string {
7976
invariant(
8077
!viewConfigCallbacks.has(name),
8178
'Tried to register two views with the same name %s',
@@ -89,14 +86,14 @@ exports.register = function (name: string, callback: () => ViewConfig): string {
8986
);
9087
viewConfigCallbacks.set(name, callback);
9188
return name;
92-
};
89+
}
9390

9491
/**
9592
* Retrieves a config for the specified view.
9693
* If this is the first time the view has been used,
9794
* This configuration will be lazy-loaded from UIManager.
9895
*/
99-
exports.get = function (name: string): ViewConfig {
96+
export function get(name: string): ViewConfig {
10097
let viewConfig;
10198
if (!viewConfigs.has(name)) {
10299
const callback = viewConfigCallbacks.get(name);
@@ -124,4 +121,4 @@ exports.get = function (name: string): ViewConfig {
124121
}
125122
invariant(viewConfig, 'View config not found for name %s', name);
126123
return viewConfig;
127-
};
124+
}

0 commit comments

Comments
 (0)