Skip to content

Commit

Permalink
Revert "refactor(resolve): replace read-pkg-up with escalade (jestjs#…
Browse files Browse the repository at this point in the history
…10558)"

This reverts commit 0a9e77d.
  • Loading branch information
SimenB committed Oct 6, 2020
1 parent 999ee46 commit 9ae346b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-resolve]` Revert usage of Escalade as it breaks Node 13

### Chore & Maintenance

### Performance
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-resolve/package.json
Expand Up @@ -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"
},
Expand Down
32 changes: 6 additions & 26 deletions packages/jest-resolve/src/shouldLoadAsEsm.ts
Expand Up @@ -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<string, boolean>();
const cachedDirLookups = new Map<string, boolean>();
const cachedChecks = new Map<string, boolean>();

export function clearCachedLookups(): void {
cachedFileLookups.clear();
cachedDirLookups.clear();
cachedChecks.clear();
}

export default function cachedShouldLoadAsEsm(path: Config.Path): boolean {
Expand Down Expand Up @@ -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';
}
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -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
Expand Down

0 comments on commit 9ae346b

Please sign in to comment.