Skip to content

Commit

Permalink
Replace shims with explicit React access from the renderers for share…
Browse files Browse the repository at this point in the history
…d global state (#9366)

* Remove non-existent /lib/ from souce files

* Replace all shims with explicit access

This deletes shims and changes to access require('react').__DO_NOT_USE__ from renderers for global shared state.

I cloned flattenChildren() and traverseAllChildren() because they relied on CurrentOwner but were used both from Stack and Isomorphic. The stack implementations will die, and the isomorphic ones can be changed to be optimized for Children specifically in the future.

I also deleted UMD shims because they are now unnecessary. I moved the internals assignment to main modules since they're now used in tests, and made them direct UMD entry points.
  • Loading branch information
gaearon authored Apr 7, 2017
1 parent 6eaa539 commit 72196da
Show file tree
Hide file tree
Showing 48 changed files with 509 additions and 398 deletions.
19 changes: 5 additions & 14 deletions scripts/rollup/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const bundles = [
moduleName: 'React',
sourceMap: false,
},
entry: 'src/umd/ReactUMDEntry.js',
entry: 'src/isomorphic/React.js',
externals: [],
fbEntry: 'src/fb/ReactFBEntry.js',
hasteName: 'React',
Expand All @@ -53,9 +53,6 @@ const bundles = [
manglePropertiesOnProd: false,
name: 'react',
paths: [
'src/umd/ReactUMDEntry.js',
'src/umd/shims/**/*.js',

'src/isomorphic/**/*.js',
'src/addons/**/*.js',

Expand All @@ -76,7 +73,7 @@ const bundles = [
moduleName: 'ReactDOM',
sourceMap: false,
},
entry: 'src/umd/ReactDOMUMDEntry.js',
entry: 'src/renderers/dom/ReactDOM.js',
externals: [],
fbEntry: 'src/fb/ReactDOMFBEntry.js',
hasteName: 'ReactDOMStack',
Expand All @@ -85,8 +82,6 @@ const bundles = [
manglePropertiesOnProd: false,
name: 'react-dom-stack',
paths: [
'src/umd/ReactDOMUMDEntry.js',

'src/renderers/dom/**/*.js',
'src/renderers/shared/**/*.js',
'src/test/**/*.js', // ReactTestUtils is currently very coupled to DOM.
Expand All @@ -106,7 +101,7 @@ const bundles = [
moduleName: 'ReactDOM',
sourceMap: false,
},
entry: 'src/umd/ReactDOMUMDEntry.js',
entry: 'src/renderers/dom/fiber/ReactDOMFiber.js',
externals: [],
fbEntry: 'src/fb/ReactDOMFiberFBEntry.js',
hasteName: 'ReactDOMFiber',
Expand All @@ -115,8 +110,6 @@ const bundles = [
manglePropertiesOnProd: false,
name: 'react-dom',
paths: [
'src/umd/ReactDOMUMDEntry.js',

'src/renderers/dom/**/*.js',
'src/renderers/shared/**/*.js',
'src/test/**/*.js', // ReactTestUtils is currently very coupled to DOM.
Expand All @@ -139,17 +132,15 @@ const bundles = [
moduleName: 'ReactDOMServer',
sourceMap: false,
},
entry: 'src/umd/ReactDOMServerUMDEntry.js',
entry: 'src/renderers/dom/ReactDOMServer.js',
externals: [],
fbEntry: 'src/umd/ReactDOMServerUMDEntry.js',
fbEntry: 'src/renderers/dom/ReactDOMServer.js',
hasteName: 'ReactDOMServerStack',
isRenderer: true,
label: 'dom-server',
manglePropertiesOnProd: false,
name: 'react-dom/server',
paths: [
'src/umd/ReactDOMServerUMDEntry.js',

'src/renderers/dom/**/*.js',
'src/renderers/shared/**/*.js',

Expand Down
101 changes: 8 additions & 93 deletions scripts/rollup/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ const fbjsModules = [
const devOnlyFilesToStubOut = [
"'ReactDebugCurrentFrame'",
"'ReactComponentTreeHook'",
"'react/lib/ReactDebugCurrentFrame'",
"'react/lib/ReactComponentTreeHook'",
"'react-dom/lib/ReactPerf'",
"'react-dom/lib/ReactTestUtils'",
"'ReactPerf'",
"'ReactTestUtils'",
];

// this function builds up a very niave Haste-like moduleMap
Expand Down Expand Up @@ -114,7 +112,6 @@ function ignoreFBModules() {
'ReactDOMFeatureFlags',
// In FB bundles, we preserve an inline require to ReactCurrentOwner.
// See the explanation in FB version of ReactCurrentOwner in www:
'react/lib/ReactCurrentOwner',
'ReactCurrentOwner',
];
}
Expand Down Expand Up @@ -157,7 +154,7 @@ function getExternalModules(externals, bundleType, isRenderer) {
case FB_DEV:
case FB_PROD:
fbjsModules.forEach(module => externalModules.push(module));
externalModules.push('react/lib/ReactCurrentOwner', 'ReactCurrentOwner');
externalModules.push('ReactCurrentOwner');
if (isRenderer) {
externalModules.push('React');
}
Expand All @@ -177,9 +174,6 @@ function getInternalModules() {
function replaceInternalModules() {
// we inline these modules in the bundles rather than leave them as external
return {
"'react-dom/lib/ReactPerf'": `'${resolve('./src/renderers/shared/ReactPerf.js')}'`,
"'react-dom/lib/ReactTestUtils'": `'${resolve('./src/test/ReactTestUtils.js')}'`,
"'react-dom/lib/ReactInstanceMap'": `'${resolve('./src/renderers/shared/shared/ReactInstanceMap.js')}'`,
"'react-dom'": `'${resolve('./src/renderers/dom/ReactDOM.js')}'`,
};
}
Expand Down Expand Up @@ -209,92 +203,16 @@ function getFbjsModuleAliases(bundleType) {

function replaceFbjsModuleAliases(bundleType) {
switch (bundleType) {
case UMD_DEV:
case UMD_PROD:
case NODE_DEV:
case NODE_PROD:
case RN_DEV:
case RN_PROD:
return {};
case FB_DEV:
case FB_PROD:
// additionally we add mappings for "react"
// so they work correctly on FB, this will change soon
// Haste at FB doesn't currently allow case sensitive names,
// and product code already uses "React". In the future,
// we will either allow both variants or migrate to lowercase.
return {
"'react'": "'React'",
};
}
}

// for renderers, we want them to require the __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner
// on the React bundle itself rather than require module directly.
// For the React bundle, ReactCurrentOwner should be bundled as part of the bundle
// itself and exposed on __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
const shimReactCurrentOwner = resolve(
'./scripts/rollup/shims/rollup/ReactCurrentOwnerRollupShim.js'
);
const realReactCurrentOwner = resolve(
'./src/isomorphic/classic/element/ReactCurrentOwner.js'
);

function getReactCurrentOwnerModuleAlias(bundleType, isRenderer) {
if (bundleType === FB_DEV || bundleType === FB_DEV) {
return {};
}
if (isRenderer) {
return {
ReactCurrentOwner: shimReactCurrentOwner,
'react/lib/ReactCurrentOwner': shimReactCurrentOwner,
};
} else {
return {
ReactCurrentOwner: realReactCurrentOwner,
'react/lib/ReactCurrentOwner': realReactCurrentOwner,
};
}
}

// this works almost identically to the ReactCurrentOwner shim above
const shimReactComponentTreeHook = resolve(
'./scripts/rollup/shims/rollup/ReactComponentTreeHookRollupShim.js'
);
const realReactComponentTreeHook = resolve(
'./src/isomorphic/hooks/ReactComponentTreeHook.js'
);

function getReactComponentTreeHookModuleAlias(bundleType, isRenderer) {
if (isRenderer) {
return {
ReactComponentTreeHook: shimReactComponentTreeHook,
'react/lib/ReactComponentTreeHook': shimReactComponentTreeHook,
};
} else {
return {
ReactComponentTreeHook: realReactComponentTreeHook,
'react/lib/ReactComponentTreeHook': realReactComponentTreeHook,
};
}
}

// this works almost identically to the ReactCurrentOwner shim above
const shimReactDebugCurrentFrame = resolve(
'./scripts/rollup/shims/rollup/ReactDebugCurrentFrameRollupShim.js'
);
const realReactDebugCurrentFrame = resolve(
'./src/isomorphic/classic/element/ReactDebugCurrentFrame.js'
);

function getReactDebugCurrentFrameModuleAlias(bundleType, isRenderer) {
if (isRenderer) {
return {
ReactDebugCurrentFrame: shimReactDebugCurrentFrame,
'react/lib/ReactDebugCurrentFrame': shimReactDebugCurrentFrame,
};
} else {
return {
ReactDebugCurrentFrame: realReactDebugCurrentFrame,
'react/lib/ReactDebugCurrentFrame': realReactDebugCurrentFrame,
};
default:
return {};
}
}

Expand All @@ -321,9 +239,6 @@ function replaceDevOnlyStubbedModules(bundleType) {

function getAliases(paths, bundleType, isRenderer, extractErrors) {
return Object.assign(
getReactCurrentOwnerModuleAlias(bundleType, isRenderer),
getReactComponentTreeHookModuleAlias(bundleType, isRenderer),
getReactDebugCurrentFrameModuleAlias(bundleType, isRenderer),
createModuleMap(
paths,
extractErrors && extractErrorCodes(errorCodeOpts),
Expand Down
Loading

0 comments on commit 72196da

Please sign in to comment.