From a982c2cac79bf22aa8e70446a749c91799f08112 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 1 Oct 2021 13:55:37 +0200 Subject: [PATCH 1/7] feat: support `.` in `exports` field --- CHANGELOG.md | 1 + e2e/__tests__/resolveConditions.test.ts | 5 -- .../__tests__/browser.test.mjs | 2 +- .../__tests__/node.test.mjs | 2 +- .../__tests__/resolveCjs.test.cjs | 2 +- .../__tests__/resolveEsm.test.mjs | 2 +- .../{ => node_modules}/fake-dep/module.cjs | 0 .../{ => node_modules}/fake-dep/module.mjs | 0 .../{ => node_modules}/fake-dep/package.json | 0 .../fake-dual-dep/browser.mjs | 0 .../{ => node_modules}/fake-dual-dep/node.mjs | 0 .../fake-dual-dep/package.json | 0 e2e/resolve-conditions/package.json | 4 -- e2e/resolve-conditions/resolver.js | 30 ----------- e2e/resolve-conditions/yarn.lock | 21 -------- packages/jest-resolve/package.json | 1 + .../conditions/node_modules/import/file.js | 0 .../node_modules/import/package.json | 6 +++ .../conditions/node_modules/main/file.js | 0 .../conditions/node_modules/main/package.json | 4 ++ .../conditions/node_modules/require/file.js | 0 .../node_modules/require/package.json | 6 +++ .../jest-resolve/src/__mocks__/package.json | 6 +++ .../src/__tests__/resolve.test.ts | 51 ++++++++++++++++--- packages/jest-resolve/src/defaultResolver.ts | 39 ++++++++++++++ yarn.lock | 8 +++ 26 files changed, 118 insertions(+), 72 deletions(-) rename e2e/resolve-conditions/{ => node_modules}/fake-dep/module.cjs (100%) rename e2e/resolve-conditions/{ => node_modules}/fake-dep/module.mjs (100%) rename e2e/resolve-conditions/{ => node_modules}/fake-dep/package.json (100%) rename e2e/resolve-conditions/{ => node_modules}/fake-dual-dep/browser.mjs (100%) rename e2e/resolve-conditions/{ => node_modules}/fake-dual-dep/node.mjs (100%) rename e2e/resolve-conditions/{ => node_modules}/fake-dual-dep/package.json (100%) delete mode 100644 e2e/resolve-conditions/resolver.js delete mode 100644 e2e/resolve-conditions/yarn.lock create mode 100644 packages/jest-resolve/src/__mocks__/conditions/node_modules/import/file.js create mode 100644 packages/jest-resolve/src/__mocks__/conditions/node_modules/import/package.json create mode 100644 packages/jest-resolve/src/__mocks__/conditions/node_modules/main/file.js create mode 100644 packages/jest-resolve/src/__mocks__/conditions/node_modules/main/package.json create mode 100644 packages/jest-resolve/src/__mocks__/conditions/node_modules/require/file.js create mode 100644 packages/jest-resolve/src/__mocks__/conditions/node_modules/require/package.json create mode 100644 packages/jest-resolve/src/__mocks__/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 48c812a51672..5356c4492d29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - `[jest-config]` Add `testEnvironmentOptions.html` to apply to jsdom input ([11950](https://github.com/facebook/jest/pull/11950)) +- `[jest-resolver]` Support default export (`.`) in `exports` field _if_ `main` is missing ([#11919](https://github.com/facebook/jest/pull/11919)) ### Fixes diff --git a/e2e/__tests__/resolveConditions.test.ts b/e2e/__tests__/resolveConditions.test.ts index 375257ac7640..991a2b27e8b1 100644 --- a/e2e/__tests__/resolveConditions.test.ts +++ b/e2e/__tests__/resolveConditions.test.ts @@ -7,15 +7,10 @@ import {resolve} from 'path'; import {onNodeVersions} from '@jest/test-utils'; -import {runYarnInstall} from '../Utils'; import runJest from '../runJest'; const dir = resolve(__dirname, '..', 'resolve-conditions'); -beforeAll(() => { - runYarnInstall(dir); -}); - // The versions where vm.Module exists and commonjs with "exports" is not broken onNodeVersions('>=12.16.0', () => { test('resolves package exports correctly with custom resolver', () => { diff --git a/e2e/resolve-conditions/__tests__/browser.test.mjs b/e2e/resolve-conditions/__tests__/browser.test.mjs index 12b397aa5afc..4edfaa7ffdaa 100644 --- a/e2e/resolve-conditions/__tests__/browser.test.mjs +++ b/e2e/resolve-conditions/__tests__/browser.test.mjs @@ -7,7 +7,7 @@ * @jest-environment /browser-env.js */ -import {fn} from '../fake-dual-dep'; +import {fn} from 'fake-dual-dep'; test('returns correct message', () => { expect(fn()).toEqual('hello from browser'); diff --git a/e2e/resolve-conditions/__tests__/node.test.mjs b/e2e/resolve-conditions/__tests__/node.test.mjs index fb86c2a8eeac..f71ed5a69e2e 100644 --- a/e2e/resolve-conditions/__tests__/node.test.mjs +++ b/e2e/resolve-conditions/__tests__/node.test.mjs @@ -7,7 +7,7 @@ * @jest-environment /node-env.js */ -import {fn} from '../fake-dual-dep'; +import {fn} from 'fake-dual-dep'; test('returns correct message', () => { expect(fn()).toEqual('hello from node'); diff --git a/e2e/resolve-conditions/__tests__/resolveCjs.test.cjs b/e2e/resolve-conditions/__tests__/resolveCjs.test.cjs index 5a9ba6b23b88..43409fed5939 100644 --- a/e2e/resolve-conditions/__tests__/resolveCjs.test.cjs +++ b/e2e/resolve-conditions/__tests__/resolveCjs.test.cjs @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -const {fn} = require('../fake-dep'); +const {fn} = require('fake-dep'); test('returns correct message', () => { expect(fn()).toEqual('hello from CJS'); diff --git a/e2e/resolve-conditions/__tests__/resolveEsm.test.mjs b/e2e/resolve-conditions/__tests__/resolveEsm.test.mjs index d46c700f2cf2..2303cef9e418 100644 --- a/e2e/resolve-conditions/__tests__/resolveEsm.test.mjs +++ b/e2e/resolve-conditions/__tests__/resolveEsm.test.mjs @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {fn} from '../fake-dep'; +import {fn} from 'fake-dep'; test('returns correct message', () => { expect(fn()).toEqual('hello from ESM'); diff --git a/e2e/resolve-conditions/fake-dep/module.cjs b/e2e/resolve-conditions/node_modules/fake-dep/module.cjs similarity index 100% rename from e2e/resolve-conditions/fake-dep/module.cjs rename to e2e/resolve-conditions/node_modules/fake-dep/module.cjs diff --git a/e2e/resolve-conditions/fake-dep/module.mjs b/e2e/resolve-conditions/node_modules/fake-dep/module.mjs similarity index 100% rename from e2e/resolve-conditions/fake-dep/module.mjs rename to e2e/resolve-conditions/node_modules/fake-dep/module.mjs diff --git a/e2e/resolve-conditions/fake-dep/package.json b/e2e/resolve-conditions/node_modules/fake-dep/package.json similarity index 100% rename from e2e/resolve-conditions/fake-dep/package.json rename to e2e/resolve-conditions/node_modules/fake-dep/package.json diff --git a/e2e/resolve-conditions/fake-dual-dep/browser.mjs b/e2e/resolve-conditions/node_modules/fake-dual-dep/browser.mjs similarity index 100% rename from e2e/resolve-conditions/fake-dual-dep/browser.mjs rename to e2e/resolve-conditions/node_modules/fake-dual-dep/browser.mjs diff --git a/e2e/resolve-conditions/fake-dual-dep/node.mjs b/e2e/resolve-conditions/node_modules/fake-dual-dep/node.mjs similarity index 100% rename from e2e/resolve-conditions/fake-dual-dep/node.mjs rename to e2e/resolve-conditions/node_modules/fake-dual-dep/node.mjs diff --git a/e2e/resolve-conditions/fake-dual-dep/package.json b/e2e/resolve-conditions/node_modules/fake-dual-dep/package.json similarity index 100% rename from e2e/resolve-conditions/fake-dual-dep/package.json rename to e2e/resolve-conditions/node_modules/fake-dual-dep/package.json diff --git a/e2e/resolve-conditions/package.json b/e2e/resolve-conditions/package.json index 33449adf2da3..6bfa1448215b 100644 --- a/e2e/resolve-conditions/package.json +++ b/e2e/resolve-conditions/package.json @@ -6,13 +6,9 @@ "mjs", "json" ], - "resolver": "/resolver.js", "testMatch": [ "/**/*.test.*" ], "transform": {} - }, - "dependencies": { - "resolve.exports": "^1.1.0" } } diff --git a/e2e/resolve-conditions/resolver.js b/e2e/resolve-conditions/resolver.js deleted file mode 100644 index 09cee4fb91a3..000000000000 --- a/e2e/resolve-conditions/resolver.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * 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. - */ - -'use strict'; - -const {resolve: resolveExports} = require('resolve.exports'); - -module.exports = (path, options) => { - return options.defaultResolver(path, { - ...options, - pathFilter: options.conditions - ? createPathFilter(options.conditions) - : undefined, - }); -}; - -function createPathFilter(conditions) { - return function pathFilter(pkg, _path, relativePath) { - // this `index` thing can backfire, but `resolve` adds it: https://github.com/browserify/resolve/blob/f1b51848ecb7f56f77bfb823511d032489a13eab/lib/sync.js#L192 - const path = relativePath === 'index' ? '.' : relativePath; - - return ( - resolveExports(pkg, path, {conditions, unsafe: true}) || relativePath - ); - }; -} diff --git a/e2e/resolve-conditions/yarn.lock b/e2e/resolve-conditions/yarn.lock deleted file mode 100644 index df2f59b137b1..000000000000 --- a/e2e/resolve-conditions/yarn.lock +++ /dev/null @@ -1,21 +0,0 @@ -# This file is generated by running "yarn install" inside your project. -# Manual changes might be lost - proceed with caution! - -__metadata: - version: 4 - cacheKey: 7 - -"resolve.exports@npm:^1.1.0": - version: 1.1.0 - resolution: "resolve.exports@npm:1.1.0" - checksum: d04d2ce651fac14fe6ba13b377690f790cbbe91e6211b8fbec97ee08282e278875c74073a9b6243143a64e33d95eefb479e1dd4965664edc73b28b712100b36c - languageName: node - linkType: hard - -"root-workspace-0b6124@workspace:.": - version: 0.0.0-use.local - resolution: "root-workspace-0b6124@workspace:." - dependencies: - resolve.exports: ^1.1.0 - languageName: unknown - linkType: soft diff --git a/packages/jest-resolve/package.json b/packages/jest-resolve/package.json index 5dbb38901a16..4377e7fe2500 100644 --- a/packages/jest-resolve/package.json +++ b/packages/jest-resolve/package.json @@ -23,6 +23,7 @@ "jest-util": "^27.2.5", "jest-validate": "^27.2.5", "resolve": "^1.20.0", + "resolve.exports": "^1.1.0", "slash": "^3.0.0" }, "devDependencies": { diff --git a/packages/jest-resolve/src/__mocks__/conditions/node_modules/import/file.js b/packages/jest-resolve/src/__mocks__/conditions/node_modules/import/file.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/jest-resolve/src/__mocks__/conditions/node_modules/import/package.json b/packages/jest-resolve/src/__mocks__/conditions/node_modules/import/package.json new file mode 100644 index 000000000000..24fc72b1cac7 --- /dev/null +++ b/packages/jest-resolve/src/__mocks__/conditions/node_modules/import/package.json @@ -0,0 +1,6 @@ +{ + "name": "import", + "exports": { + "import": "./file.js" + } +} diff --git a/packages/jest-resolve/src/__mocks__/conditions/node_modules/main/file.js b/packages/jest-resolve/src/__mocks__/conditions/node_modules/main/file.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/jest-resolve/src/__mocks__/conditions/node_modules/main/package.json b/packages/jest-resolve/src/__mocks__/conditions/node_modules/main/package.json new file mode 100644 index 000000000000..18b48565f56a --- /dev/null +++ b/packages/jest-resolve/src/__mocks__/conditions/node_modules/main/package.json @@ -0,0 +1,4 @@ +{ + "name": "main", + "main": "./file.js" +} diff --git a/packages/jest-resolve/src/__mocks__/conditions/node_modules/require/file.js b/packages/jest-resolve/src/__mocks__/conditions/node_modules/require/file.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/jest-resolve/src/__mocks__/conditions/node_modules/require/package.json b/packages/jest-resolve/src/__mocks__/conditions/node_modules/require/package.json new file mode 100644 index 000000000000..c42b33ecca86 --- /dev/null +++ b/packages/jest-resolve/src/__mocks__/conditions/node_modules/require/package.json @@ -0,0 +1,6 @@ +{ + "name": "require", + "exports": { + "require": "./file.js" + } +} diff --git a/packages/jest-resolve/src/__mocks__/package.json b/packages/jest-resolve/src/__mocks__/package.json new file mode 100644 index 000000000000..2807645e6dca --- /dev/null +++ b/packages/jest-resolve/src/__mocks__/package.json @@ -0,0 +1,6 @@ +{ + "name": "__mocks__", + "version": "1.0.0", + "dependencies": { + } +} diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index cfbe99bc841d..65b89a77b63d 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -126,7 +126,7 @@ describe('findNodeModule', () => { }); }); - it('passes packageFilter to the resolve module when using the default resolver', () => { + it('wraps passed packageFilter to the resolve module when using the default resolver', () => { const packageFilter = jest.fn(); // A resolver that delegates to defaultResolver with a packageFilter implementation @@ -134,18 +134,53 @@ describe('findNodeModule', () => { opts.defaultResolver(request, {...opts, packageFilter}), ); - Resolver.findNodeModule('test', { - basedir: '/', + Resolver.findNodeModule('./test', { + basedir: path.resolve(__dirname, '../__mocks__/'), resolver: require.resolve('../__mocks__/userResolver'), }); - expect(mockResolveSync).toHaveBeenCalledWith( - 'test', - expect.objectContaining({ - packageFilter, - }), + expect(packageFilter).toHaveBeenCalledWith( + expect.objectContaining({name: '__mocks__'}), + expect.any(String), ); }); + + describe('conditions', () => { + const conditionsRoot = path.resolve(__dirname, '../__mocks__/conditions'); + + test('resolves without exports, just main', () => { + const result = Resolver.findNodeModule('main', { + basedir: conditionsRoot, + conditions: ['require'], + }); + + expect(result).toEqual( + path.resolve(conditionsRoot, './node_modules/main/file.js'), + ); + }); + + test('resolves with import', () => { + const result = Resolver.findNodeModule('import', { + basedir: conditionsRoot, + conditions: ['import'], + }); + + expect(result).toEqual( + path.resolve(conditionsRoot, './node_modules/import/file.js'), + ); + }); + + test('resolves with require', () => { + const result = Resolver.findNodeModule('require', { + basedir: conditionsRoot, + conditions: ['require'], + }); + + expect(result).toEqual( + path.resolve(conditionsRoot, './node_modules/require/file.js'), + ); + }); + }); }); describe('resolveModule', () => { diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 0100b5e6236f..c34aa8f5dc10 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -8,6 +8,10 @@ import * as fs from 'graceful-fs'; import pnpResolver from 'jest-pnp-resolver'; import {Opts as ResolveOpts, sync as resolveSync} from 'resolve'; +import { + Options as ResolveExportsOptions, + resolve as resolveExports, +} from 'resolve.exports'; import type {Config} from '@jest/types'; import {tryRealpath} from 'jest-util'; @@ -43,6 +47,10 @@ export default function defaultResolver( ...options, isDirectory, isFile, + packageFilter: createPackageFilter( + options.conditions, + options.packageFilter, + ), preserveSymlinks: false, readPackageSync, realpathSync, @@ -149,3 +157,34 @@ function realpathSync(file: Config.Path): Config.Path { function readPackageSync(_: unknown, file: Config.Path): PkgJson { return readPackageCached(file); } + +function createPackageFilter( + conditions?: Array, + userFilter?: ResolverOptions['packageFilter'], +): ResolverOptions['packageFilter'] { + function attemptExportsFallback(pkg: PkgJson) { + const options: ResolveExportsOptions = conditions + ? {conditions, unsafe: true} + : // no conditions were passed - let's assume this is Jest internal and it should be `require` + {browser: false, require: true}; + + try { + return resolveExports(pkg, '.', options); + } catch { + return undefined; + } + } + + return function packageFilter(pkg: PkgJson, ...rest) { + let filteredPkg = pkg; + + if (userFilter) { + filteredPkg = userFilter(filteredPkg, ...rest); + } + + return { + ...filteredPkg, + main: filteredPkg.main ?? attemptExportsFallback(filteredPkg), + }; + }; +} diff --git a/yarn.lock b/yarn.lock index ffef44d4cd16..1279f5d33173 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13027,6 +13027,7 @@ fsevents@^1.2.7: jest-util: ^27.2.5 jest-validate: ^27.2.5 resolve: ^1.20.0 + resolve.exports: ^1.1.0 slash: ^3.0.0 languageName: unknown linkType: soft @@ -18945,6 +18946,13 @@ react-native@0.64.0: languageName: node linkType: hard +"resolve.exports@npm:^1.1.0": + version: 1.1.0 + resolution: "resolve.exports@npm:1.1.0" + checksum: d04d2ce651fac14fe6ba13b377690f790cbbe91e6211b8fbec97ee08282e278875c74073a9b6243143a64e33d95eefb479e1dd4965664edc73b28b712100b36c + languageName: node + linkType: hard + "resolve@^1.1.6, resolve@^1.10.0, resolve@^1.14.2, resolve@^1.15.0, resolve@^1.19.0, resolve@^1.20.0, resolve@^1.3.2": version: 1.20.0 resolution: "resolve@npm:1.20.0" From 123c1ddac86f004d82a706f7fe4c70ed672038aa Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 15 Oct 2021 09:09:21 +0200 Subject: [PATCH 2/7] roll back supports-color dep --- packages/jest-worker/package.json | 4 ++-- yarn.lock | 23 +++++++---------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/packages/jest-worker/package.json b/packages/jest-worker/package.json index d7131a43fd92..bfcb73c20cd5 100644 --- a/packages/jest-worker/package.json +++ b/packages/jest-worker/package.json @@ -16,11 +16,11 @@ "dependencies": { "@types/node": "*", "merge-stream": "^2.0.0", - "supports-color": "^8.0.0" + "supports-color": "^7.2.0" }, "devDependencies": { "@types/merge-stream": "^1.1.2", - "@types/supports-color": "^8.1.0", + "@types/supports-color": "^7.2.1", "get-stream": "^6.0.0", "jest-leak-detector": "^27.2.5", "worker-farm": "^1.6.0" diff --git a/yarn.lock b/yarn.lock index 1279f5d33173..eefdd46de85e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4922,10 +4922,10 @@ __metadata: languageName: node linkType: hard -"@types/supports-color@npm:^8.1.0": - version: 8.1.1 - resolution: "@types/supports-color@npm:8.1.1" - checksum: 024855a1123b4da83c191f11da96eb939915ffee8dec7b9c7e8ccecea3b454a971884658096a5798f84d4b750629ffe4a16bc18b40ae34163e530213dec0740b +"@types/supports-color@npm:^7.2.1": + version: 7.2.1 + resolution: "@types/supports-color@npm:7.2.1" + checksum: 07b6dd790a9809a1470e7a2c3e46b6697c3b5c260120c11be5da29880f70833958bc8e730079a31c64914ebc324af2d281813601c2cf0b9794d5315ac0d69415 languageName: node linkType: hard @@ -13313,11 +13313,11 @@ fsevents@^1.2.7: dependencies: "@types/merge-stream": ^1.1.2 "@types/node": "*" - "@types/supports-color": ^8.1.0 + "@types/supports-color": ^7.2.1 get-stream: ^6.0.0 jest-leak-detector: ^27.2.5 merge-stream: ^2.0.0 - supports-color: ^8.0.0 + supports-color: ^7.2.0 worker-farm: ^1.6.0 languageName: unknown linkType: soft @@ -20434,7 +20434,7 @@ react-native@0.64.0: languageName: node linkType: hard -"supports-color@npm:^7.0.0, supports-color@npm:^7.1.0": +"supports-color@npm:^7.0.0, supports-color@npm:^7.1.0, supports-color@npm:^7.2.0": version: 7.2.0 resolution: "supports-color@npm:7.2.0" dependencies: @@ -20443,15 +20443,6 @@ react-native@0.64.0: languageName: node linkType: hard -"supports-color@npm:^8.0.0": - version: 8.1.1 - resolution: "supports-color@npm:8.1.1" - dependencies: - has-flag: ^4.0.0 - checksum: 0219f5c91753fea8dc8046cd4b18d39458b5dc0c6421c67c1072209faae9ba93b89283252e3b05d5c18901fd9f8b95001e3247fb93e2265f66d584a676522c75 - languageName: node - linkType: hard - "supports-hyperlinks@npm:^2.0.0": version: 2.2.0 resolution: "supports-hyperlinks@npm:2.2.0" From 047c8b9bcc150331241d0221f24c9c802ea36f42 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 17 Oct 2021 18:41:47 +0200 Subject: [PATCH 3/7] Revert "roll back supports-color dep" This reverts commit 123c1ddac86f004d82a706f7fe4c70ed672038aa. --- packages/jest-worker/package.json | 4 ++-- yarn.lock | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/jest-worker/package.json b/packages/jest-worker/package.json index bfcb73c20cd5..d7131a43fd92 100644 --- a/packages/jest-worker/package.json +++ b/packages/jest-worker/package.json @@ -16,11 +16,11 @@ "dependencies": { "@types/node": "*", "merge-stream": "^2.0.0", - "supports-color": "^7.2.0" + "supports-color": "^8.0.0" }, "devDependencies": { "@types/merge-stream": "^1.1.2", - "@types/supports-color": "^7.2.1", + "@types/supports-color": "^8.1.0", "get-stream": "^6.0.0", "jest-leak-detector": "^27.2.5", "worker-farm": "^1.6.0" diff --git a/yarn.lock b/yarn.lock index eefdd46de85e..1279f5d33173 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4922,10 +4922,10 @@ __metadata: languageName: node linkType: hard -"@types/supports-color@npm:^7.2.1": - version: 7.2.1 - resolution: "@types/supports-color@npm:7.2.1" - checksum: 07b6dd790a9809a1470e7a2c3e46b6697c3b5c260120c11be5da29880f70833958bc8e730079a31c64914ebc324af2d281813601c2cf0b9794d5315ac0d69415 +"@types/supports-color@npm:^8.1.0": + version: 8.1.1 + resolution: "@types/supports-color@npm:8.1.1" + checksum: 024855a1123b4da83c191f11da96eb939915ffee8dec7b9c7e8ccecea3b454a971884658096a5798f84d4b750629ffe4a16bc18b40ae34163e530213dec0740b languageName: node linkType: hard @@ -13313,11 +13313,11 @@ fsevents@^1.2.7: dependencies: "@types/merge-stream": ^1.1.2 "@types/node": "*" - "@types/supports-color": ^7.2.1 + "@types/supports-color": ^8.1.0 get-stream: ^6.0.0 jest-leak-detector: ^27.2.5 merge-stream: ^2.0.0 - supports-color: ^7.2.0 + supports-color: ^8.0.0 worker-farm: ^1.6.0 languageName: unknown linkType: soft @@ -20434,7 +20434,7 @@ react-native@0.64.0: languageName: node linkType: hard -"supports-color@npm:^7.0.0, supports-color@npm:^7.1.0, supports-color@npm:^7.2.0": +"supports-color@npm:^7.0.0, supports-color@npm:^7.1.0": version: 7.2.0 resolution: "supports-color@npm:7.2.0" dependencies: @@ -20443,6 +20443,15 @@ react-native@0.64.0: languageName: node linkType: hard +"supports-color@npm:^8.0.0": + version: 8.1.1 + resolution: "supports-color@npm:8.1.1" + dependencies: + has-flag: ^4.0.0 + checksum: 0219f5c91753fea8dc8046cd4b18d39458b5dc0c6421c67c1072209faae9ba93b89283252e3b05d5c18901fd9f8b95001e3247fb93e2265f66d584a676522c75 + languageName: node + linkType: hard + "supports-hyperlinks@npm:^2.0.0": version: 2.2.0 resolution: "supports-hyperlinks@npm:2.2.0" From 96ee934888042167dd23fd01958bbf97bb01a0de Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 17 Oct 2021 18:46:07 +0200 Subject: [PATCH 4/7] do not add main if there is `index` in root --- packages/jest-resolve/src/defaultResolver.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index c34aa8f5dc10..024d61b5c38b 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import {resolve} from 'path'; import * as fs from 'graceful-fs'; import pnpResolver from 'jest-pnp-resolver'; import {Opts as ResolveOpts, sync as resolveSync} from 'resolve'; @@ -175,11 +176,24 @@ function createPackageFilter( } } - return function packageFilter(pkg: PkgJson, ...rest) { + return function packageFilter(pkg: PkgJson, path) { let filteredPkg = pkg; if (userFilter) { - filteredPkg = userFilter(filteredPkg, ...rest); + filteredPkg = userFilter(filteredPkg, path); + } + + if (filteredPkg.main) { + return filteredPkg; + } + + const indexInRoot = resolve(path, './index.js'); + + // if the module contains an `index.js` file in root, `resolve` will request + // that if there is no `main`. Since we don't wanna break that, add this + // check + if (isFile(indexInRoot)) { + return filteredPkg; } return { From ab67dabdf395f498fcf21f7a0abad9d6aefd04d7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 17 Oct 2021 19:33:08 +0200 Subject: [PATCH 5/7] resolve from dir, not file --- packages/jest-resolve/src/defaultResolver.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 242dd5114bac..46f538419848 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -7,7 +7,7 @@ import {resolve} from 'path'; import pnpResolver from 'jest-pnp-resolver'; -import {Opts as ResolveOpts, sync as resolveSync} from 'resolve'; +import {sync as resolveSync} from 'resolve'; import { Options as ResolveExportsOptions, resolve as resolveExports, @@ -21,13 +21,19 @@ import { realpathSync, } from './fileWalkers'; -interface ResolverOptions extends ResolveOpts { +// copy from `resolve`'s types so we don't have their types in our definition +// files +interface ResolverOptions { basedir: Config.Path; browser?: boolean; conditions?: Array; defaultResolver: typeof defaultResolver; extensions?: Array; + moduleDirectory?: Array; + paths?: Array; rootDir?: Config.Path; + packageFilter?: (pkg: PkgJson, dir: string) => PkgJson; + pathFilter?: (pkg: PkgJson, path: string, relativePath: string) => string; } // https://github.com/facebook/jest/pull/10617 @@ -92,18 +98,18 @@ function createPackageFilter( } } - return function packageFilter(pkg: PkgJson, path) { + return function packageFilter(pkg, packageDir) { let filteredPkg = pkg; if (userFilter) { - filteredPkg = userFilter(filteredPkg, path); + filteredPkg = userFilter(filteredPkg, packageDir); } if (filteredPkg.main) { return filteredPkg; } - const indexInRoot = resolve(path, './index.js'); + const indexInRoot = resolve(packageDir, './index.js'); // if the module contains an `index.js` file in root, `resolve` will request // that if there is no `main`. Since we don't wanna break that, add this From c82f084b01a3e689395fcc49b35bc1c5a9e7eb02 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 17 Oct 2021 19:39:55 +0200 Subject: [PATCH 6/7] remove unused check we return early above if this is the case --- 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 46f538419848..52df2242646b 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -120,7 +120,7 @@ function createPackageFilter( return { ...filteredPkg, - main: filteredPkg.main ?? attemptExportsFallback(filteredPkg), + main: attemptExportsFallback(filteredPkg), }; }; } From 5c97bf3ed9c27e1a27fbb6c81df53abab0fbb8e5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 17 Oct 2021 19:40:48 +0200 Subject: [PATCH 7/7] allow empty string if they exist --- 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 52df2242646b..1c461feab16e 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -105,7 +105,7 @@ function createPackageFilter( filteredPkg = userFilter(filteredPkg, packageDir); } - if (filteredPkg.main) { + if (filteredPkg.main != null) { return filteredPkg; }