2017-04-05 23:47:29 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-10-25 07:55:00 +08:00
|
|
|
const path = require('path');
|
2017-04-05 23:47:29 +08:00
|
|
|
const bundleTypes = require('./bundles').bundleTypes;
|
|
|
|
|
|
|
|
|
|
const UMD_DEV = bundleTypes.UMD_DEV;
|
|
|
|
|
const UMD_PROD = bundleTypes.UMD_PROD;
|
|
|
|
|
const FB_DEV = bundleTypes.FB_DEV;
|
|
|
|
|
const FB_PROD = bundleTypes.FB_PROD;
|
|
|
|
|
|
2017-10-25 07:55:00 +08:00
|
|
|
// Bundles exporting globals that other modules rely on.
|
|
|
|
|
const knownGlobals = Object.freeze({
|
|
|
|
|
react: 'React',
|
|
|
|
|
'react-dom': 'ReactDOM',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Redirect some modules to Haste forks in www.
|
|
|
|
|
// Assuming their names in www are the same, and also match
|
|
|
|
|
// imported names in corresponding ./shims/rollup/*-www.js shims.
|
|
|
|
|
const forkedFBModules = Object.freeze([
|
|
|
|
|
// At FB, we don't know them statically:
|
|
|
|
|
'shared/ReactFeatureFlags',
|
|
|
|
|
// This logic is also forked internally.
|
|
|
|
|
'shared/lowPriorityWarning',
|
|
|
|
|
// In FB bundles, we preserve an inline require to ReactCurrentOwner.
|
|
|
|
|
// See the explanation in FB version of ReactCurrentOwner in www:
|
|
|
|
|
'react/src/ReactCurrentOwner',
|
|
|
|
|
]);
|
|
|
|
|
|
2017-11-03 03:50:03 +08:00
|
|
|
// For any external that is used in a DEV-only condition, explicitly
|
|
|
|
|
// specify whether it has side effects during import or not. This lets
|
|
|
|
|
// us know whether we can safely omit them when they are unused.
|
|
|
|
|
const HAS_NO_SIDE_EFFECTS_ON_IMPORT = false;
|
|
|
|
|
// const HAS_SIDE_EFFECTS_ON_IMPORT = true;
|
|
|
|
|
const importSideEffects = Object.freeze({
|
|
|
|
|
'fbjs/lib/invariant': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
|
|
|
|
'fbjs/lib/warning': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
|
|
|
|
'prop-types/checkPropTypes': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
|
|
|
|
'fbjs/lib/camelizeStyleName': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
|
|
|
|
'fbjs/lib/hyphenateStyleName': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
|
|
|
|
deepFreezeAndThrowOnMutationInDev: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-25 07:55:00 +08:00
|
|
|
// Given ['react'] in bundle externals, returns { 'react': 'React' }.
|
|
|
|
|
function getPeerGlobals(externals, moduleType) {
|
|
|
|
|
const peerGlobals = {};
|
|
|
|
|
externals.forEach(name => {
|
|
|
|
|
if (
|
|
|
|
|
!knownGlobals[name] &&
|
|
|
|
|
(moduleType === UMD_DEV || moduleType === UMD_PROD)
|
|
|
|
|
) {
|
|
|
|
|
throw new Error('Cannot build UMD without a global name for: ' + name);
|
|
|
|
|
}
|
|
|
|
|
peerGlobals[name] = knownGlobals[name];
|
2017-04-05 23:47:29 +08:00
|
|
|
});
|
2017-10-25 07:55:00 +08:00
|
|
|
return peerGlobals;
|
2017-04-05 23:47:29 +08:00
|
|
|
}
|
|
|
|
|
|
2017-10-25 07:55:00 +08:00
|
|
|
// Determines node_modules packages that are safe to assume will exist.
|
|
|
|
|
function getDependencies(bundleType, entry) {
|
|
|
|
|
const packageJson = require(path.basename(
|
|
|
|
|
path.dirname(require.resolve(entry))
|
|
|
|
|
) + '/package.json');
|
|
|
|
|
// Both deps and peerDeps are assumed as accessible.
|
|
|
|
|
let deps = Array.from(
|
|
|
|
|
new Set([
|
|
|
|
|
...Object.keys(packageJson.dependencies || {}),
|
|
|
|
|
...Object.keys(packageJson.peerDependencies || {}),
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
// In www, forked modules are also require-able.
|
|
|
|
|
if (bundleType === FB_DEV || bundleType === FB_PROD) {
|
|
|
|
|
deps = [...deps, ...forkedFBModules.map(name => path.basename(name))];
|
2017-04-05 23:47:29 +08:00
|
|
|
}
|
2017-10-25 07:55:00 +08:00
|
|
|
return deps;
|
2017-04-05 23:47:29 +08:00
|
|
|
}
|
|
|
|
|
|
2017-11-03 03:50:03 +08:00
|
|
|
function getImportSideEffects() {
|
|
|
|
|
return importSideEffects;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-25 07:55:00 +08:00
|
|
|
// Hijacks some modules for optimization and integration reasons.
|
|
|
|
|
function getShims(bundleType, entry, featureFlags) {
|
|
|
|
|
const shims = {};
|
2017-04-05 23:47:29 +08:00
|
|
|
switch (bundleType) {
|
|
|
|
|
case UMD_DEV:
|
|
|
|
|
case UMD_PROD:
|
2017-10-25 07:55:00 +08:00
|
|
|
if (getDependencies(bundleType, entry).indexOf('react') !== -1) {
|
|
|
|
|
// Optimization: rely on object-assign polyfill that is already a part
|
|
|
|
|
// of the React package instead of bundling it again.
|
|
|
|
|
shims['object-assign'] = path.resolve(
|
|
|
|
|
__dirname + '/shims/rollup/assign-umd.js'
|
|
|
|
|
);
|
2017-04-05 23:47:29 +08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case FB_DEV:
|
|
|
|
|
case FB_PROD:
|
2017-10-25 07:55:00 +08:00
|
|
|
// FB forks a few modules in www that are usually bundled.
|
|
|
|
|
// Instead of bundling them, they need to be kept as require()s in the
|
|
|
|
|
// final bundles so that they import www modules with the same names.
|
|
|
|
|
// Rollup doesn't make it very easy to rewrite and ignore such a require,
|
|
|
|
|
// so we resort to using a shim that re-exports the www module, and then
|
|
|
|
|
// treating shim's target destinations as external (see getDependencies).
|
|
|
|
|
forkedFBModules.forEach(srcPath => {
|
2017-11-07 00:53:13 +08:00
|
|
|
const fileName = path.parse(srcPath).name;
|
2017-10-25 07:55:00 +08:00
|
|
|
const shimPath = path.resolve(
|
2017-11-07 00:53:13 +08:00
|
|
|
__dirname + `/shims/rollup/${fileName}-www.js`
|
2017-10-25 07:55:00 +08:00
|
|
|
);
|
2017-11-03 03:50:03 +08:00
|
|
|
shims[srcPath] = shimPath;
|
2017-11-07 00:53:13 +08:00
|
|
|
// <hack>
|
|
|
|
|
// Unfortunately the above doesn't work for relative imports,
|
|
|
|
|
// and Rollup isn't smart enough to understand they refer to the same file.
|
|
|
|
|
// We should come up with a real fix for this, but for now this will do.
|
|
|
|
|
// FIXME: this is gross.
|
|
|
|
|
shims['./' + fileName] = shimPath;
|
|
|
|
|
shims['../' + fileName] = shimPath;
|
|
|
|
|
// We don't have deeper relative requires between source files.
|
|
|
|
|
// </hack>
|
2017-10-25 07:55:00 +08:00
|
|
|
});
|
2017-04-05 23:47:29 +08:00
|
|
|
break;
|
|
|
|
|
}
|
2017-10-25 07:55:00 +08:00
|
|
|
if (featureFlags) {
|
2017-11-03 03:50:03 +08:00
|
|
|
shims['shared/ReactFeatureFlags'] = require.resolve(featureFlags);
|
Reorganize code structure (#11288)
* Move files and tests to more meaningful places
* Fix the build
Now that we import reconciler via react-reconciler, I needed to make a few tweaks.
* Update sizes
* Move @preventMunge directive to FB header
* Revert unintentional change
* Fix Flow coverage
I forgot to @flow-ify those files. This uncovered some issues.
* Prettier, I love you but you're bringing me down
Prettier, I love you but you're bringing me down
Like a rat in a cage
Pulling minimum wage
Prettier, I love you but you're bringing me down
Prettier, you're safer and you're wasting my time
Our records all show you were filthy but fine
But they shuttered your stores
When you opened the doors
To the cops who were bored once they'd run out of crime
Prettier, you're perfect, oh, please don't change a thing
Your mild billionaire mayor's now convinced he's a king
So the boring collect
I mean all disrespect
In the neighborhood bars I'd once dreamt I would drink
Prettier, I love you but you're freaking me out
There's a ton of the twist but we're fresh out of shout
Like a death in the hall
That you hear through your wall
Prettier, I love you but you're freaking me out
Prettier, I love you but you're bringing me down
Prettier, I love you but you're bringing me down
Like a death of the heart
Jesus, where do I start?
But you're still the one pool where I'd happily drown
And oh! Take me off your mailing list
For kids who think it still exists
Yes, for those who think it still exists
Maybe I'm wrong and maybe you're right
Maybe I'm wrong and maybe you're right
Maybe you're right, maybe I'm wrong
And just maybe you're right
And oh! Maybe mother told you true
And there'll always be somebody there for you
And you'll never be alone
But maybe she's wrong and maybe I'm right
And just maybe she's wrong
Maybe she's wrong and maybe I'm right
And if so, here's this song!
2017-10-20 02:50:24 +08:00
|
|
|
}
|
2017-10-25 07:55:00 +08:00
|
|
|
return shims;
|
2017-04-06 02:51:19 +08:00
|
|
|
}
|
|
|
|
|
|
2017-04-05 23:47:29 +08:00
|
|
|
module.exports = {
|
2017-11-03 03:50:03 +08:00
|
|
|
getImportSideEffects,
|
2017-10-25 07:55:00 +08:00
|
|
|
getPeerGlobals,
|
|
|
|
|
getDependencies,
|
|
|
|
|
getShims,
|
2017-04-05 23:47:29 +08:00
|
|
|
};
|