Refactor setup modules in src/private/setup as side-effect free modules (#45795)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45795

Changelog: [internal]

Our modules to set up the runtime have side-effects and depend on import order to work correctly. This is error-prone and complicates the migration to ESM in some cases, so this refactors all of them in `src/private/setup` to export a function instead.

Reviewed By: rshest

Differential Revision: D60382506

fbshipit-source-id: 9ac30b29659b74605d59eb97562d6cbf01f48e47
This commit is contained in:
Rubén Norte
2024-07-30 10:08:22 -07:00
committed by Facebook GitHub Bot
parent b74c4f6643
commit 48421df60a
5 changed files with 88 additions and 47 deletions
+1 -1
View File
@@ -29,7 +29,7 @@
const start = Date.now();
require('./setUpGlobals');
require('../../src/private/setup/setUpDOM');
require('../../src/private/setup/setUpDOM').default();
require('./setUpPerformance');
require('./setUpErrorHandling');
require('./polyfillPromise');
+14 -4
View File
@@ -11,8 +11,18 @@
import DOMRect from '../webapis/dom/geometry/DOMRect';
import DOMRectReadOnly from '../webapis/dom/geometry/DOMRectReadOnly';
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it
global.DOMRect = DOMRect;
let initialized = false;
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it
global.DOMRectReadOnly = DOMRectReadOnly;
export default function setUpDOM() {
if (initialized) {
return;
}
initialized = true;
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it
global.DOMRect = DOMRect;
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it
global.DOMRectReadOnly = DOMRectReadOnly;
}
@@ -10,7 +10,18 @@
import {polyfillGlobal} from '../../../Libraries/Utilities/PolyfillFunctions';
polyfillGlobal(
'IntersectionObserver',
() => require('../webapis/intersectionobserver/IntersectionObserver').default,
);
let initialized = false;
export default function setUpIntersectionObserver() {
if (initialized) {
return;
}
initialized = true;
polyfillGlobal(
'IntersectionObserver',
() =>
require('../webapis/intersectionobserver/IntersectionObserver').default,
);
}
@@ -10,7 +10,17 @@
import {polyfillGlobal} from '../../../Libraries/Utilities/PolyfillFunctions';
polyfillGlobal(
'MutationObserver',
() => require('../webapis/mutationobserver/MutationObserver').default,
);
let initialized = false;
export default function setUpMutationObserver() {
if (initialized) {
return;
}
initialized = true;
polyfillGlobal(
'MutationObserver',
() => require('../webapis/mutationobserver/MutationObserver').default,
);
}
@@ -10,44 +10,54 @@
import {polyfillGlobal} from '../../../Libraries/Utilities/PolyfillFunctions';
polyfillGlobal(
'PerformanceObserver',
() => require('../webapis/performance/PerformanceObserver').default,
);
let initialized = false;
polyfillGlobal(
'PerformanceObserverEntryList',
() =>
require('../webapis/performance/PerformanceObserver')
.PerformanceObserverEntryList,
);
export default function setUpPerformanceObserver() {
if (initialized) {
return;
}
polyfillGlobal(
'PerformanceEntry',
() => require('../webapis/performance/PerformanceEntry').PerformanceEntry,
);
initialized = true;
polyfillGlobal(
'PerformanceMark',
() => require('../webapis/performance/UserTiming').PerformanceMark,
);
polyfillGlobal(
'PerformanceObserver',
() => require('../webapis/performance/PerformanceObserver').default,
);
polyfillGlobal(
'PerformanceMeasure',
() => require('../webapis/performance/UserTiming').PerformanceMeasure,
);
polyfillGlobal(
'PerformanceObserverEntryList',
() =>
require('../webapis/performance/PerformanceObserver')
.PerformanceObserverEntryList,
);
polyfillGlobal(
'PerformanceEventTiming',
() => require('../webapis/performance/EventTiming').PerformanceEventTiming,
);
polyfillGlobal(
'PerformanceEntry',
() => require('../webapis/performance/PerformanceEntry').PerformanceEntry,
);
polyfillGlobal(
'TaskAttributionTiming',
() => require('../webapis/performance/LongTasks').TaskAttributionTiming,
);
polyfillGlobal(
'PerformanceMark',
() => require('../webapis/performance/UserTiming').PerformanceMark,
);
polyfillGlobal(
'PerformanceLongTaskTiming',
() => require('../webapis/performance/LongTasks').PerformanceLongTaskTiming,
);
polyfillGlobal(
'PerformanceMeasure',
() => require('../webapis/performance/UserTiming').PerformanceMeasure,
);
polyfillGlobal(
'PerformanceEventTiming',
() => require('../webapis/performance/EventTiming').PerformanceEventTiming,
);
polyfillGlobal(
'TaskAttributionTiming',
() => require('../webapis/performance/LongTasks').TaskAttributionTiming,
);
polyfillGlobal(
'PerformanceLongTaskTiming',
() => require('../webapis/performance/LongTasks').PerformanceLongTaskTiming,
);
}