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

[Proposal] Support using any object satisfied Resolver type in import-x/resolver options #75

Closed
GoodbyeNJN opened this issue Apr 23, 2024 · 1 comment · Fixed by #159

Comments

@GoodbyeNJN
Copy link

GoodbyeNJN commented Apr 23, 2024

The resolver currently supports three reference methods:

  • name -> eslint-import-resolver-name
  • npm package name
  • filesystem path

I propose to support the direct use of any object satisfied Resolver type, and I further propose configuration other than this should be deprecated in the future.

Advantage:

  • Reduce complexity when trying to require resolvers
  • Defining a custom resolver is more convenient

For advantage 1, you can refer to ESLint’s thoughts on the introduction of flat config:

Use native loading for everything - one of our biggest regrets about eslintrc was recreating the Node.js require resolution in a custom way. This was a significant source of complexity and, in hindsight, unnecessary. Going forward, we wanted to leverage the loading capabilities of the JavaScript runtime directly.

Reference: https://eslint.org/blog/2022/08/new-config-system-part-2/#the-goals-of-flat-config

For advantage 2, there may be cases when existing resolvers don't meet the needs and needs to be temporarily added.

In this case, if the direct use of objects is supported, the steps of "creating new file -> processing filesystem path" can be eliminated, and the object can be created in place and used immediately.

For example, suppose I want to bundle the eslint configuration and all dependencies into a single file for use. In this case, none of the above three configuration methods will work. However, if the feature of directly using the resolver object is provided, my needs can be well met.

In order to support the use of Resolver objects, the configuration structure of the import-x/resolver configuration item needs to be modified.

Before:

export type ImportResolver =
  | LiteralUnion<'node' | 'typescript' | 'webpack', string>
  | {
      node?: boolean | NodeResolverOptions
      typescript?: boolean | TsResolverOptions
      webpack?: WebpackResolverOptions
      [resolve: string]: unknown
    }

After:

export type Resolver = {
  interfaceVersion?: 1 | 2
  resolve: ResolverResolve
  resolveImport: ResolverResolveImport
}

export type ResolverName = LiteralUnion<
  'node' | 'typescript' | 'webpack',
  string
>

export type ResolverRecord = {
  node?: boolean | NodeResolverOptions
  typescript?: boolean | TsResolverOptions
  webpack?: WebpackResolverOptions
  [resolve: string]: unknown
}

export type ResolverObject = {
  // node, typescript, webpack...
  name: ResolverName

  // Enabled by default
  enable?: boolean

  // Options passed to the resolver
  options?:
    | NodeResolverOptions
    | TsResolverOptions
    | WebpackResolverOptions
    | unknown

  // Any object satisfied Resolver type
  resolver: Resolver
}

export type ImportResolver =
  | ResolverName
  | ResolverRecord
  | ResolverObject
  | ResolverName[]
  | ResolverRecord[]
  | ResolverObject[]
@GoodbyeNJN
Copy link
Author

Sorry, I found out after opening the issue that there has been a discussion before: #40 (comment)

But I still want to emphasize that custom resolver should be supported. This does not conflict with the current logic of trying to require resolvers. On the contrary, it will bring certain conveniences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant