forked from LedgerHQ/ledger-live
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetro.config.js
101 lines (87 loc) · 2.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
/**
* Metro configuration for React Native
* https://reactnative.dev/docs/metro
*
* @format
*/
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-console */
const path = require("path");
const tsconfig = require("./tsconfig.json");
const forcedDependencies = [
"react-redux",
"react-native",
"react-native-svg",
"styled-components",
"react-native-reanimated",
];
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
const removeStarPath = moduleName => moduleName.replace("/*", "");
const buildTsAlias = (conf = {}) =>
Object.keys(conf).reduce(
(acc, moduleName) => ({
...acc,
[removeStarPath(moduleName)]: path.resolve(__dirname, removeStarPath(conf[moduleName][0])),
}),
{},
);
const projectRootDir = path.join(__dirname, "..", "..");
function forceDependency(moduleName, filters, nodeModulesPaths) {
const matches = filters.some(
filter => moduleName === filter || moduleName.startsWith(`${filter}/`),
);
if (matches) {
const resolution = require.resolve(moduleName, {
paths: nodeModulesPaths,
});
return {
filePath: resolution,
type: "sourceFile",
};
}
return null;
}
const nodeModulesPaths = [
path.resolve(__dirname, "node_modules"),
path.resolve(projectRootDir, "node_modules"),
path.resolve(projectRootDir, "node_modules", ".pnpm"),
];
const metroConfig = {
projectRoot: path.resolve(__dirname),
watchFolders: [projectRootDir],
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
}),
},
resolver: {
unstable_enableSymlinks: true,
unstable_enablePackageExports: true,
unstable_conditionNames: ["require", "react-native", "browser"],
nodeModulesPaths,
resolverMainFields: ["react-native", "browser", "main"],
extraNodeModules: {
...require("node-libs-react-native"),
fs: require.resolve("react-native-level-fs"),
net: require.resolve("react-native-tcp-socket"),
tls: require.resolve("tls"),
...buildTsAlias(tsconfig.compilerOptions.paths),
},
resolveRequest: (context, moduleName, platform) => {
if (["tls", "http2", "dns"].includes(moduleName)) {
return { type: "empty" };
}
try {
const forcedResolution = forceDependency(moduleName, forcedDependencies, nodeModulesPaths);
if (forcedResolution) return forcedResolution;
} catch (e) {
console.log(e);
}
return context.resolveRequest(context, moduleName, platform);
},
},
};
module.exports = mergeConfig(getDefaultConfig(__dirname), metroConfig);