Repository navigation
Expand file tree
/
Copy pathmetro.config.js
More file actions
110 lines (95 loc) · 3.47 KB
/
Copy pathmetro.config.js
File metadata and controls
110 lines (95 loc) · 3.47 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
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
const path = require("node:path");
const fs = require("node:fs");
const { getDefaultConfig } = require("expo/metro-config");
const { withNativewind } = require("nativewind/metro");
const projectRoot = __dirname;
const workspaceRoot = path.resolve(projectRoot, "../..");
const config = getDefaultConfig(projectRoot);
config.watchFolders = [workspaceRoot];
config.resolver.nodeModulesPaths = [
path.resolve(projectRoot, "node_modules"),
path.resolve(workspaceRoot, "node_modules"),
];
config.resolver.unstable_enablePackageExports = true;
const WORKSPACE_SCOPES = ["@bb/", "@get-bb/"];
const TS_EXTENSIONS = [".ts", ".tsx"];
const workspaceSourceRoots = ["packages", "apps", "plugins"].map((dir) =>
path.join(workspaceRoot, dir),
);
function isWorkspaceSource(filePath) {
return workspaceSourceRoots.some(
(root) =>
filePath.startsWith(root + path.sep) &&
!filePath.includes(`${path.sep}node_modules${path.sep}`),
);
}
function fileWithTsExtension(base) {
for (const ext of TS_EXTENSIONS) {
if (fs.existsSync(base + ext)) return base + ext;
}
for (const ext of TS_EXTENSIONS) {
const indexPath = path.join(base, `index${ext}`);
if (fs.existsSync(indexPath)) return indexPath;
}
return null;
}
function splitScopedSpecifier(moduleName) {
const parts = moduleName.split("/");
const packageName = parts.slice(0, 2).join("/");
const subpath = parts.length > 2 ? "./" + parts.slice(2).join("/") : ".";
return { packageName, subpath };
}
const workspacePackageDirCache = new Map();
function findWorkspacePackageDir(packageName) {
if (workspacePackageDirCache.has(packageName)) {
return workspacePackageDirCache.get(packageName);
}
let found = null;
for (const nodeModules of config.resolver.nodeModulesPaths) {
const candidate = path.join(nodeModules, packageName);
if (fs.existsSync(path.join(candidate, "package.json"))) {
found = fs.realpathSync(candidate);
break;
}
}
workspacePackageDirCache.set(packageName, found);
return found;
}
function resolveWorkspaceSource(moduleName) {
const { packageName, subpath } = splitScopedSpecifier(moduleName);
const packageDir = findWorkspacePackageDir(packageName);
if (!packageDir || !isWorkspaceSource(packageDir + path.sep)) return null;
const packageJson = JSON.parse(
fs.readFileSync(path.join(packageDir, "package.json"), "utf8"),
);
const entry = packageJson.exports?.[subpath];
if (!entry) return null;
const source =
typeof entry === "string" ? entry : (entry.source ?? entry.default);
if (typeof source !== "string") return null;
return path.resolve(packageDir, source);
}
const defaultResolveRequest = config.resolver.resolveRequest;
config.resolver.resolveRequest = (context, moduleName, platform) => {
const resolve = defaultResolveRequest ?? context.resolveRequest;
if (WORKSPACE_SCOPES.some((scope) => moduleName.startsWith(scope))) {
const filePath = resolveWorkspaceSource(moduleName);
if (filePath && fs.existsSync(filePath)) {
return { type: "sourceFile", filePath };
}
}
if (
moduleName.startsWith(".") &&
moduleName.endsWith(".js") &&
isWorkspaceSource(context.originModulePath)
) {
const base = path.resolve(
path.dirname(context.originModulePath),
moduleName.slice(0, -3),
);
const filePath = fileWithTsExtension(base);
if (filePath) return { type: "sourceFile", filePath };
}
return resolve(context, moduleName, platform);
};
module.exports = withNativewind(config, { inlineRem: 16 });