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