2017-04-05 16:47:29 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-11-30 12:11:00 +00:00
|
|
|
const forks = require('./forks');
|
2020-02-03 23:31:31 +00:00
|
|
|
const {UMD_DEV, UMD_PROD, UMD_PROFILING} = require('./bundles').bundleTypes;
|
2017-10-25 02:55:00 +03:00
|
|
|
|
2017-11-02 19:50:03 +00: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({
|
2020-12-03 21:21:19 +00:00
|
|
|
fs: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2020-12-09 21:46:50 +00:00
|
|
|
'fs/promises': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2020-12-03 21:21:19 +00:00
|
|
|
path: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2022-06-18 22:34:31 -04:00
|
|
|
stream: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2017-11-02 19:50:03 +00:00
|
|
|
'prop-types/checkPropTypes': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2019-05-23 00:23:54 -07:00
|
|
|
'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2018-09-19 01:26:28 +01:00
|
|
|
scheduler: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2021-03-24 02:13:43 +00:00
|
|
|
react: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2019-11-01 17:39:24 -07:00
|
|
|
'react-dom/server': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2020-03-17 13:22:19 -07:00
|
|
|
'react/jsx-dev-runtime': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2020-05-12 12:21:45 -04:00
|
|
|
'react-fetch/node': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2020-06-11 19:49:45 +01:00
|
|
|
'react-dom': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2020-12-03 21:21:19 +00:00
|
|
|
url: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2021-08-03 22:30:20 -04:00
|
|
|
ReactNativeInternalFeatureFlags: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
|
2017-11-02 19:50:03 +00:00
|
|
|
});
|
|
|
|
|
|
2017-11-30 12:11:00 +00:00
|
|
|
// Bundles exporting globals that other modules rely on.
|
|
|
|
|
const knownGlobals = Object.freeze({
|
|
|
|
|
react: 'React',
|
|
|
|
|
'react-dom': 'ReactDOM',
|
2019-11-01 17:39:24 -07:00
|
|
|
'react-dom/server': 'ReactDOMServer',
|
2019-09-20 11:51:03 +01:00
|
|
|
'react-interactions/events/tap': 'ReactEventsTap',
|
2018-09-19 01:26:28 +01:00
|
|
|
scheduler: 'Scheduler',
|
2019-02-28 12:54:47 -08:00
|
|
|
'scheduler/unstable_mock': 'SchedulerMock',
|
2021-08-03 22:30:20 -04:00
|
|
|
ReactNativeInternalFeatureFlags: 'ReactNativeInternalFeatureFlags',
|
2017-11-30 12:11:00 +00:00
|
|
|
});
|
|
|
|
|
|
2017-10-25 02:55:00 +03:00
|
|
|
// Given ['react'] in bundle externals, returns { 'react': 'React' }.
|
2018-05-18 00:16:03 +08:00
|
|
|
function getPeerGlobals(externals, bundleType) {
|
2017-10-25 02:55:00 +03:00
|
|
|
const peerGlobals = {};
|
|
|
|
|
externals.forEach(name => {
|
|
|
|
|
if (
|
|
|
|
|
!knownGlobals[name] &&
|
2018-09-13 17:44:08 -07:00
|
|
|
(bundleType === UMD_DEV ||
|
|
|
|
|
bundleType === UMD_PROD ||
|
|
|
|
|
bundleType === UMD_PROFILING)
|
2017-10-25 02:55:00 +03:00
|
|
|
) {
|
|
|
|
|
throw new Error('Cannot build UMD without a global name for: ' + name);
|
|
|
|
|
}
|
|
|
|
|
peerGlobals[name] = knownGlobals[name];
|
2017-04-05 16:47:29 +01:00
|
|
|
});
|
2017-10-25 02:55:00 +03:00
|
|
|
return peerGlobals;
|
2017-04-05 16:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
2017-10-25 02:55:00 +03:00
|
|
|
// Determines node_modules packages that are safe to assume will exist.
|
|
|
|
|
function getDependencies(bundleType, entry) {
|
2018-03-23 19:51:04 +00:00
|
|
|
// Replaces any part of the entry that follow the package name (like
|
|
|
|
|
// "/server" in "react-dom/server") by the path to the package settings
|
|
|
|
|
const packageJson = require(entry.replace(/(\/.*)?$/, '/package.json'));
|
2017-10-25 02:55:00 +03:00
|
|
|
// Both deps and peerDeps are assumed as accessible.
|
2017-11-30 12:11:00 +00:00
|
|
|
return Array.from(
|
2017-10-25 02:55:00 +03:00
|
|
|
new Set([
|
|
|
|
|
...Object.keys(packageJson.dependencies || {}),
|
|
|
|
|
...Object.keys(packageJson.peerDependencies || {}),
|
|
|
|
|
])
|
|
|
|
|
);
|
2017-04-05 16:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
2017-11-30 12:11:00 +00:00
|
|
|
// Hijacks some modules for optimization and integration reasons.
|
2020-03-12 11:38:32 -07:00
|
|
|
function getForks(bundleType, entry, moduleType, bundle) {
|
2017-11-30 12:11:00 +00:00
|
|
|
const forksForBundle = {};
|
|
|
|
|
Object.keys(forks).forEach(srcModule => {
|
|
|
|
|
const dependencies = getDependencies(bundleType, entry);
|
2018-05-19 11:29:11 +01:00
|
|
|
const targetModule = forks[srcModule](
|
|
|
|
|
bundleType,
|
|
|
|
|
entry,
|
|
|
|
|
dependencies,
|
2020-03-12 11:38:32 -07:00
|
|
|
moduleType,
|
|
|
|
|
bundle
|
2018-05-19 11:29:11 +01:00
|
|
|
);
|
2017-11-30 12:11:00 +00:00
|
|
|
if (targetModule === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
forksForBundle[srcModule] = targetModule;
|
|
|
|
|
});
|
|
|
|
|
return forksForBundle;
|
2017-11-02 19:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-30 12:11:00 +00:00
|
|
|
function getImportSideEffects() {
|
|
|
|
|
return importSideEffects;
|
2017-04-05 19:51:19 +01:00
|
|
|
}
|
|
|
|
|
|
2017-04-05 16:47:29 +01:00
|
|
|
module.exports = {
|
2017-11-02 19:50:03 +00:00
|
|
|
getImportSideEffects,
|
2017-10-25 02:55:00 +03:00
|
|
|
getPeerGlobals,
|
|
|
|
|
getDependencies,
|
2017-11-30 12:11:00 +00:00
|
|
|
getForks,
|
2017-04-05 16:47:29 +01:00
|
|
|
};
|