Skip to content

Commit

Permalink
Remove import * as pattern from the codebase (facebook#14282)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sebmarkbage authored and n8schloss committed Jan 31, 2019
1 parent 451fa84 commit d035da1
Show file tree
Hide file tree
Showing 34 changed files with 467 additions and 390 deletions.
16 changes: 10 additions & 6 deletions packages/react-art/src/ReactART.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

import React from 'react';
import ReactVersion from 'shared/ReactVersion';
import * as ARTRenderer from 'react-reconciler/inline.art';
import {
createContainer,
updateContainer,
injectIntoDevTools,
} from 'react-reconciler/inline.art';
import Transform from 'art/core/transform';
import Mode from 'art/modes/current';
import FastNoSideEffects from 'art/modes/fast-noSideEffects';
Expand Down Expand Up @@ -61,8 +65,8 @@ class Surface extends React.Component {

this._surface = Mode.Surface(+width, +height, this._tagRef);

this._mountNode = ARTRenderer.createContainer(this._surface);
ARTRenderer.updateContainer(this.props.children, this._mountNode, this);
this._mountNode = createContainer(this._surface);
updateContainer(this.props.children, this._mountNode, this);
}

componentDidUpdate(prevProps, prevState) {
Expand All @@ -72,15 +76,15 @@ class Surface extends React.Component {
this._surface.resize(+props.width, +props.height);
}

ARTRenderer.updateContainer(this.props.children, this._mountNode, this);
updateContainer(this.props.children, this._mountNode, this);

if (this._surface.render) {
this._surface.render();
}
}

componentWillUnmount() {
ARTRenderer.updateContainer(null, this._mountNode, this);
updateContainer(null, this._mountNode, this);
}

render() {
Expand Down Expand Up @@ -132,7 +136,7 @@ class Text extends React.Component {
}
}

ARTRenderer.injectIntoDevTools({
injectIntoDevTools({
findFiberByHostInstance: () => null,
bundleType: __DEV__ ? 1 : 0,
version: ReactVersion,
Expand Down
144 changes: 84 additions & 60 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,42 @@ import type {Container} from './ReactDOMHostConfig';
import '../shared/checkReact';
import './ReactDOMClientInjection';

import * as DOMRenderer from 'react-reconciler/inline.dom';
import * as ReactPortal from 'shared/ReactPortal';
import {
computeUniqueAsyncExpiration,
findHostInstanceWithNoPortals,
updateContainerAtExpirationTime,
flushRoot,
createContainer,
updateContainer,
batchedUpdates,
unbatchedUpdates,
interactiveUpdates,
flushInteractiveUpdates,
flushSync,
flushControlled,
injectIntoDevTools,
getPublicRootInstance,
findHostInstance,
findHostInstanceWithWarning,
} from 'react-reconciler/inline.dom';
import {createPortal as createPortalImpl} from 'shared/ReactPortal';
import {canUseDOM} from 'shared/ExecutionEnvironment';
import * as ReactGenericBatching from 'events/ReactGenericBatching';
import * as ReactControlledComponent from 'events/ReactControlledComponent';
import * as EventPluginHub from 'events/EventPluginHub';
import * as EventPluginRegistry from 'events/EventPluginRegistry';
import * as EventPropagators from 'events/EventPropagators';
import * as ReactInstanceMap from 'shared/ReactInstanceMap';
import {setBatchingImplementation} from 'events/ReactGenericBatching';
import {
setRestoreImplementation,
enqueueStateRestore,
restoreStateIfNeeded,
} from 'events/ReactControlledComponent';
import {
injection as EventPluginHubInjection,
runEventsInBatch,
} from 'events/EventPluginHub';
import {eventNameDispatchConfigs} from 'events/EventPluginRegistry';
import {
accumulateTwoPhaseDispatches,
accumulateDirectDispatches,
} from 'events/EventPropagators';
import {has as hasInstance} from 'shared/ReactInstanceMap';
import ReactVersion from 'shared/ReactVersion';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import getComponentName from 'shared/getComponentName';
Expand All @@ -36,9 +63,14 @@ import lowPriorityWarning from 'shared/lowPriorityWarning';
import warningWithoutStack from 'shared/warningWithoutStack';
import {enableStableConcurrentModeAPIs} from 'shared/ReactFeatureFlags';

import * as ReactDOMComponentTree from './ReactDOMComponentTree';
import {
getInstanceFromNode,
getNodeFromInstance,
getFiberCurrentPropsFromNode,
getClosestInstanceFromNode,
} from './ReactDOMComponentTree';
import {restoreControlledState} from './ReactDOMComponent';
import * as ReactDOMEventListener from '../events/ReactDOMEventListener';
import {dispatchEvent} from '../events/ReactDOMEventListener';
import {
ELEMENT_NODE,
COMMENT_NODE,
Expand Down Expand Up @@ -74,7 +106,7 @@ if (__DEV__) {

topLevelUpdateWarnings = (container: DOMContainer) => {
if (container._reactRootContainer && container.nodeType !== COMMENT_NODE) {
const hostInstance = DOMRenderer.findHostInstanceWithNoPortals(
const hostInstance = findHostInstanceWithNoPortals(
container._reactRootContainer._internalRoot.current,
);
if (hostInstance) {
Expand All @@ -90,9 +122,7 @@ if (__DEV__) {

const isRootRenderedBySomeReact = !!container._reactRootContainer;
const rootEl = getReactRootElementInContainer(container);
const hasNonRootReactChild = !!(
rootEl && ReactDOMComponentTree.getInstanceFromNode(rootEl)
);
const hasNonRootReactChild = !!(rootEl && getInstanceFromNode(rootEl));

warningWithoutStack(
!hasNonRootReactChild || isRootRenderedBySomeReact,
Expand Down Expand Up @@ -125,7 +155,7 @@ if (__DEV__) {
};
}

ReactControlledComponent.setRestoreImplementation(restoreControlledState);
setRestoreImplementation(restoreControlledState);

export type DOMContainer =
| (Element & {
Expand Down Expand Up @@ -165,7 +195,7 @@ type Root = {
};

function ReactBatch(root: ReactRoot) {
const expirationTime = DOMRenderer.computeUniqueAsyncExpiration();
const expirationTime = computeUniqueAsyncExpiration();
this._expirationTime = expirationTime;
this._root = root;
this._next = null;
Expand All @@ -185,7 +215,7 @@ ReactBatch.prototype.render = function(children: ReactNodeList) {
const internalRoot = this._root._internalRoot;
const expirationTime = this._expirationTime;
const work = new ReactWork();
DOMRenderer.updateContainerAtExpirationTime(
updateContainerAtExpirationTime(
children,
internalRoot,
null,
Expand Down Expand Up @@ -256,7 +286,7 @@ ReactBatch.prototype.commit = function() {

// Synchronously flush all the work up to this batch's expiration time.
this._defer = false;
DOMRenderer.flushRoot(internalRoot, expirationTime);
flushRoot(internalRoot, expirationTime);

// Pop the batch from the list.
const next = this._next;
Expand Down Expand Up @@ -336,7 +366,7 @@ function ReactRoot(
isConcurrent: boolean,
hydrate: boolean,
) {
const root = DOMRenderer.createContainer(container, isConcurrent, hydrate);
const root = createContainer(container, isConcurrent, hydrate);
this._internalRoot = root;
}
ReactRoot.prototype.render = function(
Expand All @@ -352,7 +382,7 @@ ReactRoot.prototype.render = function(
if (callback !== null) {
work.then(callback);
}
DOMRenderer.updateContainer(children, root, null, work._onCommit);
updateContainer(children, root, null, work._onCommit);
return work;
};
ReactRoot.prototype.unmount = function(callback: ?() => mixed): Work {
Expand All @@ -365,7 +395,7 @@ ReactRoot.prototype.unmount = function(callback: ?() => mixed): Work {
if (callback !== null) {
work.then(callback);
}
DOMRenderer.updateContainer(null, root, null, work._onCommit);
updateContainer(null, root, null, work._onCommit);
return work;
};
ReactRoot.prototype.legacy_renderSubtreeIntoContainer = function(
Expand All @@ -382,7 +412,7 @@ ReactRoot.prototype.legacy_renderSubtreeIntoContainer = function(
if (callback !== null) {
work.then(callback);
}
DOMRenderer.updateContainer(children, root, parentComponent, work._onCommit);
updateContainer(children, root, parentComponent, work._onCommit);
return work;
};
ReactRoot.prototype.createBatch = function(): Batch {
Expand Down Expand Up @@ -453,10 +483,10 @@ function shouldHydrateDueToLegacyHeuristic(container) {
);
}

ReactGenericBatching.setBatchingImplementation(
DOMRenderer.batchedUpdates,
DOMRenderer.interactiveUpdates,
DOMRenderer.flushInteractiveUpdates,
setBatchingImplementation(
batchedUpdates,
interactiveUpdates,
flushInteractiveUpdates,
);

let warnedAboutHydrateAPI = false;
Expand Down Expand Up @@ -535,12 +565,12 @@ function legacyRenderSubtreeIntoContainer(
if (typeof callback === 'function') {
const originalCallback = callback;
callback = function() {
const instance = DOMRenderer.getPublicRootInstance(root._internalRoot);
const instance = getPublicRootInstance(root._internalRoot);
originalCallback.call(instance);
};
}
// Initial mount should not be batched.
DOMRenderer.unbatchedUpdates(() => {
unbatchedUpdates(() => {
if (parentComponent != null) {
root.legacy_renderSubtreeIntoContainer(
parentComponent,
Expand All @@ -555,7 +585,7 @@ function legacyRenderSubtreeIntoContainer(
if (typeof callback === 'function') {
const originalCallback = callback;
callback = function() {
const instance = DOMRenderer.getPublicRootInstance(root._internalRoot);
const instance = getPublicRootInstance(root._internalRoot);
originalCallback.call(instance);
};
}
Expand All @@ -570,7 +600,7 @@ function legacyRenderSubtreeIntoContainer(
root.render(children, callback);
}
}
return DOMRenderer.getPublicRootInstance(root._internalRoot);
return getPublicRootInstance(root._internalRoot);
}

function createPortal(
Expand All @@ -583,7 +613,7 @@ function createPortal(
'Target container is not a DOM element.',
);
// TODO: pass ReactDOM portal implementation as third argument
return ReactPortal.createPortal(children, container, null, key);
return createPortalImpl(children, container, null, key);
}

const ReactDOM: Object = {
Expand Down Expand Up @@ -616,12 +646,9 @@ const ReactDOM: Object = {
return (componentOrElement: any);
}
if (__DEV__) {
return DOMRenderer.findHostInstanceWithWarning(
componentOrElement,
'findDOMNode',
);
return findHostInstanceWithWarning(componentOrElement, 'findDOMNode');
}
return DOMRenderer.findHostInstance(componentOrElement);
return findHostInstance(componentOrElement);
},

hydrate(element: React$Node, container: DOMContainer, callback: ?Function) {
Expand Down Expand Up @@ -656,7 +683,7 @@ const ReactDOM: Object = {
callback: ?Function,
) {
invariant(
parentComponent != null && ReactInstanceMap.has(parentComponent),
parentComponent != null && hasInstance(parentComponent),
'parentComponent must be a valid React Component',
);
return legacyRenderSubtreeIntoContainer(
Expand All @@ -677,8 +704,7 @@ const ReactDOM: Object = {
if (container._reactRootContainer) {
if (__DEV__) {
const rootEl = getReactRootElementInContainer(container);
const renderedByDifferentReact =
rootEl && !ReactDOMComponentTree.getInstanceFromNode(rootEl);
const renderedByDifferentReact = rootEl && !getInstanceFromNode(rootEl);
warningWithoutStack(
!renderedByDifferentReact,
"unmountComponentAtNode(): The node you're attempting to unmount " +
Expand All @@ -687,7 +713,7 @@ const ReactDOM: Object = {
}

// Unmount should not be batched.
DOMRenderer.unbatchedUpdates(() => {
unbatchedUpdates(() => {
legacyRenderSubtreeIntoContainer(null, null, container, false, () => {
container._reactRootContainer = null;
});
Expand All @@ -698,9 +724,7 @@ const ReactDOM: Object = {
} else {
if (__DEV__) {
const rootEl = getReactRootElementInContainer(container);
const hasNonRootReactChild = !!(
rootEl && ReactDOMComponentTree.getInstanceFromNode(rootEl)
);
const hasNonRootReactChild = !!(rootEl && getInstanceFromNode(rootEl));

// Check if the container itself is a React root node.
const isContainerReactRoot =
Expand Down Expand Up @@ -740,29 +764,29 @@ const ReactDOM: Object = {
return createPortal(...args);
},

unstable_batchedUpdates: DOMRenderer.batchedUpdates,
unstable_batchedUpdates: batchedUpdates,

unstable_interactiveUpdates: DOMRenderer.interactiveUpdates,
unstable_interactiveUpdates: interactiveUpdates,

flushSync: DOMRenderer.flushSync,
flushSync: flushSync,

unstable_flushControlled: DOMRenderer.flushControlled,
unstable_flushControlled: flushControlled,

__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: {
// Keep in sync with ReactDOMUnstableNativeDependencies.js
// and ReactTestUtils.js. This is an array for better minification.
Events: [
ReactDOMComponentTree.getInstanceFromNode,
ReactDOMComponentTree.getNodeFromInstance,
ReactDOMComponentTree.getFiberCurrentPropsFromNode,
EventPluginHub.injection.injectEventPluginsByName,
EventPluginRegistry.eventNameDispatchConfigs,
EventPropagators.accumulateTwoPhaseDispatches,
EventPropagators.accumulateDirectDispatches,
ReactControlledComponent.enqueueStateRestore,
ReactControlledComponent.restoreStateIfNeeded,
ReactDOMEventListener.dispatchEvent,
EventPluginHub.runEventsInBatch,
getInstanceFromNode,
getNodeFromInstance,
getFiberCurrentPropsFromNode,
EventPluginHubInjection.injectEventPluginsByName,
eventNameDispatchConfigs,
accumulateTwoPhaseDispatches,
accumulateDirectDispatches,
enqueueStateRestore,
restoreStateIfNeeded,
dispatchEvent,
runEventsInBatch,
],
},
};
Expand Down Expand Up @@ -790,8 +814,8 @@ if (enableStableConcurrentModeAPIs) {
ReactDOM.unstable_createRoot = createRoot;
}

const foundDevTools = DOMRenderer.injectIntoDevTools({
findFiberByHostInstance: ReactDOMComponentTree.getClosestInstanceFromNode,
const foundDevTools = injectIntoDevTools({
findFiberByHostInstance: getClosestInstanceFromNode,
bundleType: __DEV__ ? 1 : 0,
version: ReactVersion,
rendererPackageName: 'react-dom',
Expand Down
Loading

0 comments on commit d035da1

Please sign in to comment.