From 9ae346bdded1155fa1cb99091fa4708a17b10c4d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 6 Oct 2020 10:39:03 +0200 Subject: [PATCH] Revert "refactor(resolve): replace read-pkg-up with escalade (#10558)" This reverts commit 0a9e77d5e75adc87f8d7497ec30472c661c6fc11. --- CHANGELOG.md | 2 ++ packages/jest-resolve/package.json | 2 +- packages/jest-resolve/src/shouldLoadAsEsm.ts | 32 ++++---------------- yarn.lock | 2 +- 4 files changed, 10 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f29887d759a1..85d6a4c3abb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-resolve]` Revert usage of Escalade as it breaks Node 13 + ### Chore & Maintenance ### Performance diff --git a/packages/jest-resolve/package.json b/packages/jest-resolve/package.json index 73a72d3f70b9..398ed20a3ed4 100644 --- a/packages/jest-resolve/package.json +++ b/packages/jest-resolve/package.json @@ -12,10 +12,10 @@ "dependencies": { "@jest/types": "^26.5.0", "chalk": "^4.0.0", - "escalade": "^3.1.0", "graceful-fs": "^4.2.4", "jest-pnp-resolver": "^1.2.2", "jest-util": "^26.5.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 654f4e75c8d7..cab7b850eae6 100644 --- a/packages/jest-resolve/src/shouldLoadAsEsm.ts +++ b/packages/jest-resolve/src/shouldLoadAsEsm.ts @@ -8,20 +8,17 @@ 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 { @@ -70,29 +67,12 @@ function shouldLoadAsEsm(path: Config.Path): boolean { } function cachedPkgCheck(cwd: Config.Path): boolean { - 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; - } + // TODO: can we cache lookups somehow? + const pkg = readPkgUp.sync({cwd, normalize: false}); - 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; + if (!pkg) { + return false; } - cachedChecks.set(pkgPath, hasModuleField); - return hasModuleField; + return pkg.packageJson.type === 'module'; } diff --git a/yarn.lock b/yarn.lock index e064dd56865d..bebab1dc40e6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11773,11 +11773,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.5.0 jest-pnp-resolver: ^1.2.2 jest-util: ^26.5.0 + read-pkg-up: ^7.0.1 resolve: ^1.17.0 slash: ^3.0.0 languageName: unknown