Skip to content

Commit 60016c4

Browse files
authored
Export React as Named Exports instead of CommonJS (#18106)
* Add options for forked entry points We currently fork .fb.js entry points. This adds a few more options. .modern.fb.js - experimental FB builds .classic.fb.js - stable FB builds .fb.js - if no other FB build, use this for FB builds .experimental.js - experimental builds .stable.js - stable builds .js - used if no other override exists This will be used to have different ES exports for different builds. * Switch React to named exports * Export named exports from the export point itself We need to re-export the Flow exported types so we can use them in our code. We don't want to use the Flow types from upstream since it doesn't have the non-public APIs that we have. This should be able to use export * but I don't know why it doesn't work. This actually enables Flow typing of React which was just "any" before. This exposed some Flow errors that needs fixing. * Create forks for the react entrypoint None of our builds expose all exports and they all differ in at least one way, so we need four forks. * Set esModule flag to false We don't want to emit the esModule compatibility flag on our CommonJS output. For now we treat our named exports as if they're CommonJS. This is a potentially breaking change for scheduler (but all those apis are unstable), react-is and use-subscription. However, it seems unlikely that anyone would rely on this since these only have named exports. * Remove unused Feature Flags * Let jest observe the stable fork for stable tests This lets it do the negative test by ensuring that the right tests fail. However, this in turn will make other tests that are not behind __EXPERIMENTAL__ fail. So I need to do that next. * Put all tests that depend on exports behind __EXPERIMENTAL__ Since there's no way to override the exports using feature flags in .intern.js anymore we can't use these APIs in stable. The tradeoff here is that we can either enable the negative tests on "stable" that means experimental are expected to fail, or we can disable tests on stable. This is unfortunate since some of these APIs now run on a "stable" config at FB instead of the experimental. * Switch ReactDOM to named exports Same strategy as React. I moved the ReactDOMFB runtime injection to classic.fb.js Since we only fork the entrypoint, the `/testing` entrypoint needs to be forked too to re-export the same things plus `act`. This is a bit unfortunate. If it becomes a pattern we can consider forking in the module resolution deeply. fix flow * Fix ReactDOM Flow Types Now that ReactDOM is Flow type checked we need to fix up its types. * Configure jest to use stable entry for ReactDOM in non-experimental * Remove additional FeatureFlags that are no longer needed These are only flagging the exports and no implementation details so we can control them fully through the export overrides.
1 parent 8d7535e commit 60016c4

File tree

62 files changed

+1073
-670
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1073
-670
lines changed

packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.internal.js

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ describe('ReactHooksInspection', () => {
2222
ReactDebugTools = require('react-debug-tools');
2323
});
2424

25+
if (!__EXPERIMENTAL__) {
26+
it("empty test so Jest doesn't complain", () => {});
27+
return;
28+
}
29+
2530
it('should inspect a simple useResponder hook', () => {
2631
const TestResponder = React.DEPRECATED_createResponder('TestResponder', {});
2732

packages/react-devtools-shared/src/devtools/ContextMenu/useContextMenu.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@ export default function useContextMenu({
1919
}: {|
2020
data: Object,
2121
id: string,
22-
ref: ElementRef<HTMLElement>,
22+
ref: {current: ElementRef<'div'> | null},
2323
|}) {
2424
const {showMenu} = useContext(RegistryContext);
2525

2626
useEffect(() => {
2727
if (ref.current !== null) {
28-
const handleContextMenu = event => {
28+
const handleContextMenu = (event: MouseEvent | TouchEvent) => {
2929
event.preventDefault();
3030
event.stopPropagation();
3131

32-
const pageX = event.pageX || (event.touches && event.touches[0].pageX);
33-
const pageY = event.pageY || (event.touches && event.touches[0].pageY);
32+
const pageX =
33+
event.pageX ||
34+
(event.touches && ((event: any): TouchEvent).touches[0].pageX);
35+
const pageY =
36+
event.pageY ||
37+
(event.touches && ((event: any): TouchEvent).touches[0].pageY);
3438

3539
showMenu({data, id, pageX, pageY});
3640
};
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import {addUserTimingListener} from 'shared/ReactFeatureFlags';
11+
import {isEnabled} from './src/events/ReactDOMEventListener';
12+
import {getClosestInstanceFromNode} from './src/client/ReactDOMComponentTree';
13+
14+
import {__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/client/ReactDOM';
15+
16+
// For classic WWW builds, include a few internals that are already in use.
17+
Object.assign((__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: any), {
18+
ReactBrowserEventEmitter: {
19+
isEnabled,
20+
},
21+
ReactDOMComponentTree: {
22+
getClosestInstanceFromNode,
23+
},
24+
// Perf experiment
25+
addUserTimingListener,
26+
});
27+
28+
export {
29+
createPortal,
30+
unstable_batchedUpdates,
31+
flushSync,
32+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
33+
version,
34+
findDOMNode,
35+
hydrate,
36+
render,
37+
unmountComponentAtNode,
38+
createRoot,
39+
createBlockingRoot,
40+
unstable_discreteUpdates,
41+
unstable_flushDiscreteUpdates,
42+
unstable_flushControlled,
43+
unstable_scheduleHydration,
44+
unstable_renderSubtreeIntoContainer,
45+
unstable_createPortal,
46+
} from './src/client/ReactDOM';
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
export {
11+
createPortal,
12+
unstable_batchedUpdates,
13+
flushSync,
14+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
15+
version,
16+
// Disabled behind disableLegacyReactDOMAPIs
17+
findDOMNode,
18+
hydrate,
19+
render,
20+
unmountComponentAtNode,
21+
// exposeConcurrentModeAPIs
22+
createRoot,
23+
createBlockingRoot,
24+
unstable_discreteUpdates,
25+
unstable_flushDiscreteUpdates,
26+
unstable_flushControlled,
27+
unstable_scheduleHydration,
28+
// Disabled behind disableUnstableRenderSubtreeIntoContainer
29+
unstable_renderSubtreeIntoContainer,
30+
// Disabled behind disableUnstableCreatePortal
31+
// Temporary alias since we already shipped React 16 RC with it.
32+
// TODO: remove in React 17.
33+
unstable_createPortal,
34+
} from './src/client/ReactDOM';

packages/react-dom/index.fb.js

-14
This file was deleted.

packages/react-dom/index.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,4 @@
77
* @flow
88
*/
99

10-
'use strict';
11-
12-
const ReactDOM = require('./src/client/ReactDOM');
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 = ReactDOM.default || ReactDOM;
10+
export * from './src/client/ReactDOM';

packages/react-dom/index.modern.fb.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
export {
11+
createPortal,
12+
unstable_batchedUpdates,
13+
flushSync,
14+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
15+
version,
16+
createRoot,
17+
createBlockingRoot,
18+
unstable_discreteUpdates,
19+
unstable_flushDiscreteUpdates,
20+
unstable_flushControlled,
21+
unstable_scheduleHydration,
22+
} from './src/client/ReactDOM';

packages/react-dom/index.stable.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
export {
11+
createPortal,
12+
unstable_batchedUpdates,
13+
flushSync,
14+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
15+
version,
16+
findDOMNode,
17+
hydrate,
18+
render,
19+
unmountComponentAtNode,
20+
unstable_renderSubtreeIntoContainer,
21+
// Temporary alias since we already shipped React 16 RC with it.
22+
// TODO: remove in React 17.
23+
unstable_createPortal,
24+
} from './src/client/ReactDOM';

packages/react-dom/src/__tests__/ReactDOMFiber-test.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ const React = require('react');
1313
const ReactDOM = require('react-dom');
1414
const PropTypes = require('prop-types');
1515

16-
const ReactFeatureFlags = require('shared/ReactFeatureFlags');
17-
1816
describe('ReactDOMFiber', () => {
1917
let container;
2018

@@ -249,7 +247,7 @@ describe('ReactDOMFiber', () => {
249247
});
250248

251249
// TODO: remove in React 17
252-
if (!ReactFeatureFlags.disableUnstableCreatePortal) {
250+
if (!__EXPERIMENTAL__) {
253251
it('should support unstable_createPortal alias', () => {
254252
const portalContainer = document.createElement('div');
255253

0 commit comments

Comments
 (0)