-
Notifications
You must be signed in to change notification settings - Fork 199
/
metro.config.js
173 lines (142 loc) · 4.66 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* eslint-env node */
function resolvePath(...parts) {
const thisPath = PATH.resolve.apply(PATH, parts);
if (!FS.existsSync(thisPath)) return;
return FS.realpathSync(thisPath);
}
function isExternalModule(modulePath) {
return modulePath.substring(0, __dirname.length) !== __dirname;
}
function listDirectories(rootPath, cb) {
FS.readdirSync(rootPath).forEach(fileName => {
if (fileName.charAt(0) === ".") return;
let fullFileName = PATH.join(rootPath, fileName),
stats = FS.lstatSync(fullFileName),
symbolic = false;
if (stats.isSymbolicLink()) {
fullFileName = resolvePath(fullFileName);
if (!fullFileName) return;
stats = FS.lstatSync(fullFileName);
symbolic = true;
}
if (!stats.isDirectory()) return;
const external = isExternalModule(fullFileName);
cb({ rootPath, symbolic, external, fullFileName, fileName });
});
}
function buildFullModuleMap(
moduleRoot,
mainModuleMap,
externalModuleMap,
_alreadyVisited,
_prefix
) {
if (!moduleRoot) return;
const alreadyVisited = _alreadyVisited || {},
prefix = _prefix;
if (alreadyVisited && alreadyVisited.hasOwnProperty(moduleRoot)) return;
alreadyVisited[moduleRoot] = true;
listDirectories(
moduleRoot,
({ fileName, fullFileName, symbolic, external }) => {
if (symbolic)
return buildFullModuleMap(
resolvePath(fullFileName, "node_modules"),
mainModuleMap,
externalModuleMap,
alreadyVisited
);
const moduleMap = external ? externalModuleMap : mainModuleMap,
moduleName = prefix ? PATH.join(prefix, fileName) : fileName;
if (fileName.charAt(0) !== "@") moduleMap[moduleName] = fullFileName;
else
return buildFullModuleMap(
fullFileName,
mainModuleMap,
externalModuleMap,
alreadyVisited,
fileName
);
}
);
}
function buildModuleResolutionMap() {
const moduleMap = {},
externalModuleMap = {};
buildFullModuleMap(baseModulePath, moduleMap, externalModuleMap);
// Root project modules take precedence over external modules
return Object.assign({}, externalModuleMap, moduleMap);
}
function findAlternateRoots(
moduleRoot = baseModulePath,
alternateRoots = [],
_alreadyVisited
) {
const alreadyVisited = _alreadyVisited || {};
if (alreadyVisited && alreadyVisited.hasOwnProperty(moduleRoot)) return;
alreadyVisited[moduleRoot] = true;
listDirectories(moduleRoot, ({ fullFileName, fileName, external }) => {
if (fileName.charAt(0) !== "@") {
if (external) alternateRoots.push(fullFileName);
} else {
findAlternateRoots(fullFileName, alternateRoots, alreadyVisited);
}
});
return alternateRoots;
}
function getPolyfillHelper() {
let getPolyfills;
// Get default react-native polyfills
try {
getPolyfills = require("react-native/rn-get-polyfills");
} catch (e) {
getPolyfills = () => [];
}
// See if project has custom polyfills, if so, include the PATH to them
try {
const customPolyfills = require.resolve("./polyfills.js");
getPolyfills = (function(originalGetPolyfills) {
return () => originalGetPolyfills().concat(customPolyfills);
})(getPolyfills);
} catch (e) {
//ignore
}
return getPolyfills;
}
const PATH = require("path");
const FS = require("fs"),
blacklist = require("metro-config/src/defaults/blacklist");
const repoDir = PATH.dirname(__dirname);
console.log(">>>>", repoDir);
const moduleBlacklist = [
new RegExp(repoDir + "/react-native-activity-feed/examples/.*"),
new RegExp(repoDir + "/react-native-activity-feed/native-example/.*"),
new RegExp(repoDir + "/react-native-activity-feed/example/.*"),
new RegExp(repoDir + "/react-native-activity-feed/native-package/.*"),
new RegExp(
repoDir + "/react-native-activity-feed/expo-package/node_modules/.*"
),
new RegExp(repoDir + "/react-native-activity-feed/node_modules/.*")
],
baseModulePath = resolvePath(__dirname, "node_modules"),
// watch alternate roots (outside of project root)
alternateRoots = findAlternateRoots(),
// build full module map for proper
// resolution of modules in external roots
extraNodeModules = buildModuleResolutionMap();
if (alternateRoots && alternateRoots.length)
console.log("Found alternate project roots: ", alternateRoots);
module.exports = {
resolver: {
blacklistRE: blacklist(moduleBlacklist),
extraNodeModules,
useWatchman: false
},
watchFolders: [PATH.resolve(__dirname)].concat(alternateRoots),
// transformer: {
// babelTransformerPath: require.resolve('./compiler/transformer'),
// },
serializer: {
getPolyfills: getPolyfillHelper()
}
};