From 66a3e6c4349fe1821a5ada967b7cff1b8191dfe4 Mon Sep 17 00:00:00 2001 From: JounQin Date: Mon, 22 Aug 2022 22:33:00 +0800 Subject: [PATCH] feat: reuse `eslint-module-utils/hash.js` for better caching (#174) --- .changeset/calm-ties-search.md | 2 +- .changeset/ten-timers-rest.md | 5 +++++ .eslintignore | 1 + .lintstagedrc.cjs | 2 +- .remarkrc | 2 +- README.md | 5 +++-- package.json | 1 + shim.d.ts | 4 ++++ src/index.ts | 14 +++++++++++--- tsconfig.json | 2 +- 10 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 .changeset/ten-timers-rest.md create mode 100644 shim.d.ts diff --git a/.changeset/calm-ties-search.md b/.changeset/calm-ties-search.md index a346f8d..3312c9d 100644 --- a/.changeset/calm-ties-search.md +++ b/.changeset/calm-ties-search.md @@ -1,5 +1,5 @@ --- -"eslint-import-resolver-typescript": patch +'eslint-import-resolver-typescript': patch --- fix: incorrect exports mapping diff --git a/.changeset/ten-timers-rest.md b/.changeset/ten-timers-rest.md new file mode 100644 index 0000000..5514424 --- /dev/null +++ b/.changeset/ten-timers-rest.md @@ -0,0 +1,5 @@ +--- +"eslint-import-resolver-typescript": minor +--- + +feat: reuse `eslint-module-utils/hash.js` for better caching diff --git a/.eslintignore b/.eslintignore index 15d4b74..74e99da 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,6 +1,7 @@ dist lib CHANGELOG.md +/shim.d.ts /pnpm-lock.yml !/.*.cjs !/.changeset diff --git a/.lintstagedrc.cjs b/.lintstagedrc.cjs index c1a438e..3f53038 100644 --- a/.lintstagedrc.cjs +++ b/.lintstagedrc.cjs @@ -1 +1 @@ -module.exports = require('@1stg/lint-staged') +module.exports = require('@1stg/lint-staged/tsc') diff --git a/.remarkrc b/.remarkrc index 63d195b..4915bad 100644 --- a/.remarkrc +++ b/.remarkrc @@ -1,5 +1,5 @@ { "plugins": [ - "@1stg/remark-config" + "@1stg/preset" ] } diff --git a/README.md b/README.md index c906bfd..5bd64b3 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 @@ -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 diff --git a/package.json b/package.json index 0849dfb..70e648d 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "types": "lib/index.d.ts", "files": [ "lib", + "shim.d.ts", "!**/*.tsbuildinfo" ], "keywords": [ diff --git a/shim.d.ts b/shim.d.ts new file mode 100644 index 0000000..e76db42 --- /dev/null +++ b/shim.d.ts @@ -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 +} diff --git a/src/index.ts b/src/index.ts index 3a80169..195148b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -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 @@ -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' @@ -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, diff --git a/tsconfig.json b/tsconfig.json index b5176ce..81e4c05 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,5 +3,5 @@ "compilerOptions": { "outDir": "./lib" }, - "include": ["./src"] + "include": ["./src", "./shim.d.ts"] }