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

Support declaring internal module regex or respect import/internal-regex setting #227

Open
harunurhan opened this issue May 24, 2023 · 6 comments

Comments

@harunurhan
Copy link

Resolver performs way too slow on a medium to large TS project where every local/internal import is absolute from the project root, since

if (RELATIVE_PATH_PATTERN.test(source)) {
optimization branch is not used.

It would be useful to allow users to specify internalRegex similar to https://github.com/import-js/eslint-plugin-import/tree/a89eadf3247af844da6b0f9e7bca7690777bf665#importinternal-regex.

Worth mentioning that using above import/internal-regex did speed up the linting times, but unexpectedly I have seen more improvements when implemented a custom resolver like below using the same regexp, so perhaps there is a bug (or misunderstanding) there and no new option is needed, just addressing that one.

export function resolve(
    importPath: string,
    importerFileAbsolutePath: string,
    tsResolverOptions: TsResolverOptions
): ResolveResult {
    if (internalModuleRegexp.test(importPath)) {
        const absoluteImportPath = path.resolve(`./${importPath}`);
        return {
            found: true,
            path: absoluteImportPath,
        };
    }

    return eslintImportResolverTypescript.resolve(importPath, importerFileAbsolutePath, tsResolverOptions);
}
@JounQin
Copy link
Collaborator

JounQin commented May 24, 2023

I don't quite follow you meanings, why internalModuleRegexp matched means the related absoluteImportPath as resolved.

@harunurhan
Copy link
Author

This is a simplified implementation that won't work for every usecase perhaps, sorry for confusion, but what I mean:

Imagine you have a large repo like this

- foo
  - nested
    -  file1.ts
- bar
  - nested
    - file2.ts 
- package.json
- .eslintrc.json
- tsconfig.json

And file1.ts is using an absolute (to the tsconfig) import path.

import { something } from "bar/nested/file2"

For this import we are using enchanced-resolve, however we could just resolve it faster since we have foo and bar are our top level folders and we use absolute import paths.

@JounQin
Copy link
Collaborator

JounQin commented May 24, 2023

however we could just resolve it faster since we have foo and bar are our top level folders and we use absolute import paths

This seems not always true?

For a common mono repo, the structure would be:

- packages
  - foo
    - nested
      - file1.ts
  - bar
    - nested
      - file2.ts
- package.json

How can we resolve it faster for bar/nested/file2 in this case?

@harunurhan
Copy link
Author

if we base tsconfig path while resolving not the package.json, this would work right?, though complicated when there are multiple projects linting via same config. Also we could ask for users to provide path to root folder 🤷‍♂️

@JounQin
Copy link
Collaborator

JounQin commented May 24, 2023

I still can't fully get your point, but a PR for showing case would be appreciated. 👍

@harunurhan
Copy link
Author

Perhaps it's also hard to implement in a generic way, that's why my explanation doesn't make a lot of sense but I'll give it a try when I have time.

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

No branches or pull requests

2 participants