Skip to content

Commit

Permalink
feat: reuse eslint-module-utils/hash.js for better caching (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Aug 22, 2022
1 parent 00abb6f commit 66a3e6c
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .changeset/calm-ties-search.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"eslint-import-resolver-typescript": patch
'eslint-import-resolver-typescript': patch
---

fix: incorrect exports mapping
5 changes: 5 additions & 0 deletions .changeset/ten-timers-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-import-resolver-typescript": minor
---

feat: reuse `eslint-module-utils/hash.js` for better caching
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dist
lib
CHANGELOG.md
/shim.d.ts
/pnpm-lock.yml
!/.*.cjs
!/.changeset
2 changes: 1 addition & 1 deletion .lintstagedrc.cjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('@1stg/lint-staged')
module.exports = require('@1stg/lint-staged/tsc')
2 changes: 1 addition & 1 deletion .remarkrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"plugins": [
"@1stg/remark-config"
"@1stg/preset"
]
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[![Code Style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![changesets](https://img.shields.io/badge/maintained%20with-changesets-176de3.svg)](https://github.com/changesets/changesets)

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

This means you can:

Expand Down Expand Up @@ -189,7 +189,7 @@ Default:

### Other options

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

### Default options

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

[ISC][]

[`eslint-plugin-i`]: https://github.com/un-es/eslint-plugin-i
[`eslint-plugin-import`]: https://github.com/import-js/eslint-plugin-import
[`enhanced-resolve`]: https://github.com/webpack/enhanced-resolve
[`typescript`]: https://www.typescriptlang.org
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"types": "lib/index.d.ts",
"files": [
"lib",
"shim.d.ts",
"!**/*.tsbuildinfo"
],
"keywords": [
Expand Down
4 changes: 4 additions & 0 deletions shim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'eslint-module-utils/hash.js' {
import type { Hash } from 'node:crypto'
export const hashObject: (object: object, hash?: Hash) => Hash
}
14 changes: 11 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Resolver,
ResolverFactory,
} from 'enhanced-resolve'
import { hashObject } from 'eslint-module-utils/hash.js'
import { createPathsMatcher, getTsconfig } from 'get-tsconfig'
import type { TsConfigResult } from 'get-tsconfig'
import isCore from 'is-core-module'
Expand Down Expand Up @@ -114,7 +115,8 @@ const fileSystem = fs as FileSystem
const JS_EXT_PATTERN = /\.(?:[cm]js|jsx?)$/
const RELATIVE_PATH_PATTERN = /^\.{1,2}(?:\/.*)?$/

let previousOptions: TsResolverOptions | null | undefined
let previousOptionsHash: string
let optionsHash: string
let cachedOptions: InternalResolverOptions | undefined

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

const digestHashObject = (value: object | null | undefined) =>
hashObject(value ?? {}).digest('hex')

/**
* @param source the module to resolve; i.e './some-module'
* @param file the importing file's full path; i.e. '/usr/local/bin/file.js'
Expand All @@ -137,8 +142,11 @@ export function resolve(
found: boolean
path?: string | null
} {
if (!cachedOptions || previousOptions !== options) {
previousOptions = options
if (
!cachedOptions ||
previousOptionsHash !== (optionsHash = digestHashObject(options))
) {
previousOptionsHash = optionsHash
cachedOptions = {
...options,
conditionNames: options?.conditionNames ?? defaultConditionNames,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"outDir": "./lib"
},
"include": ["./src"]
"include": ["./src", "./shim.d.ts"]
}

0 comments on commit 66a3e6c

Please sign in to comment.