diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index b180c87f1b..14baa79fa6 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -51,12 +51,14 @@ function getBanner(bundleType, hastName) { return ''; } -function updaateBabelConfig(babelOpts, bundleType) { +function updateBabelConfig(babelOpts, bundleType) { + let newOpts; + switch (bundleType) { case bundleTypes.PROD: case bundleTypes.DEV: case bundleTypes.NODE: - const newOpts = Object.assign({}, babelOpts); + newOpts = Object.assign({}, babelOpts); // we add the objectAssign transform for these bundles newOpts.plugins = newOpts.plugins.slice(); @@ -65,7 +67,11 @@ function updaateBabelConfig(babelOpts, bundleType) { ); return newOpts; case bundleTypes.FB: - return babelOpts; + newOpts = Object.assign({}, babelOpts); + + // for FB, we don't want the devExpressionWithCodes plugin to run + newOpts.plugins = []; + return newOpts; } } @@ -125,6 +131,24 @@ function uglifyConfig() { }; } +function getCommonJsConfig(bundleType) { + switch (bundleType) { + case bundleTypes.PROD: + case bundleTypes.DEV: + return {}; + case bundleTypes.NODE: + case bundleTypes.FB: + // we ignore the require() for some inline modules + // wrapped in __DEV__ + return { + ignore: [ + 'ReactPerf', + 'ReactTestUtils', + ], + }; + } +} + function getPlugins(entry, babelOpts, paths, filename, bundleType) { const plugins = [ replace( @@ -133,9 +157,9 @@ function getPlugins(entry, babelOpts, paths, filename, bundleType) { replaceFbjsModuleAliases(bundleType) ) ), - babel(updaateBabelConfig(babelOpts, bundleType)), + babel(updateBabelConfig(babelOpts, bundleType)), alias(getAliases(paths, bundleType)), - commonjs(), + commonjs(getCommonJsConfig(bundleType)), ]; if (bundleType === bundleTypes.PROD) { plugins.push( diff --git a/scripts/rollup/forwarding/ReactPerf.js b/scripts/rollup/forwarding/ReactPerf.js new file mode 100644 index 0000000000..02715f05bd --- /dev/null +++ b/scripts/rollup/forwarding/ReactPerf.js @@ -0,0 +1,19 @@ +/** + * Copyright 2013-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactPerf + * @flow + */ + +'use strict'; + +const { + __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, +} = require('ReactDOM'); + +module.exports = __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactPerf; diff --git a/scripts/rollup/forwarding/ReactTestUtils.js b/scripts/rollup/forwarding/ReactTestUtils.js new file mode 100644 index 0000000000..014909bd29 --- /dev/null +++ b/scripts/rollup/forwarding/ReactTestUtils.js @@ -0,0 +1,19 @@ +/** + * Copyright 2013-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactTestUtils + * @flow + */ + +'use strict'; + +const { + __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, +} = require('ReactDOM'); + +module.exports = __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactTestUtils; diff --git a/scripts/rollup/forwarding/flattenChildren.js b/scripts/rollup/forwarding/flattenChildren.js new file mode 100644 index 0000000000..a728fd79d1 --- /dev/null +++ b/scripts/rollup/forwarding/flattenChildren.js @@ -0,0 +1,19 @@ +/** + * Copyright 2013-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule flattenChildren + * @flow + */ + +'use strict'; + +const { + __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, +} = require('React'); + +module.exports = __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.flattenChildren; diff --git a/scripts/rollup/forwarding/onlyChild.js b/scripts/rollup/forwarding/onlyChild.js new file mode 100644 index 0000000000..4b781ada43 --- /dev/null +++ b/scripts/rollup/forwarding/onlyChild.js @@ -0,0 +1,39 @@ +/** + * Copyright 2013-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule onlyChild + */ +'use strict'; + +var { isValidElement } = require('React'); + +var invariant = require('invariant'); + +/** + * Returns the first child in a collection of children and verifies that there + * is only one child in the collection. + * + * See https://facebook.github.io/react/docs/react-api.html#react.children.only + * + * The current implementation of this function assumes that a single child gets + * passed without a wrapper, but the purpose of this helper function is to + * abstract away the particular structure of children. + * + * @param {?object} children Child collection structure. + * @return {ReactElement} The first and only `ReactElement` contained in the + * structure. + */ +function onlyChild(children) { + invariant( + isValidElement(children), + 'React.Children.only expected to receive a single React element child.' + ); + return children; +} + +module.exports = onlyChild; diff --git a/scripts/rollup/modules.js b/scripts/rollup/modules.js index 459347be55..5ea20b389d 100644 --- a/scripts/rollup/modules.js +++ b/scripts/rollup/modules.js @@ -113,6 +113,7 @@ function replaceFbjsModuleAliases(bundleType) { 'fbjs/lib/ExecutionEnvironment': 'ExecutionEnvironment', 'fbjs/lib/createNodesFromMarkup': 'createNodesFromMarkup', 'fbjs/lib/performanceNow': 'performanceNow', + "'react'": "'React'", }; } } diff --git a/src/fb/ReactDOMFiberFBEntry.js b/src/fb/ReactDOMFiberFBEntry.js index c11fa1128a..ef12cd6649 100644 --- a/src/fb/ReactDOMFiberFBEntry.js +++ b/src/fb/ReactDOMFiberFBEntry.js @@ -22,4 +22,16 @@ ReactDOMFiber.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = { SyntheticEvent: require('SyntheticEvent'), }; +if (__DEV__) { + Object.assign( + ReactDOMFiber.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, + { + // ReactPerf and ReactTestUtils currently only work with the DOM renderer + // so we expose them from here, but only in DEV mode. + ReactPerf: require('react-dom/lib/ReactPerf'), + ReactTestUtils: require('react-dom/lib/ReactTestUtils'), + } + ); +} + module.exports = ReactDOMFiber; diff --git a/src/fb/ReactFBEntry.js b/src/fb/ReactFBEntry.js index c7eb080795..80fcffe8b6 100644 --- a/src/fb/ReactFBEntry.js +++ b/src/fb/ReactFBEntry.js @@ -19,6 +19,7 @@ var ReactFBEntry = Object.assign({ ReactCurrentOwner: require('react/lib/ReactCurrentOwner'), getComponentName: require('getComponentName'), ReactInstanceMap: require('react-dom/lib/ReactInstanceMap'), + flattenChildren: require('flattenChildren'), }, }, React);