1const { createMetroConfiguration } = require('expo-yarn-workspaces');
2
3const baseConfig = createMetroConfiguration(__dirname);
4
5if (process.env.EXPO_USE_EXOTIC) {
6  // Use the custom transformer when exotic is enabled.
7  baseConfig.transformer.babelTransformerPath = require.resolve('./metro.transformer.js');
8}
9
10module.exports = {
11  ...baseConfig,
12
13  // NOTE(brentvatne): This can be removed when
14  // https://github.com/facebook/metro/issues/290 is fixed.
15  server: {
16    enhanceMiddleware: (middleware) => {
17      return (req, res, next) => {
18        // When an asset is imported outside the project root, it has wrong path on Android
19        // This happens for the back button in stack, so we fix the path to correct one
20        const assets = '/node_modules/@react-navigation/stack/src/views/assets';
21
22        if (req.url.startsWith(assets)) {
23          req.url = req.url.replace(assets, `/assets/../..${assets}`);
24        }
25
26        return middleware(req, res, next);
27      };
28    },
29  },
30
31  resolver: {
32    ...baseConfig.resolver,
33    blockList: [
34      ...baseConfig.resolver.blockList,
35
36      // Because react-native versions may be different between node_modules/react-native and react-native-lab,
37      // metro and react-native cannot serve duplicated files from different paths.
38      // Assuming NCL only serves for Expo Go,
39      // the strategy here is to serve react-native imports from `react-native-lab/react-native` but not its transitive dependencies.
40      // That is not ideal but should work for most cases if the two react-native versions do not have too much difference.
41      // For example, `react-native-lab/react-native/node_modules/@react-native/polyfills` and `node_modules/@react-native/polyfills` may be different,
42      // the metro config will use the transitive dependency from `node_modules/@react-native/polyfills`.
43      /\bnode_modules\/react-native\/\b/,
44      /\breact-native-lab\/react-native\/node_modules\b/,
45    ],
46  },
47};
48