/
metro-resolver.ts
192 lines (178 loc) · 5.13 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
184
185
186
187
188
189
190
191
192
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 { appRootPath } from 'nx/src/utils/app-root';
/*
* 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,
moduleName: string
) {
const DEBUG = process.env.NX_REACT_NATIVE_DEBUG === 'true';
if (DEBUG) console.log(chalk.cyan(`[Nx] Resolving: ${moduleName}`));
const { resolveRequest, ...context } = _context;
let resolvedPath = defaultMetroResolver(context, moduleName, platform);
if (resolvedPath) {
return resolvedPath;
}
resolvedPath = pnpmResolver(
extensions,
context,
realModuleName,
moduleName
);
if (resolvedPath) {
return resolvedPath;
}
return tsconfigPathsResolver(
context,
extensions,
realModuleName,
moduleName,
platform
);
};
}
/**
* This function try to resolve path using metro's default resolver
* @returns path if resolved, else undefined
*/
function defaultMetroResolver(
context: any,
moduleName: string,
platform: string
) {
const DEBUG = process.env.NX_REACT_NATIVE_DEBUG === 'true';
try {
return metroResolver.resolve(context, moduleName, platform);
} catch {
if (DEBUG)
console.log(
chalk.cyan(
`[Nx] Unable to resolve with default Metro resolver: ${moduleName}`
)
);
}
}
/**
* 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, context, realModuleName, moduleName) {
const DEBUG = process.env.NX_REACT_NATIVE_DEBUG === 'true';
try {
const pnpmResolver = getPnpmResolver(appRootPath, extensions);
const lookupStartPath = dirname(context.originModulePath);
const filePath = pnpmResolver.resolveSync(
{},
lookupStartPath,
realModuleName
);
if (filePath) {
return { type: 'sourceFile', filePath };
}
} catch {
if (DEBUG)
console.log(
chalk.cyan(
`[Nx] Unable to resolve with default PNPM resolver: ${moduleName}`
)
);
}
}
/**
* 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,
moduleName: string,
platform: string
) {
const DEBUG = process.env.NX_REACT_NATIVE_DEBUG === 'true';
const matcher = getMatcher();
const match = matcher(
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(moduleName)}`)
);
console.log(
chalk.cyan(
`[Nx] The following tsconfig paths was used:\n:${chalk.bold(
JSON.stringify(paths, null, 2)
)}`
)
);
}
throw new Error(`Cannot resolve ${chalk.bold(moduleName)}`);
}
}
let matcher: MatchPath;
let absoluteBaseUrl: string;
let paths: Record<string, string[]>;
function getMatcher() {
const DEBUG = process.env.NX_REACT_NATIVE_DEBUG === 'true';
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(appRootPath: string, extensions: string[]) {
if (!resolver) {
const fileSystem = new CachedInputFileSystem(fs, 4000);
resolver = ResolverFactory.createResolver({
fileSystem,
extensions: extensions.map((extension) => '.' + extension),
useSyncFileSystemCalls: true,
modules: [join(appRootPath, 'node_modules'), 'node_modules'],
});
}
return resolver;
}