|
| 1 | +# eslint-import-resolver-x |
| 2 | + |
| 3 | +This resolver adds [`TypeScript`][] or [`JavaScript`][] import support to [`eslint-plugin-import`][] with `tsconfig.json` or `jsconfig.json` aliases (`compilerOptions.paths`) |
| 4 | + |
| 5 | +This is fork of [`eslint-import-resolver-typescript`][] but much faster **(~30-40%)** |
| 6 | + |
| 7 | +For example we can use `strace` package for count of [fstat](`https://nodejs.org/api/fs.html#fsfstatsyncfd-options`) call when linting you codebase |
| 8 | + |
| 9 | +Example you codebase has multiple packages with 100k LOC and more |
| 10 | + |
| 11 | +| syscall | eslint-import-resolver-x | eslint-import-resolver-typescript | |
| 12 | +| ---------- | ------------------------ | --------------------------------- | |
| 13 | +| access | 85 027 | |
| 14 | +| chdir | 1 | |
| 15 | +| execve | 78 | |
| 16 | +| faccessat2 | 1 | |
| 17 | +| getcwd | 3 | |
| 18 | +| mkdir | 2 | |
| 19 | +| newfstatat | 31 761 | |
| 20 | +| openat | 86 287 | |
| 21 | +| readlink | 1 183 633 | |
| 22 | +| statfs | 2 | |
| 23 | +| statx | 1 081 306 | |
| 24 | +| unlink | 3 | |
| 25 | + |
| 26 | +We are interested in how many accesses to the file system occurred during the linting process. We can pay attention to `statx` syscall. |
| 27 | + |
| 28 | +`eslint-import-resolver-x` makes calls `statx` 100 times less. |
| 29 | + |
| 30 | +**If you notice an increase in linting performance in your CI on large code bases, write your feedback [here](https://github.com/helljs/eslint-import-resolver-x/discussions/1)** |
| 31 | + |
| 32 | +## Description |
| 33 | + |
| 34 | +You can: |
| 35 | + |
| 36 | +- `import`/`require` files with extension any extenstions of `js` or `ts` |
| 37 | +- Use [`paths`](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping) defined in `tsconfig.json` or `jsconfig.json` |
| 38 | +- Multiple tsconfigs or jsconfigs support |
| 39 | +- `imports/exports` fields support in `package.json` |
| 40 | + |
| 41 | +## Installation |
| 42 | + |
| 43 | +```sh |
| 44 | +# npm |
| 45 | +npm i -D eslint-plugin-import @helljs/eslint-import-resolver-x |
| 46 | + |
| 47 | +# pnpm |
| 48 | +pnpm i -D eslint-plugin-import @helljs/eslint-import-resolver-x |
| 49 | + |
| 50 | +# yarn |
| 51 | +yarn add -D eslint-plugin-import @helljs/eslint-import-resolver-x |
| 52 | +``` |
| 53 | + |
| 54 | +## Configuration |
| 55 | + |
| 56 | +Add the following to your `.eslintrc` config: |
| 57 | + |
| 58 | +### TypeScript |
| 59 | + |
| 60 | +```jsonc |
| 61 | +{ |
| 62 | + "plugins": ["import"], |
| 63 | + "rules": { |
| 64 | + // turn on errors for missing imports |
| 65 | + "import/no-unresolved": "error", |
| 66 | + }, |
| 67 | + "settings": { |
| 68 | + "import/parsers": { |
| 69 | + "@typescript-eslint/parser": [".ts", ".tsx"], |
| 70 | + }, |
| 71 | + "import/resolver": { |
| 72 | + "x": { |
| 73 | + "alwaysTryTypes": true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist` |
| 74 | + |
| 75 | + // Choose from one of the "project" configs below or omit to use <root>/tsconfig.json by default |
| 76 | + |
| 77 | + // use <root>/path/to/folder/tsconfig.json |
| 78 | + "project": "path/to/folder", |
| 79 | + |
| 80 | + // Multiple tsconfigs (Useful for monorepos) |
| 81 | + |
| 82 | + // use a glob pattern |
| 83 | + "project": "packages/*/tsconfig.json", |
| 84 | + |
| 85 | + // use an array |
| 86 | + "project": ["packages/module-a/tsconfig.json", "packages/module-b/tsconfig.json"], |
| 87 | + |
| 88 | + // use an array of glob patterns |
| 89 | + "project": ["packages/*/tsconfig.json", "other-packages/*/tsconfig.json"], |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### JavaScript |
| 97 | + |
| 98 | +```jsonc |
| 99 | +{ |
| 100 | + "plugins": ["import"], |
| 101 | + "rules": { |
| 102 | + // turn on errors for missing imports |
| 103 | + "import/no-unresolved": "error", |
| 104 | + }, |
| 105 | + "settings": { |
| 106 | + "import/parsers": { |
| 107 | + "@babel/eslint-parser": [".js", ".mjs"], |
| 108 | + }, |
| 109 | + "import/resolver": { |
| 110 | + "x": { |
| 111 | + // use <root>/path/to/folder/jsconfig.json |
| 112 | + "project": "path/to/folder", |
| 113 | + |
| 114 | + // Multiple jsconfigs (Useful for monorepos) |
| 115 | + |
| 116 | + // use a glob pattern |
| 117 | + "project": "packages/*/jsconfig.json", |
| 118 | + |
| 119 | + // use an array |
| 120 | + "project": ["packages/module-a/jsconfig.json", "packages/module-b/jsconfig.json"], |
| 121 | + |
| 122 | + // use an array of glob patterns |
| 123 | + "project": ["packages/*/jsconfig.json", "other-packages/*/jsconfig.json"], |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## Options from [`enhanced-resolve`][] |
| 131 | + |
| 132 | +#### `conditionNames` - [See default](src/default.ts) |
| 133 | + |
| 134 | +#### `extensions` - [See default](src/default.ts) |
| 135 | + |
| 136 | +#### `extensionAlias` - [See default](src/default.ts) |
| 137 | + |
| 138 | +### `mainFields` - [See default](src/default.ts) |
| 139 | + |
| 140 | +### Other options |
| 141 | + |
| 142 | +You can pass through other options of [`enhanced-resolve`][] directly |
0 commit comments