File size: 2,638 Bytes
f0ccdab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const fs = require("fs");
const path = require("path");
const { getDefaultConfig } = require("expo/metro-config");

const projectRoot = __dirname;
// expo-modules-core is a dependency of expo rather than of the app, so with
// an isolated install it is resolved from expo's own location.
const expoRoot = path.dirname(
  require.resolve("expo/package.json", { paths: [projectRoot] }),
);
const singletonOrigins = {
  react: projectRoot,
  "react-native": projectRoot,
  expo: projectRoot,
  "expo-modules-core": expoRoot,
};

const config = getDefaultConfig(projectRoot);
const defaultResolveRequest = config.resolver.resolveRequest;

// Inside MentraOS this app is a member of the examples/ Bun workspace, which
// links the @mentra packages to the SDK source in mobile/modules and
// cloud-v2/packages and keeps npm packages in examples/node_modules. The SDK
// packages are also members of the root, mobile/, cloud-v2/ and sdk/
// workspaces, so their own dependency links point into whichever of those
// stores was installed last. Metro only serves files inside watched folders,
// so watch all of them.
// In a standalone copy of this example every package is under its node_modules.
const repoRoot = path.resolve(projectRoot, "..", "..", "..");
const sourceFolders = [
  ...["", "examples", "mobile", "cloud-v2", "sdk"].map((workspace) =>
    path.join(repoRoot, workspace, "node_modules"),
  ),
  path.join(repoRoot, "mobile", "modules"),
  path.join(repoRoot, "cloud-v2", "packages"),
];
if (fs.existsSync(path.join(repoRoot, "mobile", "modules", "bluetooth-sdk"))) {
  config.watchFolders = Array.from(
    new Set([
      ...(config.watchFolders || []),
      ...sourceFolders.filter((folder) => fs.existsSync(folder)),
    ]),
  );
}

// Use one copy of React, React Native and Expo: the app's own. Another
// workspace's install can link an SDK package's react or react-native to a
// different copy, so resolve these packages, and their subpaths, from the app
// wherever they are imported.
config.resolver.resolveRequest = (context, moduleName, platform) => {
  const singleton = Object.keys(singletonOrigins).find(
    (name) => moduleName === name || moduleName.startsWith(`${name}/`),
  );
  const resolveContext = singleton
    ? {
        ...context,
        originModulePath: path.join(singletonOrigins[singleton], "package.json"),
      }
    : context;

  return (defaultResolveRequest ?? context.resolveRequest)(
    resolveContext,
    moduleName,
    platform,
  );
};
config.resolver.unstable_enableSymlinks = true;

module.exports = config;