From 0a9e77d5e75adc87f8d7497ec30472c661c6fc11 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Thu, 1 Oct 2020 15:32:50 +0300 Subject: [PATCH] refactor(resolve): replace read-pkg-up with escalade (#10558) --- CHANGELOG.md | 2 ++ .../__snapshots__/nativeEsm.test.ts.snap | 2 +- e2e/__tests__/nativeEsm.test.ts | 4 +-- packages/jest-resolve/package.json | 2 +- packages/jest-resolve/src/shouldLoadAsEsm.ts | 32 +++++++++++++++---- yarn.lock | 4 +-- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99689b1d4169..95b3d0ec82d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ ### Chore & Maintenance +- `[jest-resolve]` Replace read-pkg-up with escalade package ([10558](https://github.com/facebook/jest/pull/10558)) + ### Performance ## 26.4.2 diff --git a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap index 953aa9a25bfa..eb5d4ee7e03c 100644 --- a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap +++ b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`on node ^12.16.0 || >=13.2.0 runs test with native ESM 1`] = ` +exports[`on node ^12.16.0 || >=13.7.0 runs test with native ESM 1`] = ` Test Suites: 1 passed, 1 total Tests: 12 passed, 12 total Snapshots: 0 total diff --git a/e2e/__tests__/nativeEsm.test.ts b/e2e/__tests__/nativeEsm.test.ts index e8addfb934f3..49a8427ab0c9 100644 --- a/e2e/__tests__/nativeEsm.test.ts +++ b/e2e/__tests__/nativeEsm.test.ts @@ -20,8 +20,8 @@ test('test config is without transform', () => { expect(configs[0].transform).toEqual([]); }); -// The versions vm.Module was introduced -onNodeVersions('^12.16.0 || >=13.2.0', () => { +// The versions where vm.Module exists and commonjs with "exports" is not broken +onNodeVersions('^12.16.0 || >=13.7.0', () => { test('runs test with native ESM', () => { const {exitCode, stderr, stdout} = runJest(DIR, [], { nodeOptions: '--experimental-vm-modules', diff --git a/packages/jest-resolve/package.json b/packages/jest-resolve/package.json index 75ff392890fe..9d0532636d78 100644 --- a/packages/jest-resolve/package.json +++ b/packages/jest-resolve/package.json @@ -12,10 +12,10 @@ "dependencies": { "@jest/types": "^26.3.0", "chalk": "^4.0.0", + "escalade": "^3.1.0", "graceful-fs": "^4.2.4", "jest-pnp-resolver": "^1.2.2", "jest-util": "^26.3.0", - "read-pkg-up": "^7.0.1", "resolve": "^1.17.0", "slash": "^3.0.0" }, diff --git a/packages/jest-resolve/src/shouldLoadAsEsm.ts b/packages/jest-resolve/src/shouldLoadAsEsm.ts index cab7b850eae6..654f4e75c8d7 100644 --- a/packages/jest-resolve/src/shouldLoadAsEsm.ts +++ b/packages/jest-resolve/src/shouldLoadAsEsm.ts @@ -8,17 +8,20 @@ import {dirname, extname} from 'path'; // @ts-expect-error: experimental, not added to the types import {SyntheticModule} from 'vm'; +import {readFileSync} from 'graceful-fs'; +import escalade from 'escalade/sync'; import type {Config} from '@jest/types'; -import readPkgUp = require('read-pkg-up'); const runtimeSupportsVmModules = typeof SyntheticModule === 'function'; const cachedFileLookups = new Map(); const cachedDirLookups = new Map(); +const cachedChecks = new Map(); export function clearCachedLookups(): void { cachedFileLookups.clear(); cachedDirLookups.clear(); + cachedChecks.clear(); } export default function cachedShouldLoadAsEsm(path: Config.Path): boolean { @@ -67,12 +70,29 @@ function shouldLoadAsEsm(path: Config.Path): boolean { } function cachedPkgCheck(cwd: Config.Path): boolean { - // TODO: can we cache lookups somehow? - const pkg = readPkgUp.sync({cwd, normalize: false}); - - if (!pkg) { + const pkgPath = escalade(cwd, (_dir, names) => { + if (names.includes('package.json')) { + // will be resolved into absolute + return 'package.json'; + } + return false; + }); + if (!pkgPath) { return false; } - return pkg.packageJson.type === 'module'; + let hasModuleField = cachedChecks.get(pkgPath); + if (hasModuleField != null) { + return hasModuleField; + } + + try { + const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')); + hasModuleField = pkg.type === 'module'; + } catch { + hasModuleField = false; + } + + cachedChecks.set(pkgPath, hasModuleField); + return hasModuleField; } diff --git a/yarn.lock b/yarn.lock index d39891853c33..59702da2e1aa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7743,7 +7743,7 @@ __metadata: languageName: node linkType: hard -"escalade@npm:^3.0.2": +"escalade@npm:^3.0.2, escalade@npm:^3.1.0": version: 3.1.0 resolution: "escalade@npm:3.1.0" checksum: 437c5b2619a412c0b075fb33e590e3516f187f7da8b20035685e08f346e27842722e5740a3398535d7d590ae4fb70068374ed59190d4eb4f9bb06d052e2fc92f @@ -11758,11 +11758,11 @@ fsevents@^1.2.7: "@types/graceful-fs": ^4.1.3 "@types/resolve": ^1.17.0 chalk: ^4.0.0 + escalade: ^3.1.0 graceful-fs: ^4.2.4 jest-haste-map: ^26.3.0 jest-pnp-resolver: ^1.2.2 jest-util: ^26.3.0 - read-pkg-up: ^7.0.1 resolve: ^1.17.0 slash: ^3.0.0 languageName: unknown