Skip to content

Commit 66a3e6c

Browse files
authored
feat: reuse eslint-module-utils/hash.js for better caching (#174)
1 parent 00abb6f commit 66a3e6c

10 files changed

+29
-9
lines changed

.changeset/calm-ties-search.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
"eslint-import-resolver-typescript": patch
2+
'eslint-import-resolver-typescript': patch
33
---
44

55
fix: incorrect exports mapping

.changeset/ten-timers-rest.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-import-resolver-typescript": minor
3+
---
4+
5+
feat: reuse `eslint-module-utils/hash.js` for better caching

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
dist
22
lib
33
CHANGELOG.md
4+
/shim.d.ts
45
/pnpm-lock.yml
56
!/.*.cjs
67
!/.changeset

.lintstagedrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('@1stg/lint-staged')
1+
module.exports = require('@1stg/lint-staged/tsc')

.remarkrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"plugins": [
3-
"@1stg/remark-config"
3+
"@1stg/preset"
44
]
55
}

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
[![Code Style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
1313
[![changesets](https://img.shields.io/badge/maintained%20with-changesets-176de3.svg)](https://github.com/changesets/changesets)
1414

15-
This plugin adds [`TypeScript`][] support to [`eslint-plugin-import`][]
15+
This plugin adds [`TypeScript`][] support to [`eslint-plugin-import`][] (Or maybe you want to try [`eslint-plugin-i`][] for faster speed)
1616

1717
This means you can:
1818

@@ -189,7 +189,7 @@ Default:
189189

190190
### Other options
191191

192-
You can pass through other options of [`enhanced-resolve`] directly
192+
You can pass through other options of [`enhanced-resolve`][] directly
193193

194194
### Default options
195195

@@ -226,6 +226,7 @@ Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.m
226226

227227
[ISC][]
228228

229+
[`eslint-plugin-i`]: https://github.com/un-es/eslint-plugin-i
229230
[`eslint-plugin-import`]: https://github.com/import-js/eslint-plugin-import
230231
[`enhanced-resolve`]: https://github.com/webpack/enhanced-resolve
231232
[`typescript`]: https://www.typescriptlang.org

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"types": "lib/index.d.ts",
6363
"files": [
6464
"lib",
65+
"shim.d.ts",
6566
"!**/*.tsbuildinfo"
6667
],
6768
"keywords": [

shim.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module 'eslint-module-utils/hash.js' {
2+
import type { Hash } from 'node:crypto'
3+
export const hashObject: (object: object, hash?: Hash) => Hash
4+
}

src/index.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Resolver,
1111
ResolverFactory,
1212
} from 'enhanced-resolve'
13+
import { hashObject } from 'eslint-module-utils/hash.js'
1314
import { createPathsMatcher, getTsconfig } from 'get-tsconfig'
1415
import type { TsConfigResult } from 'get-tsconfig'
1516
import isCore from 'is-core-module'
@@ -114,7 +115,8 @@ const fileSystem = fs as FileSystem
114115
const JS_EXT_PATTERN = /\.(?:[cm]js|jsx?)$/
115116
const RELATIVE_PATH_PATTERN = /^\.{1,2}(?:\/.*)?$/
116117

117-
let previousOptions: TsResolverOptions | null | undefined
118+
let previousOptionsHash: string
119+
let optionsHash: string
118120
let cachedOptions: InternalResolverOptions | undefined
119121

120122
let mappersCachedOptions: InternalResolverOptions
@@ -123,6 +125,9 @@ let mappers: Array<((specifier: string) => string[]) | null> | undefined
123125
let resolverCachedOptions: InternalResolverOptions
124126
let resolver: Resolver | undefined
125127

128+
const digestHashObject = (value: object | null | undefined) =>
129+
hashObject(value ?? {}).digest('hex')
130+
126131
/**
127132
* @param source the module to resolve; i.e './some-module'
128133
* @param file the importing file's full path; i.e. '/usr/local/bin/file.js'
@@ -137,8 +142,11 @@ export function resolve(
137142
found: boolean
138143
path?: string | null
139144
} {
140-
if (!cachedOptions || previousOptions !== options) {
141-
previousOptions = options
145+
if (
146+
!cachedOptions ||
147+
previousOptionsHash !== (optionsHash = digestHashObject(options))
148+
) {
149+
previousOptionsHash = optionsHash
142150
cachedOptions = {
143151
...options,
144152
conditionNames: options?.conditionNames ?? defaultConditionNames,

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"compilerOptions": {
44
"outDir": "./lib"
55
},
6-
"include": ["./src"]
6+
"include": ["./src", "./shim.d.ts"]
77
}

0 commit comments

Comments
 (0)