Skip to content

Add v3 resolver interface #2

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

Merged
merged 1 commit into from
Dec 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fs from 'node:fs'

import enhancedResolve from 'enhanced-resolve'
import { CachedInputFileSystem, ResolverFactory } from 'enhanced-resolve'

import { defaultConditionNames, defaultExtensionAlias, defaultExtensions, defaultMainFields } from './default'
import { digestHashObject } from './helpers'
import { logger } from './logger'
import { resolveImport } from './resolveImport'

import type { InternalResolverOptions, ResolverOptions } from './types'
import type { InternalResolverOptions, ResolvedResult, ResolverOptions } from './types'
import type { Resolver } from 'enhanced-resolve'

const times = []
Expand All @@ -25,11 +25,7 @@ export const interfaceVersion = 2
* @param file the importing file's full path; i.e. '/usr/local/bin/file.js'
* @param options
*/
export function resolve(
specifier: string,
file: string,
options?: ResolverOptions | undefined | null
): { found: boolean; path?: string | null } {
export function resolve(specifier: string, file: string, options?: ResolverOptions | undefined | null): ResolvedResult {
let t0: DOMHighResTimeStamp = 0

if (options?.performanceToLog) t0 = performance.now()
Expand All @@ -42,13 +38,13 @@ export function resolve(
extensions: options?.extensions ?? defaultExtensions,
extensionAlias: options?.extensionAlias ?? defaultExtensionAlias,
mainFields: options?.mainFields ?? defaultMainFields,
fileSystem: new enhancedResolve.CachedInputFileSystem(fs, 5 * 1000),
fileSystem: new CachedInputFileSystem(fs, 5 * 1000),
useSyncFileSystemCalls: true,
}
}

if (!resolver || resolverCachedOptions !== cachedOptions) {
resolver = enhancedResolve.ResolverFactory.createResolver(cachedOptions)
resolver = ResolverFactory.createResolver(cachedOptions)
resolverCachedOptions = cachedOptions
}

Expand All @@ -62,3 +58,35 @@ export function resolve(

return result
}

export function createCustomResolver(options: ResolverOptions) {
const resolver = ResolverFactory.createResolver({
...options,
conditionNames: options?.conditionNames ?? defaultConditionNames,
extensions: options?.extensions ?? defaultExtensions,
extensionAlias: options?.extensionAlias ?? defaultExtensionAlias,
mainFields: options?.mainFields ?? defaultMainFields,
fileSystem: new CachedInputFileSystem(fs, 5 * 1000),
useSyncFileSystemCalls: true,
})

return {
name: 'eslint-import-resolver-x',
interfaceVersion: 3,
resolve(modulePath: string, source: string) {
let t0: DOMHighResTimeStamp = 0

if (options?.performanceToLog) t0 = performance.now()

const result = resolveImport(modulePath, source, cachedOptions, resolver)

if (options?.performanceToLog) {
const t1 = performance.now()
times.push(t1 - t0)
logger('time resolve:', t1)
}

return result
},
}
}
6 changes: 3 additions & 3 deletions src/resolveImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { isFile, isModule, removeQuerystring } from './helpers'
import { init } from './init'
import { logger } from './logger'

import type { InternalResolverOptions, Matcher } from './types'
import type { InternalResolverOptions, Matcher, ResolvedResult } from './types'
import type { Resolver } from 'enhanced-resolve'
import type { TsConfigResult } from 'get-tsconfig'
import type { Version } from 'is-bun-module'
Expand All @@ -25,14 +25,14 @@ const possiblePathsBySpecifier: Map<string, { paths: string[]; path: TsConfigRes
* @param {string} file path of the file importing the specifier
* @param {InternalResolverOptions} options options for the resolver
* @param {Resolver} resolver resolver to use for resolving
* @returns { found: boolean; path?: string | null } whether the import was found and the resolved path
* @returns {ResolvedResult} whether the import was found and the resolved path
*/
export function resolveImport(
specifier: string,
file: string,
options: InternalResolverOptions,
resolver: Resolver
): { found: boolean; path?: string | null } {
): ResolvedResult {
logger('Resolving:', `'${specifier}'`, 'from:', file)

specifier = removeQuerystring(specifier)
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ export type Matcher = {
path: string
config: TsConfigJsonResolved
}

export type ResultNotFound = {
found: false
path?: undefined
}

export type ResultFound = {
found: true
path: string | null
}

export type ResolvedResult = ResultNotFound | ResultFound