Skip to content

Commit 3d422e8

Browse files
authored
Remove import * as pattern from the codebase (#14282)
Whenever we do this, Rollup needs to materialize this as an object. This causes it to also add the Babel compatibility property which is unnecessary bloat. However, since when we use these, we leak the object this often also deopts any compiler optimizations. If we really need an object we should export default an object. Currently there is an exception for DOMTopLevelEventTypes since listing out the imports is a PITA and it doesn't escape so it should get properly inlined. We should probably move to a different pattern to avoid this for consistency though.
1 parent 86158b2 commit 3d422e8

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

src/ReactTestHostConfig.js

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

1010
import warning from 'shared/warning';
11-
import * as TestRendererScheduling from './ReactTestRendererScheduling';
11+
import {
12+
nowImplementation as TestRendererSchedulingNowImplementation,
13+
scheduleDeferredCallback as TestRendererSchedulingScheduleDeferredCallback,
14+
cancelDeferredCallback as TestRendererSchedulingCancelDeferredCallback,
15+
shouldYield as TestRendererSchedulingShouldYield,
16+
} from './ReactTestRendererScheduling';
1217

1318
export type Type = string;
1419
export type Props = Object;
@@ -197,12 +202,10 @@ export function createTextInstance(
197202
export const isPrimaryRenderer = false;
198203
// This approach enables `now` to be mocked by tests,
199204
// Even after the reconciler has initialized and read host config values.
200-
export const now = () => TestRendererScheduling.nowImplementation();
201-
export const scheduleDeferredCallback =
202-
TestRendererScheduling.scheduleDeferredCallback;
203-
export const cancelDeferredCallback =
204-
TestRendererScheduling.cancelDeferredCallback;
205-
export const shouldYield = TestRendererScheduling.shouldYield;
205+
export const now = () => TestRendererSchedulingNowImplementation();
206+
export const scheduleDeferredCallback = TestRendererSchedulingScheduleDeferredCallback;
207+
export const cancelDeferredCallback = TestRendererSchedulingCancelDeferredCallback;
208+
export const shouldYield = TestRendererSchedulingShouldYield;
206209

207210
export const scheduleTimeout = setTimeout;
208211
export const cancelTimeout = clearTimeout;

src/ReactTestRenderer.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import type {Fiber} from 'react-reconciler/src/ReactFiber';
1111
import type {FiberRoot} from 'react-reconciler/src/ReactFiberRoot';
1212
import type {Instance, TextInstance} from './ReactTestHostConfig';
1313

14-
import * as TestRenderer from 'react-reconciler/inline.test';
14+
import {
15+
getPublicRootInstance,
16+
createContainer,
17+
updateContainer,
18+
flushSync,
19+
injectIntoDevTools,
20+
} from 'react-reconciler/inline.test';
1521
import {batchedUpdates} from 'events/ReactGenericBatching';
1622
import {findCurrentFiberUsingSlowPath} from 'react-reconciler/reflection';
1723
import {
@@ -34,8 +40,14 @@ import {
3440
import invariant from 'shared/invariant';
3541
import ReactVersion from 'shared/ReactVersion';
3642

37-
import * as ReactTestHostConfig from './ReactTestHostConfig';
38-
import * as TestRendererScheduling from './ReactTestRendererScheduling';
43+
import {getPublicInstance} from './ReactTestHostConfig';
44+
import {
45+
flushAll,
46+
flushNumberOfYields,
47+
clearYields,
48+
setNowImplementation,
49+
yieldValue,
50+
} from './ReactTestRendererScheduling';
3951

4052
type TestRendererOptions = {
4153
createNodeMock: (element: React$Element<any>) => any,
@@ -277,7 +289,7 @@ class ReactTestInstance {
277289

278290
get instance() {
279291
if (this._fiber.tag === HostComponent) {
280-
return ReactTestHostConfig.getPublicInstance(this._fiber.stateNode);
292+
return getPublicInstance(this._fiber.stateNode);
281293
} else {
282294
return this._fiber.stateNode;
283295
}
@@ -428,13 +440,13 @@ const ReactTestRendererFiber = {
428440
createNodeMock,
429441
tag: 'CONTAINER',
430442
};
431-
let root: FiberRoot | null = TestRenderer.createContainer(
443+
let root: FiberRoot | null = createContainer(
432444
container,
433445
isConcurrent,
434446
false,
435447
);
436448
invariant(root != null, 'something went wrong');
437-
TestRenderer.updateContainer(element, root, null, null);
449+
updateContainer(element, root, null, null);
438450

439451
const entry = {
440452
root: undefined, // makes flow happy
@@ -475,30 +487,30 @@ const ReactTestRendererFiber = {
475487
if (root == null || root.current == null) {
476488
return;
477489
}
478-
TestRenderer.updateContainer(newElement, root, null, null);
490+
updateContainer(newElement, root, null, null);
479491
},
480492
unmount() {
481493
if (root == null || root.current == null) {
482494
return;
483495
}
484-
TestRenderer.updateContainer(null, root, null, null);
496+
updateContainer(null, root, null, null);
485497
container = null;
486498
root = null;
487499
},
488500
getInstance() {
489501
if (root == null || root.current == null) {
490502
return null;
491503
}
492-
return TestRenderer.getPublicRootInstance(root);
504+
return getPublicRootInstance(root);
493505
},
494506

495-
unstable_flushAll: TestRendererScheduling.flushAll,
507+
unstable_flushAll: flushAll,
496508
unstable_flushSync<T>(fn: () => T): T {
497-
TestRendererScheduling.clearYields();
498-
return TestRenderer.flushSync(fn);
509+
clearYields();
510+
return flushSync(fn);
499511
},
500-
unstable_flushNumberOfYields: TestRendererScheduling.flushNumberOfYields,
501-
unstable_clearYields: TestRendererScheduling.clearYields,
512+
unstable_flushNumberOfYields: flushNumberOfYields,
513+
unstable_clearYields: clearYields,
502514
};
503515

504516
Object.defineProperty(
@@ -529,14 +541,14 @@ const ReactTestRendererFiber = {
529541
return entry;
530542
},
531543

532-
unstable_yield: TestRendererScheduling.yieldValue,
533-
unstable_clearYields: TestRendererScheduling.clearYields,
544+
unstable_yield: yieldValue,
545+
unstable_clearYields: clearYields,
534546

535547
/* eslint-disable camelcase */
536548
unstable_batchedUpdates: batchedUpdates,
537549
/* eslint-enable camelcase */
538550

539-
unstable_setNowImplementation: TestRendererScheduling.setNowImplementation,
551+
unstable_setNowImplementation: setNowImplementation,
540552
};
541553

542554
const fiberToWrapper = new WeakMap();
@@ -553,7 +565,7 @@ function wrapFiber(fiber: Fiber): ReactTestInstance {
553565
}
554566

555567
// Enable ReactTestRenderer to be used to test DevTools integration.
556-
TestRenderer.injectIntoDevTools({
568+
injectIntoDevTools({
557569
findFiberByHostInstance: (() => {
558570
throw new Error('TestRenderer does not support findFiberByHostInstance()');
559571
}: any),

0 commit comments

Comments
 (0)