|
1 | 1 | /* eslint-env node */ |
2 | | -function resolvePath(...parts) { |
3 | | - const thisPath = PATH.resolve.apply(PATH, parts); |
4 | | - if (!FS.existsSync(thisPath)) return; |
5 | | - |
6 | | - return FS.realpathSync(thisPath); |
7 | | -} |
8 | | - |
9 | | -function isExternalModule(modulePath) { |
10 | | - return modulePath.substring(0, __dirname.length) !== __dirname; |
11 | | -} |
12 | | - |
13 | | -function listDirectories(rootPath, cb) { |
14 | | - FS.readdirSync(rootPath).forEach((fileName) => { |
15 | | - if (fileName.charAt(0) === '.') return; |
16 | | - |
17 | | - let fullFileName = PATH.join(rootPath, fileName), |
18 | | - stats = FS.lstatSync(fullFileName), |
19 | | - symbolic = false; |
20 | | - |
21 | | - if (stats.isSymbolicLink()) { |
22 | | - fullFileName = resolvePath(fullFileName); |
23 | | - if (!fullFileName) return; |
24 | | - |
25 | | - stats = FS.lstatSync(fullFileName); |
26 | | - |
27 | | - symbolic = true; |
28 | | - } |
29 | | - |
30 | | - if (!stats.isDirectory()) return; |
31 | | - |
32 | | - const external = isExternalModule(fullFileName); |
33 | | - cb({ rootPath, symbolic, external, fullFileName, fileName }); |
34 | | - }); |
35 | | -} |
36 | | - |
37 | | -function buildFullModuleMap( |
38 | | - moduleRoot, |
39 | | - mainModuleMap, |
40 | | - externalModuleMap, |
41 | | - _alreadyVisited, |
42 | | - _prefix, |
43 | | -) { |
44 | | - if (!moduleRoot) return; |
45 | | - |
46 | | - const alreadyVisited = _alreadyVisited || {}, |
47 | | - prefix = _prefix; |
48 | | - |
49 | | - if (alreadyVisited && alreadyVisited.hasOwnProperty(moduleRoot)) return; |
50 | | - |
51 | | - alreadyVisited[moduleRoot] = true; |
52 | | - |
53 | | - listDirectories(moduleRoot, ({ fileName, fullFileName, symbolic, external }) => { |
54 | | - if (symbolic) |
55 | | - return buildFullModuleMap( |
56 | | - resolvePath(fullFileName, 'node_modules'), |
57 | | - mainModuleMap, |
58 | | - externalModuleMap, |
59 | | - alreadyVisited, |
60 | | - ); |
61 | | - |
62 | | - const moduleMap = external ? externalModuleMap : mainModuleMap, |
63 | | - moduleName = prefix ? PATH.join(prefix, fileName) : fileName; |
64 | | - |
65 | | - if (fileName.charAt(0) !== '@') moduleMap[moduleName] = fullFileName; |
66 | | - else |
67 | | - return buildFullModuleMap( |
68 | | - fullFileName, |
69 | | - mainModuleMap, |
70 | | - externalModuleMap, |
71 | | - alreadyVisited, |
72 | | - fileName, |
73 | | - ); |
74 | | - }); |
75 | | -} |
76 | | - |
77 | | -function buildModuleResolutionMap() { |
78 | | - const moduleMap = {}, |
79 | | - externalModuleMap = {}; |
80 | | - |
81 | | - buildFullModuleMap(baseModulePath, moduleMap, externalModuleMap); |
82 | | - |
83 | | - // Root project modules take precedence over external modules |
84 | | - return Object.assign({}, externalModuleMap, moduleMap); |
85 | | -} |
86 | | - |
87 | | -function findAlternateRoots(moduleRoot = baseModulePath, alternateRoots = [], _alreadyVisited) { |
88 | | - const alreadyVisited = _alreadyVisited || {}; |
89 | | - if (alreadyVisited && alreadyVisited.hasOwnProperty(moduleRoot)) return; |
90 | | - |
91 | | - alreadyVisited[moduleRoot] = true; |
92 | | - |
93 | | - listDirectories(moduleRoot, ({ fullFileName, fileName, external }) => { |
94 | | - if (fileName.charAt(0) !== '@') { |
95 | | - if (external) alternateRoots.push(fullFileName); |
96 | | - } else { |
97 | | - findAlternateRoots(fullFileName, alternateRoots, alreadyVisited); |
98 | | - } |
99 | | - }); |
100 | | - |
101 | | - return alternateRoots; |
102 | | -} |
103 | | - |
104 | | -function getPolyfillHelper() { |
105 | | - let getPolyfills; |
106 | | - |
107 | | - // Get default react-native polyfills |
108 | | - try { |
109 | | - getPolyfills = require('react-native/rn-get-polyfills'); |
110 | | - } catch (e) { |
111 | | - getPolyfills = () => []; |
112 | | - } |
113 | | - |
114 | | - // See if project has custom polyfills, if so, include the PATH to them |
115 | | - try { |
116 | | - const customPolyfills = require.resolve('./polyfills.js'); |
117 | | - getPolyfills = (function (originalGetPolyfills) { |
118 | | - return () => originalGetPolyfills().concat(customPolyfills); |
119 | | - })(getPolyfills); |
120 | | - } catch (e) { |
121 | | - //ignore |
122 | | - } |
123 | | - |
124 | | - return getPolyfills; |
125 | | -} |
126 | 2 |
|
127 | 3 | const PATH = require('path'); |
128 | | -const FS = require('fs'), |
129 | | - exclusionList = require('metro-config/src/defaults/exclusionList'); |
| 4 | +const blacklist = require('metro-config/src/defaults/exclusionList'); |
130 | 5 |
|
131 | | -const repoDir = PATH.dirname(PATH.dirname(__dirname)); |
| 6 | +const extractLinkedPackages = require('stream-chat-react-native-core/metro-dev-helpers/extract-linked-packages'); |
132 | 7 |
|
133 | | -const moduleExclusionList = [ |
134 | | - new RegExp(repoDir + '/examples/ExpoMessaging/.*'), |
135 | | - new RegExp(repoDir + '/examples/SampleApp/.*'), |
136 | | - new RegExp(repoDir + '/examples/TypeScriptMessaging/.*'), |
137 | | - new RegExp(repoDir + '/expo-package/.*'), |
138 | | - new RegExp(repoDir + '/native-package/node_modules/.*'), |
139 | | - new RegExp(repoDir + '/node_modules/.*'), |
140 | | - ], |
141 | | - baseModulePath = resolvePath(__dirname, 'node_modules'), |
142 | | - // watch alternate roots (outside of project root) |
143 | | - alternateRoots = findAlternateRoots(), |
144 | | - // build full module map for proper |
145 | | - // resolution of modules in external roots |
146 | | - extraNodeModules = buildModuleResolutionMap(); |
| 8 | +const projectRoot = PATH.resolve(__dirname); |
147 | 9 |
|
148 | | -if (alternateRoots && alternateRoots.length) |
149 | | - console.log('Found alternate project roots: ', alternateRoots); |
| 10 | +const { alternateRoots, extraNodeModules, moduleBlacklist } = extractLinkedPackages(projectRoot); |
150 | 11 |
|
151 | 12 | module.exports = { |
152 | 13 | resolver: { |
153 | | - blacklistRE: exclusionList(moduleExclusionList), |
| 14 | + blacklistRE: blacklist(moduleBlacklist), |
154 | 15 | extraNodeModules, |
155 | 16 | useWatchman: false, |
156 | 17 | }, |
157 | | - watchFolders: [PATH.resolve(__dirname)].concat(alternateRoots), |
158 | | - // transformer: { |
159 | | - // babelTransformerPath: require.resolve('./compiler/transformer'), |
160 | | - // }, |
161 | | - serializer: { |
162 | | - getPolyfills: getPolyfillHelper(), |
163 | | - }, |
| 18 | + watchFolders: [projectRoot].concat(alternateRoots), |
164 | 19 | }; |
0 commit comments