Skip to content

Commit f2ba85b

Browse files
committed
feat: implement resolver caching improvements
1 parent a8560f9 commit f2ba85b

File tree

4 files changed

+66
-17
lines changed

4 files changed

+66
-17
lines changed

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
"types": "./lib/index.d.ts"
1212
},
1313
"description": "The next resolver for `eslint-plugin-import` or `eslint-plugin-import-x`",
14-
"keywords": ["eslint", "eslint-plugin-import", "eslint-plugin-import-x", "eslint-import-resolver", "oxc-resolver"],
14+
"keywords": [
15+
"eslint",
16+
"eslint-plugin-import",
17+
"eslint-plugin-import-x",
18+
"eslint-import-resolver",
19+
"oxc-resolver"
20+
],
1521
"repository": "https://github.com/kuoruan/eslint-import-resolver-next",
1622
"license": "MIT",
1723
"files": [
@@ -30,7 +36,8 @@
3036
"dependencies": {
3137
"fast-glob": "^3.3.2",
3238
"js-yaml": "^4.1.0",
33-
"oxc-resolver": "^2.0.1"
39+
"oxc-resolver": "^2.0.1",
40+
"stable-hash": "^0.0.4"
3441
},
3542
"devDependencies": {
3643
"@babel/eslint-parser": "^7.24.7",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/resolve.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
cleanModulePath,
1414
findClosestPackageRoot,
1515
findPackages,
16+
hashObject,
1617
normalizeAlias,
1718
normalizeConfigFileOptions,
1819
normalizePackageGlobOptions,
@@ -52,7 +53,8 @@ function resolveRelativePath(
5253
}
5354

5455
const pathToPackagesMap = new Map<string, string[]>();
55-
let resolver: ResolverFactory | null = null;
56+
57+
const resolverCache = new Map<string, ResolverFactory>();
5658

5759
export default function resolve(
5860
modulePath: string,
@@ -138,24 +140,24 @@ export default function resolve(
138140
}
139141
}
140142

141-
if (!resolver) {
142-
resolver = new ResolverFactory({
143-
alias: resolveAlias,
144-
tsconfig: configFileOptions,
145-
roots: resolveRoots,
146-
...restOptions,
147-
});
148-
} else {
149-
const oldResolver = resolver;
143+
const specificOptions = {
144+
alias: resolveAlias,
145+
tsconfig: configFileOptions,
146+
roots: resolveRoots,
147+
} as const;
148+
149+
const hashKey = hashObject(specificOptions);
150150

151-
resolver = oldResolver.cloneWithOptions({
152-
alias: resolveAlias,
153-
tsconfig: configFileOptions,
154-
roots: resolveRoots,
151+
let resolver: ResolverFactory;
152+
if (resolverCache.has(hashKey)) {
153+
resolver = resolverCache.get(hashKey)!;
154+
} else {
155+
resolver = new ResolverFactory({
156+
...specificOptions,
155157
...restOptions,
156158
});
157159

158-
oldResolver.clearCache();
160+
resolverCache.set(hashKey, resolver);
159161
}
160162

161163
const result = resolver.sync(path.dirname(sourceFile), modulePath);

src/utils.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import crypto from "crypto";
12
import fastGlob from "fast-glob";
23
import fs from "fs";
34
import yaml from "js-yaml";
45
import path from "path";
6+
import stableHash from "stable-hash";
57

68
import {
79
defaultConfigFileOptions,
@@ -146,6 +148,12 @@ export function sortPaths(paths: string[]): string[] {
146148
});
147149
}
148150

151+
/**
152+
* Read a yaml file.
153+
*
154+
* @param filePath {string} - the file path to read
155+
* @returns {T | null} - the parsed yaml file
156+
*/
149157
export function readYamlFile<T>(filePath: string): T | null {
150158
if (!fs.existsSync(filePath)) {
151159
return null;
@@ -161,6 +169,13 @@ export function readYamlFile<T>(filePath: string): T | null {
161169
return doc;
162170
}
163171

172+
/**
173+
* Normalize package glob options.
174+
*
175+
* @param opts {PackageOptions | string[]} - the package options
176+
* @param root {string} - the root path
177+
* @returns {PackageGlobOptions} - the normalized package glob options
178+
*/
164179
export function normalizePackageGlobOptions(
165180
opts: PackageOptions | string[],
166181
root: string,
@@ -201,6 +216,13 @@ export function normalizePackageGlobOptions(
201216
};
202217
}
203218

219+
/**
220+
* Find the closest package from the source file.
221+
*
222+
* @param sourceFile {string} - the source file
223+
* @param paths {string[]} - the paths to search
224+
* @returns {string | undefined} - the closest package root
225+
*/
204226
export function findClosestPackageRoot(
205227
sourceFile: string,
206228
paths: string[],
@@ -291,3 +313,13 @@ export function normalizeAlias(
291313
{} as Record<string, string[]>,
292314
);
293315
}
316+
317+
/**
318+
* Get the hash of an object.
319+
*
320+
* @param obj {unknown} - the object to hash
321+
* @returns - the hash of the object
322+
*/
323+
export function hashObject(obj: unknown): string {
324+
return crypto.createHash("sha256").update(stableHash(obj)).digest("hex");
325+
}

0 commit comments

Comments
 (0)