Skip to content

Commit

Permalink
cache lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Dec 30, 2020
1 parent d45984f commit 688f0aa
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -165,7 +165,8 @@ export default class Runtime {
private readonly _moduleMocker: ModuleMocker;
private _isolatedModuleRegistry: ModuleRegistry | null;
private _moduleRegistry: ModuleRegistry;
private readonly _esmoduleRegistry: Map<string, EsmModuleCache>;
private readonly _esmoduleRegistry: Map<Config.Path, EsmModuleCache>;
private readonly _cjsNamedExports: Map<Config.Path, Set<string>>;
private readonly _testPath: Config.Path;
private readonly _resolver: Resolver;
private _shouldAutoMock: boolean;
Expand Down Expand Up @@ -214,6 +215,7 @@ export default class Runtime {
this._isolatedMockRegistry = null;
this._moduleRegistry = new Map();
this._esmoduleRegistry = new Map();
this._cjsNamedExports = new Map();
this._testPath = testPath;
this._resolver = resolver;
this._scriptTransformer = new ScriptTransformer(config, this._cacheFS);
Expand Down Expand Up @@ -551,21 +553,29 @@ export default class Runtime {
}

private getExportsOfCjs(modulePath: Config.Path) {
const cachedNamedExports = this._cjsNamedExports.get(modulePath);

if (cachedNamedExports) {
return cachedNamedExports;
}

const transformedCode =
this._fileTransforms.get(modulePath)?.code ?? this.readFile(modulePath);

const {exports, reexports} = parseCjs(transformedCode);

let namedExports = new Set(exports);
const namedExports = new Set(exports);

reexports.forEach(reexport => {
const resolved = this._resolveModule(modulePath, reexport);

const exports = this.getExportsOfCjs(resolved);

namedExports = new Set([...namedExports, ...exports]);
exports.forEach(namedExports.add, namedExports);
});

this._cjsNamedExports.set(modulePath, namedExports);

return namedExports;
}

Expand Down Expand Up @@ -858,6 +868,7 @@ export default class Runtime {
this._mockRegistry.clear();
this._moduleRegistry.clear();
this._esmoduleRegistry.clear();
this._cjsNamedExports.clear();

if (this._environment) {
if (this._environment.global) {
Expand Down

0 comments on commit 688f0aa

Please sign in to comment.