-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
metro-resolver.ts
183 lines (172 loc) · 4.86 KB
/
metro-resolver.ts
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
174
175
176
177
178
179
180
181
182
183
import * as metroResolver from 'metro-resolver';
import type { MatchPath } from 'tsconfig-paths';
import { createMatchPath, loadConfig } from 'tsconfig-paths';
import * as chalk from 'chalk';
import { CachedInputFileSystem, ResolverFactory } from 'enhanced-resolve';
import { dirname, join } from 'path';
import * as fs from 'fs';
import { workspaceRoot } from '@nrwl/devkit';
/*
* Use tsconfig to resolve additional workspace libs.
*
* This resolve function requires projectRoot to be set to
* workspace root in order modules and assets to be registered and watched.
*/
export function getResolveRequest(extensions: string[]) {
return function (
_context: any,
realModuleName: string,
platform: string | null
) {
const DEBUG = process.env.NX_REACT_NATIVE_DEBUG === 'true';
if (DEBUG) console.log(chalk.cyan(`[Nx] Resolving: ${realModuleName}`));
const { resolveRequest, ...context } = _context;
const resolvedPath =
defaultMetroResolver(context, realModuleName, platform, DEBUG) ||
tsconfigPathsResolver(
context,
extensions,
realModuleName,
platform,
DEBUG
) ||
pnpmResolver(extensions, context, realModuleName, DEBUG);
if (resolvedPath) {
return resolvedPath;
}
throw new Error(`Cannot resolve ${chalk.bold(realModuleName)}`);
};
}
/**
* This function try to resolve path using metro's default resolver
* @returns path if resolved, else undefined
*/
function defaultMetroResolver(
context: any,
realModuleName: string,
platform: string,
debug: boolean
) {
try {
return metroResolver.resolve(context, realModuleName, platform);
} catch {
if (debug)
console.log(
chalk.cyan(
`[Nx] Unable to resolve with default Metro resolver: ${realModuleName}`
)
);
}
}
/**
* This resolver try to resolve module for pnpm.
* @returns path if resolved, else undefined
* This pnpm resolver is inspired from https://github.com/vjpr/pnpm-react-native-example/blob/main/packages/pnpm-expo-helper/util/make-resolver.js
*/
function pnpmResolver(
extensions: string[],
context: any,
realModuleName: string,
debug: boolean
) {
try {
const pnpmResolve = getPnpmResolver(extensions);
const lookupStartPath = dirname(context.originModulePath);
const filePath = pnpmResolve.resolveSync(
{},
lookupStartPath,
realModuleName
);
if (filePath) {
return { type: 'sourceFile', filePath };
}
} catch {
if (debug)
console.log(
chalk.cyan(
`[Nx] Unable to resolve with default PNPM resolver: ${realModuleName}`
)
);
}
}
/**
* This function try to resolve files that are specified in tsconfig's paths
* @returns path if resolved, else undefined
*/
function tsconfigPathsResolver(
context: any,
extensions: string[],
realModuleName: string,
platform: string,
debug: boolean
) {
const tsConfigPathMatcher = getMatcher(debug);
const match = tsConfigPathMatcher(
realModuleName,
undefined,
undefined,
extensions.map((ext) => `.${ext}`)
);
if (match) {
return metroResolver.resolve(context, match, platform);
} else {
if (debug) {
console.log(
chalk.red(`[Nx] Failed to resolve ${chalk.bold(realModuleName)}`)
);
console.log(
chalk.cyan(
`[Nx] The following tsconfig paths was used:\n:${chalk.bold(
JSON.stringify(paths, null, 2)
)}`
)
);
}
}
}
let matcher: MatchPath;
let absoluteBaseUrl: string;
let paths: Record<string, string[]>;
function getMatcher(debug: boolean) {
if (!matcher) {
const result = loadConfig();
if (result.resultType === 'success') {
absoluteBaseUrl = result.absoluteBaseUrl;
paths = result.paths;
if (debug) {
console.log(
chalk.cyan(`[Nx] Located tsconfig at ${chalk.bold(absoluteBaseUrl)}`)
);
console.log(
chalk.cyan(
`[Nx] Found the following paths:\n:${chalk.bold(
JSON.stringify(paths, null, 2)
)}`
)
);
}
matcher = createMatchPath(absoluteBaseUrl, paths);
} else {
console.log(chalk.cyan(`[Nx] Failed to locate tsconfig}`));
throw new Error(`Could not load tsconfig for project`);
}
}
return matcher;
}
/**
* This function returns resolver for pnpm.
* It is inspired form https://github.com/vjpr/pnpm-expo-example/blob/main/packages/pnpm-expo-helper/util/make-resolver.js.
*/
let resolver;
function getPnpmResolver(extensions: string[]) {
if (!resolver) {
const fileSystem = new CachedInputFileSystem(fs, 4000);
resolver = ResolverFactory.createResolver({
fileSystem,
extensions: extensions.map((extension) => '.' + extension),
useSyncFileSystemCalls: true,
modules: [join(workspaceRoot, 'node_modules'), 'node_modules'],
});
}
return resolver;
}