1. 'use strict';
    
  2. 
    
  3. const forks = require('./forks');
    
  4. const {UMD_DEV, UMD_PROD, UMD_PROFILING} = require('./bundles').bundleTypes;
    
  5. 
    
  6. // For any external that is used in a DEV-only condition, explicitly
    
  7. // specify whether it has side effects during import or not. This lets
    
  8. // us know whether we can safely omit them when they are unused.
    
  9. const HAS_NO_SIDE_EFFECTS_ON_IMPORT = false;
    
  10. // const HAS_SIDE_EFFECTS_ON_IMPORT = true;
    
  11. const importSideEffects = Object.freeze({
    
  12.   fs: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  13.   'fs/promises': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  14.   path: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  15.   stream: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  16.   'prop-types/checkPropTypes': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  17.   'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface':
    
  18.     HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  19.   scheduler: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  20.   react: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  21.   'react-dom/server': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  22.   'react/jsx-dev-runtime': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  23.   'react-dom': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  24.   url: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  25.   ReactNativeInternalFeatureFlags: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
    
  26. });
    
  27. 
    
  28. // Bundles exporting globals that other modules rely on.
    
  29. const knownGlobals = Object.freeze({
    
  30.   react: 'React',
    
  31.   'react-dom': 'ReactDOM',
    
  32.   'react-dom/server': 'ReactDOMServer',
    
  33.   'react-interactions/events/tap': 'ReactEventsTap',
    
  34.   scheduler: 'Scheduler',
    
  35.   'scheduler/unstable_mock': 'SchedulerMock',
    
  36.   ReactNativeInternalFeatureFlags: 'ReactNativeInternalFeatureFlags',
    
  37. });
    
  38. 
    
  39. // Given ['react'] in bundle externals, returns { 'react': 'React' }.
    
  40. function getPeerGlobals(externals, bundleType) {
    
  41.   const peerGlobals = {};
    
  42.   externals.forEach(name => {
    
  43.     if (
    
  44.       !knownGlobals[name] &&
    
  45.       (bundleType === UMD_DEV ||
    
  46.         bundleType === UMD_PROD ||
    
  47.         bundleType === UMD_PROFILING)
    
  48.     ) {
    
  49.       throw new Error('Cannot build UMD without a global name for: ' + name);
    
  50.     }
    
  51.     peerGlobals[name] = knownGlobals[name];
    
  52.   });
    
  53.   return peerGlobals;
    
  54. }
    
  55. 
    
  56. // Determines node_modules packages that are safe to assume will exist.
    
  57. function getDependencies(bundleType, entry) {
    
  58.   // Replaces any part of the entry that follow the package name (like
    
  59.   // "/server" in "react-dom/server") by the path to the package settings
    
  60.   const packageJson = require(entry.replace(/(\/.*)?$/, '/package.json'));
    
  61.   // Both deps and peerDeps are assumed as accessible.
    
  62.   return Array.from(
    
  63.     new Set([
    
  64.       ...Object.keys(packageJson.dependencies || {}),
    
  65.       ...Object.keys(packageJson.peerDependencies || {}),
    
  66.     ])
    
  67.   );
    
  68. }
    
  69. 
    
  70. // Hijacks some modules for optimization and integration reasons.
    
  71. function getForks(bundleType, entry, moduleType, bundle) {
    
  72.   const forksForBundle = {};
    
  73.   Object.keys(forks).forEach(srcModule => {
    
  74.     const dependencies = getDependencies(bundleType, entry);
    
  75.     const targetModule = forks[srcModule](
    
  76.       bundleType,
    
  77.       entry,
    
  78.       dependencies,
    
  79.       moduleType,
    
  80.       bundle
    
  81.     );
    
  82.     if (targetModule === null) {
    
  83.       return;
    
  84.     }
    
  85.     forksForBundle[srcModule] = targetModule;
    
  86.   });
    
  87.   return forksForBundle;
    
  88. }
    
  89. 
    
  90. function getImportSideEffects() {
    
  91.   return importSideEffects;
    
  92. }
    
  93. 
    
  94. module.exports = {
    
  95.   getImportSideEffects,
    
  96.   getPeerGlobals,
    
  97.   getDependencies,
    
  98.   getForks,
    
  99. };