Skip to content

Commit a9321df

Browse files
authored
Merge pull request #713 from GetStream/metro-dev-helpers
Use new metro-dev-helpers instead of copy-pasted metro config on examples
2 parents 703c47c + 5ac8a0f commit a9321df

File tree

9 files changed

+219
-644
lines changed

9 files changed

+219
-644
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ yarn-error.log
1010
.DS_Store
1111
dist/
1212
lib/
13+
# lib/ ignores metro-dev-helpers/lib so:
14+
!metro-dev-helpers/lib
1315
.env*
1416
examples/SampleApp/ios/vendor/
1517
vendor
Lines changed: 5 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,19 @@
11
/* 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-
}
1262

1273
const PATH = require('path');
128-
const FS = require('fs'),
129-
blacklist = require('metro-config/src/defaults/blacklist');
4+
const blacklist = require('metro-config/src/defaults/blackList');
1305

131-
const repoDir = PATH.dirname(PATH.dirname(__dirname));
6+
const extractLinkedPackages = require('stream-chat-react-native-core/metro-dev-helpers/extract-linked-packages');
1327

133-
const moduleBlacklist = [
134-
new RegExp(repoDir + '/examples/NativeMessaging/.*'),
135-
new RegExp(repoDir + '/examples/SampleApp/.*'),
136-
new RegExp(repoDir + '/examples/TypeScriptMessaging/.*'),
137-
new RegExp(repoDir + '/native-package/.*'),
138-
new RegExp(repoDir + '/expo-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);
1479

148-
if (alternateRoots && alternateRoots.length)
149-
console.log('Found alternate project roots: ', alternateRoots);
10+
const { alternateRoots, extraNodeModules, moduleBlacklist } = extractLinkedPackages(projectRoot);
15011

15112
module.exports = {
15213
resolver: {
15314
blacklistRE: blacklist(moduleBlacklist),
15415
extraNodeModules,
15516
useWatchman: false,
15617
},
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),
16419
};
Lines changed: 6 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,19 @@
11
/* 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-
}
1262

1273
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');
1305

131-
const repoDir = PATH.dirname(PATH.dirname(__dirname));
6+
const extractLinkedPackages = require('stream-chat-react-native-core/metro-dev-helpers/extract-linked-packages');
1327

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);
1479

148-
if (alternateRoots && alternateRoots.length)
149-
console.log('Found alternate project roots: ', alternateRoots);
10+
const { alternateRoots, extraNodeModules, moduleBlacklist } = extractLinkedPackages(projectRoot);
15011

15112
module.exports = {
15213
resolver: {
153-
blacklistRE: exclusionList(moduleExclusionList),
14+
blacklistRE: blacklist(moduleBlacklist),
15415
extraNodeModules,
15516
useWatchman: false,
15617
},
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),
16419
};

0 commit comments

Comments
 (0)