Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reuse eslint-module-utils/hash.js for better caching #174

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"]
}