|
| 1 | +import type {InputConfigT} from 'metro-config'; |
| 2 | +import path from 'path'; |
| 3 | +import type {ConfigLoadingContext} from './loadMetroConfig'; |
| 4 | + |
| 5 | +/** |
| 6 | + * This module reproduces defaults from the @react-native/metro-config package, |
| 7 | + * used in ./loadMetroConfig.js to provide a soft upgrade process when upgrading |
| 8 | + * to React Native 0.72. |
| 9 | + * |
| 10 | + * These values will be used when: |
| 11 | + * - RN CLI 11.x or greater is present in a project (from React Native 0.72). |
| 12 | + * - The project has not yet followed the upgrade instructions to update |
| 13 | + * metro.config.js to extend '@react-native/metro-config'. |
| 14 | + * |
| 15 | + * Until we remove this file in a future release, updates should be made both |
| 16 | + * here and in '@react-native/metro-config'. (Note: Updates to these values are |
| 17 | + * generally rare.) |
| 18 | + * |
| 19 | + * TODO(@huntie): Remove this file in a future React Native release. |
| 20 | + */ |
| 21 | + |
| 22 | +/** |
| 23 | + * @deprecated (React Native 0.72.0) Defaults should be updated here and in |
| 24 | + * https://github.com/facebook/react-native/tree/main/package/metro-config/index.js |
| 25 | + */ |
| 26 | +const INTERNAL_CALLSITES_REGEX = new RegExp( |
| 27 | + [ |
| 28 | + '/Libraries/Renderer/implementations/.+\\.js$', |
| 29 | + '/Libraries/BatchedBridge/MessageQueue\\.js$', |
| 30 | + '/Libraries/YellowBox/.+\\.js$', |
| 31 | + '/Libraries/LogBox/.+\\.js$', |
| 32 | + '/Libraries/Core/Timers/.+\\.js$', |
| 33 | + '/Libraries/WebSocket/.+\\.js$', |
| 34 | + '/Libraries/vendor/.+\\.js$', |
| 35 | + '/node_modules/react-devtools-core/.+\\.js$', |
| 36 | + '/node_modules/react-refresh/.+\\.js$', |
| 37 | + '/node_modules/scheduler/.+\\.js$', |
| 38 | + '/node_modules/event-target-shim/.+\\.js$', |
| 39 | + '/node_modules/invariant/.+\\.js$', |
| 40 | + '/node_modules/react-native/index.js$', |
| 41 | + '/metro-runtime/.+\\.js$', |
| 42 | + '^\\[native code\\]$', |
| 43 | + ].join('|'), |
| 44 | +); |
| 45 | + |
| 46 | +/** |
| 47 | + * Get the static Metro config defaults for a React Native project. |
| 48 | + * |
| 49 | + * @deprecated (React Native 0.72.0) Defaults should be updated here and in |
| 50 | + * https://github.com/facebook/react-native/tree/main/package/metro-config/index.js |
| 51 | + */ |
| 52 | +export default function getDefaultMetroConfig( |
| 53 | + ctx: ConfigLoadingContext, |
| 54 | +): InputConfigT { |
| 55 | + return { |
| 56 | + resolver: { |
| 57 | + resolverMainFields: ['react-native', 'browser', 'main'], |
| 58 | + unstable_conditionNames: ['require', 'react-native'], |
| 59 | + }, |
| 60 | + serializer: { |
| 61 | + getPolyfills: () => |
| 62 | + require(path.join(ctx.reactNativePath, 'rn-get-polyfills'))(), |
| 63 | + }, |
| 64 | + server: { |
| 65 | + port: Number(process.env.RCT_METRO_PORT) || 8081, |
| 66 | + }, |
| 67 | + symbolicator: { |
| 68 | + customizeFrame: (frame: {file?: string}) => { |
| 69 | + const collapse = Boolean( |
| 70 | + frame.file && INTERNAL_CALLSITES_REGEX.test(frame.file), |
| 71 | + ); |
| 72 | + return {collapse}; |
| 73 | + }, |
| 74 | + }, |
| 75 | + transformer: { |
| 76 | + allowOptionalDependencies: true, |
| 77 | + assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry', |
| 78 | + asyncRequireModulePath: require.resolve( |
| 79 | + 'metro-runtime/src/modules/asyncRequire', |
| 80 | + ), |
| 81 | + babelTransformerPath: require.resolve( |
| 82 | + 'metro-react-native-babel-transformer', |
| 83 | + ), |
| 84 | + }, |
| 85 | + watchFolders: [], |
| 86 | + }; |
| 87 | +} |
0 commit comments