mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
* Unify the way we fork modules * Replace rollup-plugin-alias with our own plugin This does exactly what we need and doesn't suffer from https://github.com/rollup/rollup-plugin-alias/issues/34. * Move the new plugin to its own file * Rename variable for consistency I settled on calling them "forks" since we already have a different concept of "shims". * Move fork config into its own file
83 lines
2.5 KiB
JavaScript
83 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
const path = require('path');
|
|
const forks = require('./forks');
|
|
const bundleTypes = require('./bundles').bundleTypes;
|
|
|
|
const UMD_DEV = bundleTypes.UMD_DEV;
|
|
const UMD_PROD = bundleTypes.UMD_PROD;
|
|
|
|
// 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,
|
|
});
|
|
|
|
// Bundles exporting globals that other modules rely on.
|
|
const knownGlobals = Object.freeze({
|
|
react: 'React',
|
|
'react-dom': 'ReactDOM',
|
|
});
|
|
|
|
// 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];
|
|
});
|
|
return peerGlobals;
|
|
}
|
|
|
|
// 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.
|
|
return Array.from(
|
|
new Set([
|
|
...Object.keys(packageJson.dependencies || {}),
|
|
...Object.keys(packageJson.peerDependencies || {}),
|
|
])
|
|
);
|
|
}
|
|
|
|
// Hijacks some modules for optimization and integration reasons.
|
|
function getForks(bundleType, entry) {
|
|
const forksForBundle = {};
|
|
Object.keys(forks).forEach(srcModule => {
|
|
const dependencies = getDependencies(bundleType, entry);
|
|
const targetModule = forks[srcModule](bundleType, entry, dependencies);
|
|
if (targetModule === null) {
|
|
return;
|
|
}
|
|
forksForBundle[srcModule] = targetModule;
|
|
});
|
|
return forksForBundle;
|
|
}
|
|
|
|
function getImportSideEffects() {
|
|
return importSideEffects;
|
|
}
|
|
|
|
module.exports = {
|
|
getImportSideEffects,
|
|
getPeerGlobals,
|
|
getDependencies,
|
|
getForks,
|
|
};
|