-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
defaultResolver.ts
67 lines (59 loc) · 1.59 KB
/
defaultResolver.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import pnpResolver from 'jest-pnp-resolver';
import {Opts as ResolveOpts, sync as resolveSync} from 'resolve';
import type {Config} from '@jest/types';
import {
PkgJson,
isDirectory,
isFile,
readPackageCached,
realpathSync,
} from './fileWalkers';
interface ResolverOptions extends ResolveOpts {
basedir: Config.Path;
browser?: boolean;
conditions?: Array<string>;
defaultResolver: typeof defaultResolver;
extensions?: Array<string>;
rootDir?: Config.Path;
}
// https://github.com/facebook/jest/pull/10617
declare global {
namespace NodeJS {
export interface ProcessVersions {
pnp?: any;
}
}
}
export default function defaultResolver(
path: Config.Path,
options: ResolverOptions,
): Config.Path {
// Yarn 2 adds support to `resolve` automatically so the pnpResolver is only
// needed for Yarn 1 which implements version 1 of the pnp spec
if (process.versions.pnp === '1') {
return pnpResolver(path, options);
}
const result = resolveSync(path, {
...options,
isDirectory,
isFile,
preserveSymlinks: false,
readPackageSync,
realpathSync,
});
// Dereference symlinks to ensure we don't create a separate
// module instance depending on how it was referenced.
return realpathSync(result);
}
/*
* helper functions
*/
function readPackageSync(_: unknown, file: Config.Path): PkgJson {
return readPackageCached(file);
}