-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathmetro.config.js
More file actions
64 lines (57 loc) · 2.15 KB
/
Copy pathmetro.config.js
File metadata and controls
64 lines (57 loc) · 2.15 KB
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
const { getDefaultConfig } = require("expo/metro-config");
const path = require("path");
const projectRoot = __dirname;
const monorepoRoot = path.resolve(projectRoot, "../..");
const config = getDefaultConfig(projectRoot);
config.projectRoot = projectRoot;
config.watchFolders = [monorepoRoot];
config.resolver.nodeModulesPaths = [
path.resolve(projectRoot, "node_modules"),
path.resolve(monorepoRoot, "node_modules"),
];
// Stub out Node.js-only packages that gl-react imports unconditionally
// but are not needed (and crash Hermes) in React Native.
const emptyModule = require.resolve("./empty-module");
// Force single copies of react/react-native from this package's node_modules
// to prevent monorepo root copies from being bundled alongside.
const singletonPkgs = ["react", "react-dom", "react-native"];
const originalResolveRequest = config.resolver.resolveRequest;
config.resolver.resolveRequest = (context, moduleName, platform) => {
// With watchFolders = [monorepoRoot], Metro resolves the entry "./index"
// from the monorepo root instead of this package. Redirect to our index.ts.
if (
moduleName === "./index" &&
context.originModulePath === monorepoRoot + "/."
) {
return {
type: "sourceFile",
filePath: path.resolve(projectRoot, "index.ts"),
};
}
if (
moduleName === "webgltexture-loader-ndarray" ||
moduleName === "ndarray" ||
moduleName === "ndarray-ops" ||
moduleName === "typedarray-pool"
) {
return { type: "sourceFile", filePath: emptyModule };
}
// Redirect singleton packages to this package's node_modules
// to prevent the monorepo root's older React from being bundled.
const singletonPkg = singletonPkgs.find(
(pkg) => moduleName === pkg || moduleName.startsWith(pkg + "/")
);
if (singletonPkg) {
const fakeOrigin = path.resolve(projectRoot, "index.ts");
return context.resolveRequest(
{ ...context, originModulePath: fakeOrigin },
moduleName,
platform
);
}
if (originalResolveRequest) {
return originalResolveRequest(context, moduleName, platform);
}
return context.resolveRequest(context, moduleName, platform);
};
module.exports = config;