From 915bb50802f510adcabc094592730c7c4a679d01 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 11 Feb 2022 15:26:05 +0100 Subject: [PATCH 01/15] fix: correctly resolve remapped directories --- .../conditions/node_modules/exports/package.json | 3 ++- .../exports/some-other-directory/file.js | 0 .../jest-resolve/src/__tests__/resolve.test.ts | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 packages/jest-resolve/src/__mocks__/conditions/node_modules/exports/some-other-directory/file.js diff --git a/packages/jest-resolve/src/__mocks__/conditions/node_modules/exports/package.json b/packages/jest-resolve/src/__mocks__/conditions/node_modules/exports/package.json index 7fd4bab2a661..fc9039934b2c 100644 --- a/packages/jest-resolve/src/__mocks__/conditions/node_modules/exports/package.json +++ b/packages/jest-resolve/src/__mocks__/conditions/node_modules/exports/package.json @@ -11,6 +11,7 @@ "./deeplyNested" : { "require": "./nestedRequire.js", "default": "./nestedDefault.js" - } + }, + "./directory/*": "./some-other-directory/*" } } diff --git a/packages/jest-resolve/src/__mocks__/conditions/node_modules/exports/some-other-directory/file.js b/packages/jest-resolve/src/__mocks__/conditions/node_modules/exports/some-other-directory/file.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index 6c6dfaf0705e..4d0b1af072a6 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -234,6 +234,20 @@ describe('findNodeModule', () => { path.resolve(conditionsRoot, './node_modules/exports/nestedDefault.js'), ); }); + + test('supports separate directory path', () => { + const result = Resolver.findNodeModule('exports/directory/file.js', { + basedir: conditionsRoot, + conditions: [], + }); + + expect(result).toEqual( + path.resolve( + conditionsRoot, + './node_modules/exports/some-other-directory/file.js', + ), + ); + }); }); }); From 1385905bae61771773c92e586c515b5eba384aa9 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 13:47:35 +0100 Subject: [PATCH 02/15] check export map before calling resolve --- packages/jest-resolve/src/defaultResolver.ts | 100 ++++++++++++------- 1 file changed, 65 insertions(+), 35 deletions(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 1ca6204d0b6d..472883f17334 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -5,14 +5,13 @@ * LICENSE file in the root directory of this source tree. */ -import {isAbsolute} from 'path'; +import {dirname, isAbsolute, resolve as pathResolve} from 'path'; import pnpResolver from 'jest-pnp-resolver'; import {sync as resolveSync} from 'resolve'; import { Options as ResolveExportsOptions, resolve as resolveExports, } from 'resolve.exports'; -import slash = require('slash'); import { PkgJson, isDirectory, @@ -45,6 +44,62 @@ declare global { } } +function getPathInModule(path: string, options: ResolverOptions): string { + if (isAbsolute(path) || path.startsWith('.')) { + return path; + } + + const segments = path.split('/'); + + let moduleName = segments.shift(); + + if (moduleName) { + // TODO: handle `#` here: https://github.com/facebook/jest/issues/12270 + if (moduleName.startsWith('@')) { + moduleName = `${moduleName}/${segments.shift()}`; + } + + let packageJsonPath = ''; + + try { + packageJsonPath = resolveSync(`${moduleName}/package.json`, { + ...options, + isDirectory, + isFile, + preserveSymlinks: false, + readPackageSync, + realpathSync, + }); + } catch { + // ignore if package.json cannot be found + } + + if (packageJsonPath && isFile(packageJsonPath)) { + const pkg = readPackageCached(packageJsonPath); + + if (pkg.exports) { + // we need to make sure resolve ignores `main` + delete pkg.main; + + const subpath = segments.join('/') || '.'; + + const resolved = resolveExports( + pkg, + subpath, + createResolveOptions(options.conditions), + ); + + // TODO: should we throw if not? + if (resolved) { + return pathResolve(dirname(packageJsonPath), resolved); + } + } + } + } + + return path; +} + export default function defaultResolver( path: string, options: ResolverOptions, @@ -55,12 +110,13 @@ export default function defaultResolver( return pnpResolver(path, options); } - const result = resolveSync(path, { + const pathToResolve = getPathInModule(path, options); + + const result = resolveSync(pathToResolve, { ...options, isDirectory, isFile, - packageFilter: createPackageFilter(path, options.packageFilter), - pathFilter: createPathFilter(path, options.conditions, options.pathFilter), + packageFilter: createPackageFilter(pathToResolve, options.packageFilter), preserveSymlinks: false, readPackageSync, realpathSync, @@ -107,39 +163,13 @@ function createPackageFilter( }; } -function createPathFilter( - originalPath: string, - conditions?: Array, - userFilter?: ResolverOptions['pathFilter'], -): ResolverOptions['pathFilter'] { - if (shouldIgnoreRequestForExports(originalPath)) { - return userFilter; - } - - const options: ResolveExportsOptions = conditions +function createResolveOptions( + conditions: Array | undefined, +): ResolveExportsOptions { + return conditions ? {conditions, unsafe: true} : // no conditions were passed - let's assume this is Jest internal and it should be `require` {browser: false, require: true}; - - return function pathFilter(pkg, path, relativePath, ...rest) { - let pathToUse = relativePath; - - if (userFilter) { - pathToUse = userFilter(pkg, path, relativePath, ...rest); - } - - if (pkg.exports == null) { - return pathToUse; - } - - // this `index` thing can backfire, but `resolve` adds it: https://github.com/browserify/resolve/blob/f1b51848ecb7f56f77bfb823511d032489a13eab/lib/sync.js#L192 - const isRootRequire = - pathToUse === 'index' && !originalPath.endsWith('/index'); - - const newPath = isRootRequire ? '.' : slash(pathToUse); - - return resolveExports(pkg, newPath, options) || pathToUse; - }; } // if it's a relative import or an absolute path, exports are ignored From 6a61bd3207fc48659121cd550aa29b1a80ac6cff Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 13:54:29 +0100 Subject: [PATCH 03/15] chore: only create options once --- packages/jest-resolve/src/defaultResolver.ts | 31 +++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 472883f17334..d243ad4d0e10 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -7,7 +7,7 @@ import {dirname, isAbsolute, resolve as pathResolve} from 'path'; import pnpResolver from 'jest-pnp-resolver'; -import {sync as resolveSync} from 'resolve'; +import {SyncOpts as UpstreamResolveOptions, sync as resolveSync} from 'resolve'; import { Options as ResolveExportsOptions, resolve as resolveExports, @@ -35,6 +35,9 @@ interface ResolverOptions { pathFilter?: (pkg: PkgJson, path: string, relativePath: string) => string; } +type UpstreamResolveOptionsWithConditions = UpstreamResolveOptions & + Pick; + // https://github.com/facebook/jest/pull/10617 declare global { namespace NodeJS { @@ -44,7 +47,10 @@ declare global { } } -function getPathInModule(path: string, options: ResolverOptions): string { +function getPathInModule( + path: string, + options: UpstreamResolveOptionsWithConditions, +): string { if (isAbsolute(path) || path.startsWith('.')) { return path; } @@ -62,14 +68,7 @@ function getPathInModule(path: string, options: ResolverOptions): string { let packageJsonPath = ''; try { - packageJsonPath = resolveSync(`${moduleName}/package.json`, { - ...options, - isDirectory, - isFile, - preserveSymlinks: false, - readPackageSync, - realpathSync, - }); + packageJsonPath = resolveSync(`${moduleName}/package.json`, options); } catch { // ignore if package.json cannot be found } @@ -110,16 +109,20 @@ export default function defaultResolver( return pnpResolver(path, options); } - const pathToResolve = getPathInModule(path, options); - - const result = resolveSync(pathToResolve, { + const resolveOptions: UpstreamResolveOptionsWithConditions = { ...options, isDirectory, isFile, - packageFilter: createPackageFilter(pathToResolve, options.packageFilter), preserveSymlinks: false, readPackageSync, realpathSync, + }; + + const pathToResolve = getPathInModule(path, options); + + const result = resolveSync(pathToResolve, { + ...resolveOptions, + packageFilter: createPackageFilter(pathToResolve, options.packageFilter), }); // Dereference symlinks to ensure we don't create a separate From fc30eb9373e385305ec81e12a16d8d130e5dbdeb Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 13:55:11 +0100 Subject: [PATCH 04/15] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67d8a7cd9fc5..8b0ea9c4eec4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ - `[jest-environment-node]` [**BREAKING**] Add default `node` and `node-addon` conditions to `exportConditions` for `node` environment ([#11924](https://github.com/facebook/jest/pull/11924)) - `[@jest/expect]` New module which extends `expect` with `jest-snapshot` matchers ([#12404](https://github.com/facebook/jest/pull/12404), [#12410](https://github.com/facebook/jest/pull/12410), [#12418](https://github.com/facebook/jest/pull/12418)) - `[@jest/expect-utils]` New module exporting utils for `expect` ([#12323](https://github.com/facebook/jest/pull/12323)) -- `[jest-resolver]` [**BREAKING**] Add support for `package.json` `exports` ([11961](https://github.com/facebook/jest/pull/11961)) +- `[jest-resolve]` [**BREAKING**] Add support for `package.json` `exports` ([#11961](https://github.com/facebook/jest/pull/11961), [#12373](https://github.com/facebook/jest/pull/12373)) - `[jest-resolve, jest-runtime]` Add support for `data:` URI import and mock ([#12392](https://github.com/facebook/jest/pull/12392)) - `[@jest/schemas]` New module for JSON schemas for Jest's config ([#12384](https://github.com/facebook/jest/pull/12384)) - `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343)) From 6c88c8e2fe08900af4f60e27748d72fd16611581 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 14:01:36 +0100 Subject: [PATCH 05/15] oops --- packages/jest-resolve/src/defaultResolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index d243ad4d0e10..2a7322852947 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -118,7 +118,7 @@ export default function defaultResolver( realpathSync, }; - const pathToResolve = getPathInModule(path, options); + const pathToResolve = getPathInModule(path, resolveOptions); const result = resolveSync(pathToResolve, { ...resolveOptions, From 015efc7a9f0537b376954571178977611f20cba3 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 14:01:52 +0100 Subject: [PATCH 06/15] do not remove main, but set it --- packages/jest-resolve/src/defaultResolver.ts | 28 ++++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 2a7322852947..34963e30c645 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -122,7 +122,7 @@ export default function defaultResolver( const result = resolveSync(pathToResolve, { ...resolveOptions, - packageFilter: createPackageFilter(pathToResolve, options.packageFilter), + packageFilter: createPackageFilter(pathToResolve, options), }); // Dereference symlinks to ensure we don't create a separate @@ -140,28 +140,40 @@ function readPackageSync(_: unknown, file: string): PkgJson { function createPackageFilter( originalPath: string, - userFilter?: ResolverOptions['packageFilter'], + options: ResolverOptions, ): ResolverOptions['packageFilter'] { if (shouldIgnoreRequestForExports(originalPath)) { - return userFilter; + return options.packageFilter; } return function packageFilter(pkg, ...rest) { let filteredPkg = pkg; - if (userFilter) { - filteredPkg = userFilter(filteredPkg, ...rest); + if (options.packageFilter) { + filteredPkg = options.packageFilter(filteredPkg, ...rest); } if (filteredPkg.exports == null) { return filteredPkg; } + let resolvedMain: string | void = undefined; + + try { + resolvedMain = resolveExports( + filteredPkg, + '.', + createResolveOptions(options.conditions), + ); + } catch { + // ignore + } + return { ...filteredPkg, - // remove `main` so `resolve` doesn't look at it and confuse the `.` - // loading in `pathFilter` - main: undefined, + // override `main` so `resolve` resolves it correctly while respecting + // `exports`. + main: resolvedMain, }; }; } From 8b18b88980464c28ec7e95df0219602e8333993e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 14:05:22 +0100 Subject: [PATCH 07/15] skip second resolve call if path is absolute --- packages/jest-resolve/src/defaultResolver.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 34963e30c645..ee53502ece4b 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -120,10 +120,12 @@ export default function defaultResolver( const pathToResolve = getPathInModule(path, resolveOptions); - const result = resolveSync(pathToResolve, { - ...resolveOptions, - packageFilter: createPackageFilter(pathToResolve, options), - }); + const result = isAbsolute(pathToResolve) + ? pathToResolve + : resolveSync(pathToResolve, { + ...resolveOptions, + packageFilter: createPackageFilter(pathToResolve, options), + }); // Dereference symlinks to ensure we don't create a separate // module instance depending on how it was referenced. From 6eec12a7140377be35ab37da83b8ec2bbc04d15e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 14:12:14 +0100 Subject: [PATCH 08/15] tweak weird test --- .../__tests__/runtimeInternalModuleRegistry.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js b/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js index 23d7ba831933..53334daccdb5 100644 --- a/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js +++ b/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js @@ -13,10 +13,10 @@ describe('Runtime internal module registry', () => { it('behaves correctly when requiring a module that is used by jest internals', () => { const fs = require('fs'); - // We require from this crazy path so that we can mimick Jest (and it's - // transitive deps) being installed along side a projects deps (e.g. with an + // We require from this crazy path so that we can mimick Jest (and its + // transitive deps) being installed alongside a projects deps (e.g. with an // NPM3 flat dep tree) - const jestUtil = require('../../../packages/jest-util'); + const jestUtil = require('jest-util'); // If FS is mocked correctly, this folder won't actually be created on the // filesystem From 0f23f26917e167f6716d28fc38c5f64eade834b5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 14:21:16 +0100 Subject: [PATCH 09/15] correct logic --- packages/jest-resolve/src/defaultResolver.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index ee53502ece4b..c289c4b2fd9d 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -120,12 +120,13 @@ export default function defaultResolver( const pathToResolve = getPathInModule(path, resolveOptions); - const result = isAbsolute(pathToResolve) - ? pathToResolve - : resolveSync(pathToResolve, { - ...resolveOptions, - packageFilter: createPackageFilter(pathToResolve, options), - }); + const result = + pathToResolve === path + ? resolveSync(pathToResolve, { + ...resolveOptions, + packageFilter: createPackageFilter(pathToResolve, options), + }) + : pathToResolve; // Dereference symlinks to ensure we don't create a separate // module instance depending on how it was referenced. From a38e5c6fd3b5bb81b2c11774b6edd00ff127b2b4 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 14:22:11 +0100 Subject: [PATCH 10/15] get rid of package filter --- packages/jest-resolve/src/defaultResolver.ts | 47 +------------------- 1 file changed, 2 insertions(+), 45 deletions(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index c289c4b2fd9d..6c9a68e36f3b 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -51,7 +51,7 @@ function getPathInModule( path: string, options: UpstreamResolveOptionsWithConditions, ): string { - if (isAbsolute(path) || path.startsWith('.')) { + if (shouldIgnoreRequestForExports(path)) { return path; } @@ -122,10 +122,7 @@ export default function defaultResolver( const result = pathToResolve === path - ? resolveSync(pathToResolve, { - ...resolveOptions, - packageFilter: createPackageFilter(pathToResolve, options), - }) + ? resolveSync(pathToResolve, resolveOptions) : pathToResolve; // Dereference symlinks to ensure we don't create a separate @@ -141,46 +138,6 @@ function readPackageSync(_: unknown, file: string): PkgJson { return readPackageCached(file); } -function createPackageFilter( - originalPath: string, - options: ResolverOptions, -): ResolverOptions['packageFilter'] { - if (shouldIgnoreRequestForExports(originalPath)) { - return options.packageFilter; - } - - return function packageFilter(pkg, ...rest) { - let filteredPkg = pkg; - - if (options.packageFilter) { - filteredPkg = options.packageFilter(filteredPkg, ...rest); - } - - if (filteredPkg.exports == null) { - return filteredPkg; - } - - let resolvedMain: string | void = undefined; - - try { - resolvedMain = resolveExports( - filteredPkg, - '.', - createResolveOptions(options.conditions), - ); - } catch { - // ignore - } - - return { - ...filteredPkg, - // override `main` so `resolve` resolves it correctly while respecting - // `exports`. - main: resolvedMain, - }; - }; -} - function createResolveOptions( conditions: Array | undefined, ): ResolveExportsOptions { From e8cf6d4c073beb09bc11929a4af6f336ca6e4da1 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 14:23:23 +0100 Subject: [PATCH 11/15] rollback test change --- .../__tests__/runtimeInternalModuleRegistry.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js b/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js index 53334daccdb5..377764a2020e 100644 --- a/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js +++ b/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js @@ -16,7 +16,7 @@ describe('Runtime internal module registry', () => { // We require from this crazy path so that we can mimick Jest (and its // transitive deps) being installed alongside a projects deps (e.g. with an // NPM3 flat dep tree) - const jestUtil = require('jest-util'); + const jestUtil = require('../../../packages/jest-util'); // If FS is mocked correctly, this folder won't actually be created on the // filesystem From b18f819fa02e8438338aadd339d3aec82fa97ad0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 14:24:30 +0100 Subject: [PATCH 12/15] Revert "rollback test change" This reverts commit e8cf6d4c073beb09bc11929a4af6f336ca6e4da1. --- .../__tests__/runtimeInternalModuleRegistry.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js b/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js index 377764a2020e..53334daccdb5 100644 --- a/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js +++ b/e2e/runtime-internal-module-registry/__tests__/runtimeInternalModuleRegistry.test.js @@ -16,7 +16,7 @@ describe('Runtime internal module registry', () => { // We require from this crazy path so that we can mimick Jest (and its // transitive deps) being installed alongside a projects deps (e.g. with an // NPM3 flat dep tree) - const jestUtil = require('../../../packages/jest-util'); + const jestUtil = require('jest-util'); // If FS is mocked correctly, this folder won't actually be created on the // filesystem From 4bc622d01775d201e11847a11a3eff7e95446d42 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 14:43:01 +0100 Subject: [PATCH 13/15] ?? --- packages/jest-worker/src/__tests__/leak-integration.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-worker/src/__tests__/leak-integration.test.ts b/packages/jest-worker/src/__tests__/leak-integration.test.ts index 3acfa58be8df..f8e440e2d904 100644 --- a/packages/jest-worker/src/__tests__/leak-integration.test.ts +++ b/packages/jest-worker/src/__tests__/leak-integration.test.ts @@ -9,7 +9,7 @@ import {tmpdir} from 'os'; import {join} from 'path'; import {writeFileSync} from 'graceful-fs'; import LeakDetector from 'jest-leak-detector'; -import {Worker} from '../..'; +import {Worker} from '../../build/index'; let workerFile!: string; beforeAll(() => { @@ -30,10 +30,10 @@ afterEach(async () => { it('does not retain arguments after a task finished', async () => { let leakDetector!: LeakDetector; - await new Promise(resolve => { + await new Promise((resolve, reject) => { const obj = {}; leakDetector = new LeakDetector(obj); - (worker as any).fn(obj).then(resolve); + (worker as any).fn(obj).then(resolve, reject); }); expect(await leakDetector.isLeaking()).toBe(false); From 2890f2515b9dc3450873c4af5b87b7da6efb56d9 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 15:04:46 +0100 Subject: [PATCH 14/15] move new function --- packages/jest-resolve/src/defaultResolver.ts | 78 ++++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 6c9a68e36f3b..07a6243b22d1 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -47,6 +47,45 @@ declare global { } } +export default function defaultResolver( + path: string, + options: ResolverOptions, +): string { + // 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 resolveOptions: UpstreamResolveOptionsWithConditions = { + ...options, + isDirectory, + isFile, + preserveSymlinks: false, + readPackageSync, + realpathSync, + }; + + const pathToResolve = getPathInModule(path, resolveOptions); + + const result = + pathToResolve === path + ? resolveSync(pathToResolve, resolveOptions) + : pathToResolve; + + // 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: string): PkgJson { + return readPackageCached(file); +} + function getPathInModule( path: string, options: UpstreamResolveOptionsWithConditions, @@ -99,45 +138,6 @@ function getPathInModule( return path; } -export default function defaultResolver( - path: string, - options: ResolverOptions, -): string { - // 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 resolveOptions: UpstreamResolveOptionsWithConditions = { - ...options, - isDirectory, - isFile, - preserveSymlinks: false, - readPackageSync, - realpathSync, - }; - - const pathToResolve = getPathInModule(path, resolveOptions); - - const result = - pathToResolve === path - ? resolveSync(pathToResolve, resolveOptions) - : pathToResolve; - - // 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: string): PkgJson { - return readPackageCached(file); -} - function createResolveOptions( conditions: Array | undefined, ): ResolveExportsOptions { From b2f3d59be5e785dbe26a88ca90ca09d1aa4f4c80 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 17 Feb 2022 15:06:10 +0100 Subject: [PATCH 15/15] comment --- packages/jest-resolve/src/defaultResolver.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 07a6243b22d1..893a18b1b3bf 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -69,6 +69,7 @@ export default function defaultResolver( const pathToResolve = getPathInModule(path, resolveOptions); const result = + // if `getPathInModule` doesn't change the path, attempt to resolve it pathToResolve === path ? resolveSync(pathToResolve, resolveOptions) : pathToResolve;