Skip to content

Commit

Permalink
fix(jest-resolve): cache package.json lookups (#11076)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 18, 2021
1 parent 4fcb354 commit aec8573
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -84,6 +84,7 @@

### Performance

- `[jest-resolve]` Cache reading and parsing of `package.json`s ([#11076](https://github.com/facebook/jest/pull/11076))
- `[jest-runtime]` Load `chalk` only once per worker ([#10864](https://github.com/facebook/jest/pull/10864))

## 26.6.3
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Expand Up @@ -41,7 +41,7 @@ FAIL __tests__/index.js
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:556:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:558:17)
at Object.require (index.js:10:1)
`;

Expand Down Expand Up @@ -70,6 +70,6 @@ FAIL __tests__/index.js
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:556:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:558:17)
at Object.require (index.js:10:1)
`;
4 changes: 2 additions & 2 deletions packages/jest-resolve/package.json
Expand Up @@ -20,12 +20,12 @@
"graceful-fs": "^4.2.4",
"jest-pnp-resolver": "^1.2.2",
"jest-util": "^27.0.0-next.1",
"resolve": "^1.18.1",
"resolve": "^1.20.0",
"slash": "^3.0.0"
},
"devDependencies": {
"@types/graceful-fs": "^4.1.3",
"@types/resolve": "^1.17.0",
"@types/resolve": "^1.20.0",
"jest-haste-map": "^27.0.0-next.2"
},
"engines": {
Expand Down
27 changes: 25 additions & 2 deletions packages/jest-resolve/src/defaultResolver.ts
Expand Up @@ -7,7 +7,7 @@

import * as fs from 'graceful-fs';
import pnpResolver from 'jest-pnp-resolver';
import {Opts as ResolveOpts, sync as resolveSync} from 'resolve';
import {sync as resolveSync} from 'resolve';
import type {Config} from '@jest/types';
import {tryRealpath} from 'jest-util';

Expand All @@ -19,7 +19,7 @@ type ResolverOptions = {
moduleDirectory?: Array<string>;
paths?: Array<Config.Path>;
rootDir?: Config.Path;
packageFilter?: ResolveOpts['packageFilter'];
packageFilter?: (pkg: any, pkgfile: string) => any;
};

// https://github.com/facebook/jest/pull/10617
Expand Down Expand Up @@ -50,6 +50,7 @@ export default function defaultResolver(
packageFilter: options.packageFilter,
paths: options.paths,
preserveSymlinks: false,
readPackageSync,
realpathSync,
});

Expand All @@ -61,6 +62,7 @@ export default function defaultResolver(
export function clearDefaultResolverCache(): void {
checkedPaths.clear();
checkedRealpathPaths.clear();
packageContents.clear();
}

enum IPathType {
Expand Down Expand Up @@ -118,6 +120,23 @@ function realpathCached(path: Config.Path): Config.Path {
return result;
}

type PkgJson = Record<string, unknown>;

const packageContents = new Map<string, PkgJson>();
function readPackageCached(path: Config.Path): PkgJson {
let result = packageContents.get(path);

if (result !== undefined) {
return result;
}

result = JSON.parse(fs.readFileSync(path, 'utf8')) as PkgJson;

packageContents.set(path, result);

return result;
}

/*
* helper functions
*/
Expand All @@ -132,3 +151,7 @@ function isDirectory(dir: Config.Path): boolean {
function realpathSync(file: Config.Path): Config.Path {
return realpathCached(file);
}

function readPackageSync(_: unknown, file: Config.Path): PkgJson {
return readPackageCached(file);
}
4 changes: 1 addition & 3 deletions packages/jest-resolve/src/index.ts
Expand Up @@ -48,7 +48,7 @@ const nodePaths = NODE_PATH
.map(p => path.resolve(resolvedCwd, p))
: undefined;

class Resolver {
export default class Resolver {
private readonly _options: ResolverConfig;
private readonly _moduleMap: ModuleMap;
private readonly _moduleIDCache: Map<string, string>;
Expand Down Expand Up @@ -495,5 +495,3 @@ Please check your configuration for these entries:

return error;
};

export default Resolver;
21 changes: 9 additions & 12 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -271,7 +271,7 @@ export default class Runtime {

static shouldInstrument = shouldInstrument;

static createContext(
static async createContext(
config: Config.ProjectConfig,
options: {
console?: Console;
Expand All @@ -288,17 +288,14 @@ export default class Runtime {
watch: options.watch,
watchman: options.watchman,
});
return instance.build().then(
hasteMap => ({
config,
hasteFS: hasteMap.hasteFS,
moduleMap: hasteMap.moduleMap,
resolver: Runtime.createResolver(config, hasteMap.moduleMap),
}),
error => {
throw error;
},
);
const hasteMap = await instance.build();

return {
config,
hasteFS: hasteMap.hasteFS,
moduleMap: hasteMap.moduleMap,
resolver: Runtime.createResolver(config, hasteMap.moduleMap),
};
}

static createHasteMap(
Expand Down
18 changes: 8 additions & 10 deletions yarn.lock
Expand Up @@ -3830,12 +3830,10 @@ __metadata:
languageName: node
linkType: hard

"@types/resolve@npm:^1.17.0":
version: 1.19.0
resolution: "@types/resolve@npm:1.19.0"
dependencies:
"@types/node": "*"
checksum: 6dab5d4bf568d4fe7ed2207f839736d3558e04f994cdad122ce3c0a3d75f56080f4f63a297d5dcf61a1d3f450ebb99a8ca3719e3b7147c863d80436f1a9890e2
"@types/resolve@npm:^1.20.0":
version: 1.20.0
resolution: "@types/resolve@npm:1.20.0"
checksum: 3c75135d5cf3652453ef8f099b109f7fec5e82fe67bf38048226614dbcd11a943affee383e5d28c12c5f03b049281a3e486395326b9810297f9649c7b00f41fd
languageName: node
linkType: hard

Expand Down Expand Up @@ -12067,14 +12065,14 @@ fsevents@^1.2.7:
dependencies:
"@jest/types": ^27.0.0-next.1
"@types/graceful-fs": ^4.1.3
"@types/resolve": ^1.17.0
"@types/resolve": ^1.20.0
chalk: ^4.0.0
escalade: ^3.1.1
graceful-fs: ^4.2.4
jest-haste-map: ^27.0.0-next.2
jest-pnp-resolver: ^1.2.2
jest-util: ^27.0.0-next.1
resolve: ^1.18.1
resolve: ^1.20.0
slash: ^3.0.0
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -17276,7 +17274,7 @@ react-native@0.63.2:
languageName: node
linkType: hard

"resolve@^1.1.6, resolve@^1.10.0, resolve@^1.13.1, resolve@^1.15.0, resolve@^1.17.0, resolve@^1.18.1, resolve@^1.5.0":
"resolve@^1.1.6, resolve@^1.10.0, resolve@^1.13.1, resolve@^1.15.0, resolve@^1.17.0, resolve@^1.20.0, resolve@^1.5.0":
version: 1.20.0
resolution: "resolve@npm:1.20.0"
dependencies:
Expand All @@ -17286,7 +17284,7 @@ react-native@0.63.2:
languageName: node
linkType: hard

"resolve@patch:resolve@^1.1.6#builtin<compat/resolve>, resolve@patch:resolve@^1.10.0#builtin<compat/resolve>, resolve@patch:resolve@^1.13.1#builtin<compat/resolve>, resolve@patch:resolve@^1.15.0#builtin<compat/resolve>, resolve@patch:resolve@^1.17.0#builtin<compat/resolve>, resolve@patch:resolve@^1.18.1#builtin<compat/resolve>, resolve@patch:resolve@^1.5.0#builtin<compat/resolve>":
"resolve@patch:resolve@^1.1.6#builtin<compat/resolve>, resolve@patch:resolve@^1.10.0#builtin<compat/resolve>, resolve@patch:resolve@^1.13.1#builtin<compat/resolve>, resolve@patch:resolve@^1.15.0#builtin<compat/resolve>, resolve@patch:resolve@^1.17.0#builtin<compat/resolve>, resolve@patch:resolve@^1.20.0#builtin<compat/resolve>, resolve@patch:resolve@^1.5.0#builtin<compat/resolve>":
version: 1.20.0
resolution: "resolve@patch:resolve@npm%3A1.20.0#builtin<compat/resolve>::version=1.20.0&hash=3388aa"
dependencies:
Expand Down

0 comments on commit aec8573

Please sign in to comment.