Skip to content

Commit 9e4485e

Browse files
committed
Use a shim file to load addons' dependencies on DOM
By replacing this intermediate file we can do the lazy loading without needing any lazy requires. This set up works with ES modules. We could also replace the globalShim thing with aliased files instead for consistency.
1 parent c6f32c5 commit 9e4485e

9 files changed

+94
-57
lines changed

grunt/config/browserify.js

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ var grunt = require('grunt');
77
var UglifyJS = require('uglify-js');
88
var uglifyify = require('uglifyify');
99
var derequire = require('derequire');
10+
var aliasify = require('aliasify');
1011
var globalShim = require('browserify-global-shim');
1112
var collapser = require('bundle-collapser/plugin');
1213

1314
var envifyDev = envify({NODE_ENV: process.env.NODE_ENV || 'development'});
1415
var envifyProd = envify({NODE_ENV: process.env.NODE_ENV || 'production'});
1516

1617
var SECRET_INTERNALS_NAME = 'React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED';
17-
var SECRET_DOM_INTERNALS_NAME = 'ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED';
1818

1919
var shimSharedModules = globalShim.configure({
2020
'./ReactCurrentOwner': SECRET_INTERNALS_NAME + '.ReactCurrentOwner',
@@ -25,25 +25,12 @@ var shimSharedModules = globalShim.configure({
2525
'./ReactChildren': 'React.Children',
2626
});
2727

28-
// Shim references to ReactDOM internals from addons.
29-
var shimDOM = globalShim.configure({
30-
'./ReactDOM': 'ReactDOM',
31-
'./ReactInstanceMap': SECRET_DOM_INTERNALS_NAME + '.ReactInstanceMap',
32-
33-
// TestUtils pulls in a bunch of internals.
34-
'./EventConstants': SECRET_DOM_INTERNALS_NAME + '.EventConstants',
35-
'./EventPluginHub': SECRET_DOM_INTERNALS_NAME + '.EventPluginHub',
36-
'./EventPluginRegistry': SECRET_DOM_INTERNALS_NAME + '.EventPluginRegistry',
37-
'./EventPropagators': SECRET_DOM_INTERNALS_NAME + '.EventPropagators',
38-
'./ReactDefaultInjection': SECRET_DOM_INTERNALS_NAME + '.ReactDefaultInjection',
39-
'./ReactDOMComponentTree': SECRET_DOM_INTERNALS_NAME + '.ReactDOMComponentTree',
40-
'./ReactBrowserEventEmitter': SECRET_DOM_INTERNALS_NAME + '.ReactBrowserEventEmitter',
41-
'./ReactCompositeComponent': SECRET_DOM_INTERNALS_NAME + '.ReactCompositeComponent',
42-
'./ReactInstrumentation': SECRET_DOM_INTERNALS_NAME + '.ReactInstrumentation',
43-
'./ReactReconciler': SECRET_DOM_INTERNALS_NAME + '.ReactReconciler',
44-
'./ReactUpdates': SECRET_DOM_INTERNALS_NAME + '.ReactUpdates',
45-
'./SyntheticEvent': SECRET_DOM_INTERNALS_NAME + '.SyntheticEvent',
46-
'./findDOMNode': SECRET_DOM_INTERNALS_NAME + '.findDOMNode',
28+
var shimDOMModules = aliasify.configure({
29+
30+
'aliases': {
31+
'./ReactAddonsDOMDependencies': './build/modules/ReactAddonsDOMDependenciesUMDShim.js',
32+
}
33+
4734
});
4835

4936
var SIMPLE_TEMPLATE =
@@ -121,7 +108,7 @@ var addons = {
121108
debug: false,
122109
standalone: 'React',
123110
packageName: 'React (with addons)',
124-
transforms: [shimDOM],
111+
transforms: [shimDOMModules],
125112
globalTransforms: [envifyDev],
126113
plugins: [collapser],
127114
after: [derequire, simpleBannerify],
@@ -135,7 +122,7 @@ var addonsMin = {
135122
debug: false,
136123
standalone: 'React',
137124
packageName: 'React (with addons)',
138-
transforms: [shimDOM, envifyProd, uglifyify],
125+
transforms: [shimDOMModules, envifyProd, uglifyify],
139126
globalTransforms: [envifyProd],
140127
plugins: [collapser],
141128
// No need to derequire because the minifier will mangle

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"private": true,
44
"version": "16.0.0-alpha",
55
"devDependencies": {
6+
"aliasify": "^2.0.0",
67
"art": "^0.10.1",
78
"async": "^1.5.0",
89
"babel-cli": "^6.6.5",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule ReactAddonsDOMDependencies
10+
*/
11+
12+
'use strict';
13+
14+
var ReactDOM = require('ReactDOM');
15+
var ReactInstanceMap = require('ReactInstanceMap');
16+
17+
exports.getReactDOM = function() {
18+
return ReactDOM;
19+
};
20+
21+
exports.getReactInstanceMap = function() {
22+
return ReactInstanceMap;
23+
};
24+
25+
if (__DEV__) {
26+
var ReactPerf = require('ReactPerf');
27+
var ReactTestUtils = require('ReactTestUtils');
28+
29+
exports.getReactPerf = function() {
30+
return ReactPerf;
31+
};
32+
33+
exports.getReactTestUtils = function() {
34+
return ReactTestUtils;
35+
};
36+
}

src/addons/ReactWithAddons.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
var LinkedStateMixin = require('LinkedStateMixin');
1515
var React = require('React');
16+
var ReactAddonsDOMDependencies = require('ReactAddonsDOMDependencies');
1617
var ReactComponentWithPureRenderMixin =
1718
require('ReactComponentWithPureRenderMixin');
1819
var ReactCSSTransitionGroup = require('ReactCSSTransitionGroup');
@@ -34,18 +35,18 @@ React.addons = {
3435
};
3536

3637
if (__DEV__) {
37-
// We need to lazily require these modules for the browser build since they
38-
// will depend on DOM internals which hasn't loaded yet.
38+
// For the UMD build we get these lazily from the global since they're tied
39+
// to the DOM renderer and it hasn't loaded yet.
3940
Object.defineProperty(React.addons, 'Perf', {
4041
enumerable: true,
4142
get: function() {
42-
return require('ReactPerf');
43+
return ReactAddonsDOMDependencies.getReactPerf();
4344
},
4445
});
4546
Object.defineProperty(React.addons, 'TestUtils', {
4647
enumerable: true,
4748
get: function() {
48-
return require('ReactTestUtils');
49+
return ReactAddonsDOMDependencies.getReactTestUtils();
4950
},
5051
});
5152
}

src/addons/transitions/ReactCSSTransitionGroupChild.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
'use strict';
1313

1414
var React = require('React');
15-
// For the browser build we need to lazily load this since the DOM package
16-
// instantiates after the addons package.
17-
var ReactDOM = null;
15+
var ReactAddonsDOMDependencies = require('ReactAddonsDOMDependencies');
1816

1917
var CSSCore = require('CSSCore');
2018
var ReactTransitionEvents = require('ReactTransitionEvents');
@@ -56,7 +54,7 @@ var ReactCSSTransitionGroupChild = React.createClass({
5654
},
5755

5856
transition: function(animationType, finishCallback, userSpecifiedDelay) {
59-
var node = ReactDOM.findDOMNode(this);
57+
var node = ReactAddonsDOMDependencies.getReactDOM().findDOMNode(this);
6058

6159
if (!node) {
6260
if (finishCallback) {
@@ -126,9 +124,6 @@ var ReactCSSTransitionGroupChild = React.createClass({
126124
},
127125

128126
componentWillMount: function() {
129-
if (!ReactDOM) {
130-
ReactDOM = require('ReactDOM');
131-
}
132127
this.classNameAndNodeQueue = [];
133128
this.transitionTimeouts = [];
134129
},

src/addons/transitions/ReactTransitionGroup.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
'use strict';
1313

1414
var React = require('React');
15-
// We need to lazily require this for the browser build because the DOM
16-
// renderer gets initialized after the main React bundle.
17-
var ReactInstanceMap = null;
15+
var ReactAddonsDOMDependencies = require('ReactAddonsDOMDependencies');
1816
var ReactTransitionChildMapping = require('ReactTransitionChildMapping');
1917

2018
var emptyFunction = require('emptyFunction');
@@ -47,9 +45,6 @@ var ReactTransitionGroup = React.createClass({
4745
},
4846

4947
componentWillMount: function() {
50-
if (!ReactInstanceMap) {
51-
ReactInstanceMap = require('ReactInstanceMap');
52-
}
5348
this.currentlyTransitioningKeys = {};
5449
this.keysToEnter = [];
5550
this.keysToLeave = [];
@@ -69,7 +64,7 @@ var ReactTransitionGroup = React.createClass({
6964
if (__DEV__) {
7065
nextChildMapping = ReactTransitionChildMapping.getChildMapping(
7166
nextProps.children,
72-
ReactInstanceMap.get(this)._debugID
67+
ReactAddonsDOMDependencies.getReactInstanceMap().get(this)._debugID
7368
);
7469
} else {
7570
nextChildMapping = ReactTransitionChildMapping.getChildMapping(
@@ -142,7 +137,7 @@ var ReactTransitionGroup = React.createClass({
142137
if (__DEV__) {
143138
currentChildMapping = ReactTransitionChildMapping.getChildMapping(
144139
this.props.children,
145-
ReactInstanceMap.get(this)._debugID
140+
ReactAddonsDOMDependencies.getReactInstanceMap().get(this)._debugID
146141
);
147142
} else {
148143
currentChildMapping = ReactTransitionChildMapping.getChildMapping(
@@ -182,7 +177,7 @@ var ReactTransitionGroup = React.createClass({
182177
if (__DEV__) {
183178
currentChildMapping = ReactTransitionChildMapping.getChildMapping(
184179
this.props.children,
185-
ReactInstanceMap.get(this)._debugID
180+
ReactAddonsDOMDependencies.getReactInstanceMap().get(this)._debugID
186181
);
187182
} else {
188183
currentChildMapping = ReactTransitionChildMapping.getChildMapping(
@@ -223,7 +218,7 @@ var ReactTransitionGroup = React.createClass({
223218
if (__DEV__) {
224219
currentChildMapping = ReactTransitionChildMapping.getChildMapping(
225220
this.props.children,
226-
ReactInstanceMap.get(this)._debugID
221+
ReactAddonsDOMDependencies.getReactInstanceMap().get(this)._debugID
227222
);
228223
} else {
229224
currentChildMapping = ReactTransitionChildMapping.getChildMapping(

src/umd/ReactDOMUMDEntry.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,13 @@ var ReactDOMUMDEntry = Object.assign({
2020
}, ReactDOM);
2121

2222
if (__DEV__) {
23-
// These are used by ReactTestUtils in ReactWithAddons. Ugh.
2423
Object.assign(
2524
ReactDOMUMDEntry.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
2625
{
27-
EventConstants: require('EventConstants'),
28-
EventPluginHub: require('EventPluginHub'),
29-
EventPluginRegistry: require('EventPluginRegistry'),
30-
EventPropagators: require('EventPropagators'),
31-
ReactDefaultInjection: require('ReactDefaultInjection'),
32-
ReactDOMComponentTree: require('ReactDOMComponentTree'),
33-
ReactBrowserEventEmitter: require('ReactBrowserEventEmitter'),
34-
ReactCompositeComponent: require('ReactCompositeComponent'),
35-
ReactInstrumentation: require('ReactInstrumentation'),
36-
ReactReconciler: require('ReactReconciler'),
37-
ReactUpdates: require('ReactUpdates'),
38-
SyntheticEvent: require('SyntheticEvent'),
39-
findDOMNode: require('findDOMNode'),
26+
// ReactPerf and ReactTestUtils currently only work with the DOM renderer
27+
// so we expose them from here, but only in DEV mode.
28+
ReactPerf: require('ReactPerf'),
29+
ReactTestUtils: require('ReactTestUtils'),
4030
}
4131
);
4232
}

src/umd/ReactWithAddonsUMDEntry.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
'use strict';
1313

1414
var ReactWithAddons = require('ReactWithAddons');
15+
var ReactCurrentOwner = require('ReactCurrentOwner');
16+
var ReactComponentTreeHook = require('ReactComponentTreeHook');
1517

1618
// `version` will be added here by the React module.
1719
var ReactWithAddonsUMDEntry = Object.assign({
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule ReactAddonsDOMDependenciesUMDShim
10+
*/
11+
12+
'use strict';
13+
14+
exports.getReactDOM = function() {
15+
return ReactDOM;
16+
};
17+
18+
exports.getReactInstanceMap = function() {
19+
return ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactInstanceMap;
20+
};
21+
22+
if (__DEV__) {
23+
exports.getReactPerf = function() {
24+
return ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactPerf;
25+
};
26+
27+
exports.getReactTestUtils = function() {
28+
return ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactTestUtils;
29+
};
30+
}

0 commit comments

Comments
 (0)