Skip to content

Commit

Permalink
refactor(resolve): replace read-pkg-up with escalade (#10558)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound authored Oct 1, 2020
1 parent a4bdf96 commit 0a9e77d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/nativeEsm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
32 changes: 26 additions & 6 deletions packages/jest-resolve/src/shouldLoadAsEsm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -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;
}
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0a9e77d

Please sign in to comment.