Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update resolve #9872

Merged
merged 4 commits into from Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,8 @@

### Performance

- `[jest-resolve]` Update `resolve` to a version using native `realpath`, which is faster than the default JS implementation ([#9872](https://github.com/facebook/jest/pull/9872))

## 25.4.0

- `[expect]` Support `async function`s in `toThrow` ([#9817](https://github.com/facebook/jest/pull/9817))
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-resolve/package.json
Expand Up @@ -23,7 +23,7 @@
"jest-pnp-resolver": "^1.2.1",
"read-pkg-up": "^7.0.1",
"realpath-native": "^2.0.0",
"resolve": "^1.16.1",
"resolve": "^1.17.0",
"slash": "^3.0.0"
},
"devDependencies": {
Expand Down
39 changes: 26 additions & 13 deletions packages/jest-resolve/src/defaultResolver.ts
Expand Up @@ -41,21 +41,13 @@ export default function defaultResolver(
moduleDirectory: options.moduleDirectory,
paths: options.paths,
preserveSymlinks: false,
// @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/44137
realpathSync,
});

try {
// Dereference symlinks to ensure we don't create a separate
// module instance depending on how it was referenced.
const resolved = realpath(result);

if (resolved) {
return resolved;
}
} catch {
// ignore
}

return result;
// Dereference symlinks to ensure we don't create a separate
// module instance depending on how it was referenced.
return realpathSync(result);
}

export function clearDefaultResolverCache(): void {
Expand Down Expand Up @@ -97,6 +89,23 @@ function statSyncCached(path: string): IPathType {
return IPathType.OTHER;
}

function tolerantRealpath(path: Config.Path): Config.Path {
let result: Config.Path | undefined = undefined;
try {
result = realpath(path);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

if (!result) {
result = path;
}

return result;
}

/*
* helper functions
*/
Expand All @@ -107,3 +116,7 @@ function isFile(file: Config.Path): boolean {
function isDirectory(dir: Config.Path): boolean {
return statSyncCached(dir) === IPathType.DIRECTORY;
}

function realpathSync(file: Config.Path): Config.Path {
return tolerantRealpath(file);
}
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -12708,10 +12708,10 @@ resolve@1.1.7:
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs=

resolve@^1.1.6, resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.15.0, resolve@^1.15.1, resolve@^1.16.1, resolve@^1.3.2, resolve@^1.5.0, resolve@^1.8.1:
version "1.16.1"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.16.1.tgz#49fac5d8bacf1fd53f200fa51247ae736175832c"
integrity sha512-rmAglCSqWWMrrBv/XM6sW0NuRFiKViw/W4d9EbC4pt+49H8JwHy+mcGmALTEg504AUDcLTvb1T2q3E9AnmY+ig==
resolve@^1.1.6, resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.15.0, resolve@^1.15.1, resolve@^1.17.0, resolve@^1.3.2, resolve@^1.5.0, resolve@^1.8.1:
version "1.17.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.17.0.tgz#b25941b54968231cc2d1bb76a79cb7f2c0bf8444"
integrity sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==
dependencies:
path-parse "^1.0.6"

Expand Down