-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathmetro.config.js
69 lines (56 loc) · 2.6 KB
/
metro.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const { getDefaultConfig } = require('@expo/metro-config');
const { exclusionList, resolveUniqueModule } = require('@rnx-kit/metro-config');
/**
* Metro configuration
* https://facebook.github.io/metro/docs/configuration
*
* @type {import('metro-config').MetroConfig}
*/
const config = getDefaultConfig(__dirname);
const PATH = require('path');
const packageDirPath = PATH.resolve(__dirname, '../../package');
const expoPackageDirPath = PATH.resolve(__dirname, '../../package/expo-package');
const symlinked = {
'stream-chat-expo': expoPackageDirPath,
'stream-chat-react-native-core': packageDirPath,
};
// find what all modules need to be unique for the app (mainly react and react-native)
// note: we filter the symlinked modules as they are already unique
// and as they dont follow the workspace pattern the auto-generated path to the module is incorrect
const dependencyPackageNames = Object.keys(require('./package.json').dependencies);
const uniqueModules = dependencyPackageNames.map((packageName) => {
if (symlinked[packageName]) {
const modulePath = symlinked[packageName];
const escapedPackageName = PATH.normalize(packageName).replace(/\\/g, '\\\\');
// exclude the symlinked package from being resolved from node_modules
// example: .*\/node_modules\/stream-chat-react-native-core\/.*
// the above would avoid native-package to resolve core from its own node_modules
const exclusionRE = new RegExp(
`.*${PATH.sep}node_modules\\${PATH.sep}${escapedPackageName}\\${PATH.sep}.*`,
);
return {
packageName, // name of the package
modulePath,
blockPattern: exclusionRE, // paths that match this pattern will be blocked from being resolved
};
}
const [modulePath, blockPattern] = resolveUniqueModule(packageName, __dirname);
return {
packageName, // name of the package
modulePath, // actual path to the module in the project's node modules
blockPattern, // paths that match this pattern will be blocked from being resolved
};
});
// block the other paths for unique modules from being resolved
const blockList = uniqueModules.map(({ blockPattern }) => blockPattern);
// provide the path for the unique modules
const extraNodeModules = uniqueModules.reduce((acc, item) => {
acc[item.packageName] = item.modulePath;
return acc;
}, {});
config.resolver.blockList = exclusionList(blockList);
config.resolver.extraNodeModules = extraNodeModules;
config.resolver.nodeModulesPaths = [PATH.resolve(__dirname, 'node_modules')];
// add the package dir for metro to access the package folder
config.watchFolders = [packageDirPath];
module.exports = config;