Files
react/scripts/rollup/wrappers.js
Dan Abramov 47b003a828 Resolve host configs at build time (#12792)
* Extract base Jest config

This makes it easier to change the source config without affecting the build test config.

* Statically import the host config

This changes react-reconciler to import HostConfig instead of getting it through a function argument.

Rather than start with packages like ReactDOM that want to inline it, I started with React Noop and ensured that *custom* renderers using react-reconciler package still work. To do this, I'm making HostConfig module in the reconciler look at a global variable by default (which, in case of the react-reconciler npm package, ends up being the host config argument in the top-level scope).

This is still very broken.

* Add scaffolding for importing an inlined renderer

* Fix the build

* ES exports for renderer methods

* ES modules for host configs

* Remove closures from the reconciler

* Check each renderer's config with Flow

* Fix uncovered Flow issue

We know nextHydratableInstance doesn't get mutated inside this function, but Flow doesn't so it thinks it may be null.
Help Flow.

* Prettier

* Get rid of enable*Reconciler flags

They are not as useful anymore because for almost all cases (except third party renderers) we *know* whether it supports mutation or persistence.

This refactoring means react-reconciler and react-reconciler/persistent third-party packages now ship the same thing.
Not ideal, but this seems worth how simpler the code becomes. We can later look into addressing it by having a single toggle instead.

* Prettier again

* Fix Flow config creation issue

* Fix imprecise Flow typing

* Revert accidental changes
2018-05-19 11:29:11 +01:00

253 lines
5.6 KiB
JavaScript

'use strict';
const Bundles = require('./bundles');
const reactVersion = require('../../package.json').version;
const UMD_DEV = Bundles.bundleTypes.UMD_DEV;
const UMD_PROD = Bundles.bundleTypes.UMD_PROD;
const NODE_DEV = Bundles.bundleTypes.NODE_DEV;
const NODE_PROD = Bundles.bundleTypes.NODE_PROD;
const FB_WWW_DEV = Bundles.bundleTypes.FB_WWW_DEV;
const FB_WWW_PROD = Bundles.bundleTypes.FB_WWW_PROD;
const RN_OSS_DEV = Bundles.bundleTypes.RN_OSS_DEV;
const RN_OSS_PROD = Bundles.bundleTypes.RN_OSS_PROD;
const RN_FB_DEV = Bundles.bundleTypes.RN_FB_DEV;
const RN_FB_PROD = Bundles.bundleTypes.RN_FB_PROD;
const RECONCILER = Bundles.moduleTypes.RECONCILER;
const license = ` * Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.`;
const wrappers = {
/***************** UMD_DEV *****************/
[UMD_DEV](source, globalName, filename, moduleType) {
return `/** @license React v${reactVersion}
* ${filename}
*
${license}
*/
'use strict';
${source}`;
},
/***************** UMD_PROD *****************/
[UMD_PROD](source, globalName, filename, moduleType) {
return `/** @license React v${reactVersion}
* ${filename}
*
${license}
*/
${source}`;
},
/***************** NODE_DEV *****************/
[NODE_DEV](source, globalName, filename, moduleType) {
return `/** @license React v${reactVersion}
* ${filename}
*
${license}
*/
'use strict';
${
globalName === 'ReactNoopRenderer' ||
globalName === 'ReactNoopRendererPersistent'
? // React Noop needs regenerator runtime because it uses
// generators but GCC doesn't handle them in the output.
// So we use Babel for them.
`const regeneratorRuntime = require("regenerator-runtime");`
: ``
}
if (process.env.NODE_ENV !== "production") {
(function() {
${source}
})();
}`;
},
/***************** NODE_PROD *****************/
[NODE_PROD](source, globalName, filename, moduleType) {
return `/** @license React v${reactVersion}
* ${filename}
*
${license}
*/
${
globalName === 'ReactNoopRenderer' ||
globalName === 'ReactNoopRendererPersistent'
? // React Noop needs regenerator runtime because it uses
// generators but GCC doesn't handle them in the output.
// So we use Babel for them.
`const regeneratorRuntime = require("regenerator-runtime");`
: ``
}
${source}`;
},
/****************** FB_WWW_DEV ******************/
[FB_WWW_DEV](source, globalName, filename, moduleType) {
return `/**
${license}
*
* @noflow
* @preventMunge
* @preserve-invariant-messages
*/
'use strict';
if (__DEV__) {
(function() {
${source}
})();
}`;
},
/****************** FB_WWW_PROD ******************/
[FB_WWW_PROD](source, globalName, filename, moduleType) {
return `/**
${license}
*
* @noflow
* @preventMunge
* @preserve-invariant-messages
*/
${source}`;
},
/****************** RN_OSS_DEV ******************/
[RN_OSS_DEV](source, globalName, filename, moduleType) {
return `/**
${license}
*
* @noflow
* @providesModule ${globalName}-dev
* @preventMunge
* ${'@gen' + 'erated'}
*/
'use strict';
if (__DEV__) {
(function() {
${source}
})();
}`;
},
/****************** RN_OSS_PROD ******************/
[RN_OSS_PROD](source, globalName, filename, moduleType) {
return `/**
${license}
*
* @noflow
* @providesModule ${globalName}-prod
* @preventMunge
* ${'@gen' + 'erated'}
*/
${source}`;
},
/****************** RN_FB_DEV ******************/
[RN_FB_DEV](source, globalName, filename, moduleType) {
return `/**
${license}
*
* @noflow
* @preventMunge
* ${'@gen' + 'erated'}
*/
'use strict';
if (__DEV__) {
(function() {
${source}
})();
}`;
},
/****************** RN_FB_PROD ******************/
[RN_FB_PROD](source, globalName, filename, moduleType) {
return `/**
${license}
*
* @noflow
* @preventMunge
* ${'@gen' + 'erated'}
*/
${source}`;
},
};
const reconcilerWrappers = {
/***************** NODE_DEV (reconciler only) *****************/
[NODE_DEV](source, globalName, filename, moduleType) {
return `/** @license React v${reactVersion}
* ${filename}
*
${license}
*/
'use strict';
if (process.env.NODE_ENV !== "production") {
module.exports = function $$$reconciler($$$hostConfig) {
${source}
var $$$renderer = module.exports;
module.exports = $$$reconciler;
return $$$renderer;
};
}`;
},
/***************** NODE_PROD (reconciler only) *****************/
[NODE_PROD](source, globalName, filename, moduleType) {
return `/** @license React v${reactVersion}
* ${filename}
*
${license}
*/
module.exports = function $$$reconciler($$$hostConfig) {
${source}
var $$$renderer = module.exports;
module.exports = $$$reconciler;
return $$$renderer;
};`;
},
};
function wrapBundle(source, bundleType, globalName, filename, moduleType) {
if (moduleType === RECONCILER) {
// Standalone reconciler is only used by third-party renderers.
// It is handled separately.
const wrapper = reconcilerWrappers[bundleType];
if (typeof wrapper !== 'function') {
throw new Error(
`Unsupported build type for the reconciler package: ${bundleType}.`
);
}
return wrapper(source, globalName, filename, moduleType);
}
// All the other packages.
const wrapper = wrappers[bundleType];
if (typeof wrapper !== 'function') {
throw new Error(`Unsupported build type: ${bundleType}.`);
}
return wrapper(source, globalName, filename, moduleType);
}
module.exports = {
wrapBundle,
};