-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
resolver.ts
80 lines (74 loc) · 2.44 KB
/
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
import { dirname, extname, join, resolve } from 'path';
import { resolve as resolveExports } from 'resolve.exports';
import type { ResolverOptions } from 'jest-resolve';
let compilerSetup;
let ts;
function getCompilerSetup(rootDir: string) {
const tsConfigPath =
ts.findConfigFile(rootDir, ts.sys.fileExists, 'tsconfig.spec.json') ||
ts.findConfigFile(rootDir, ts.sys.fileExists, 'tsconfig.test.json') ||
ts.findConfigFile(rootDir, ts.sys.fileExists, 'tsconfig.jest.json');
if (!tsConfigPath) {
console.error(
`Cannot locate a tsconfig.spec.json. Please create one at ${rootDir}/tsconfig.spec.json`
);
}
const readResult = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
const config = ts.parseJsonConfigFileContent(
readResult.config,
ts.sys,
dirname(tsConfigPath)
);
const compilerOptions = config.options;
const host = ts.createCompilerHost(compilerOptions, true);
return { compilerOptions, host };
}
module.exports = function (path: string, options: ResolverOptions) {
const ext = extname(path);
if (ext === '.css' || ext === '.scss' || ext === '.sass' || ext === '.less') {
return require.resolve('identity-obj-proxy');
}
try {
try {
// Try to use the defaultResolver with default options
return options.defaultResolver(path, options);
} catch {
// Try to use the defaultResolver with a packageFilter
return options.defaultResolver(path, {
...options,
packageFilter: (pkg) => ({
...pkg,
main: pkg.main || pkg.es2015 || pkg.module,
}),
pathFilter: (pkg) => {
if (!pkg.exports) {
return path;
}
return resolveExports(pkg, path)?.[0] || path;
},
});
}
} catch (e) {
if (
path === 'jest-sequencer-@jest/test-sequencer' ||
path === '@jest/test-sequencer' ||
path.startsWith('jest-sequencer-')
) {
return;
}
// Fallback to using typescript
ts = ts || require('typescript');
compilerSetup = compilerSetup || getCompilerSetup(options.rootDir);
const { compilerOptions, host } = compilerSetup;
const resolvedFileName = ts.resolveModuleName(
path,
join(options.basedir, 'fake-placeholder.ts'),
compilerOptions,
host
).resolvedModule?.resolvedFileName;
if (!resolvedFileName) {
throw new Error(`Could not resolve ${path}`);
}
return resolve(options.rootDir, resolvedFileName);
}
};