Skip to content

Commit

Permalink
docs: document options from enhanced-resolve (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jul 19, 2022
1 parent 42f2dd6 commit 4bd60c3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-ways-yell.md
@@ -0,0 +1,5 @@
---
"eslint-import-resolver-typescript": patch
---

docs: document options from `enhanced-resolve`
116 changes: 108 additions & 8 deletions README.md
Expand Up @@ -12,21 +12,28 @@
[![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/atlassian/changesets)

This plugin adds TypeScript support to [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-plugin-import).
This plugin adds [`TypeScript`][] support to [`eslint-plugin-import`][]

This means you can:

- `import`/`require` files with extension `.cts`/`.mts`/`.ts`/`.tsx`!
- Use [`paths`](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping) defined in `tsconfig.json`.
- Prefer resolve `@types/*` definitions over plain `.js`.
- Multiple tsconfigs support just like normal.
- `exports` fields support in `package.json`
- `import`/`require` files with extension `.cts`/`.mts`/`.ts`/`.tsx`/`.d.cts`/`.d.mts`/`.d.ts`
- Use [`paths`](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping) defined in `tsconfig.json`
- Prefer resolving `@types/*` definitions over plain `.js`/`.jsx`
- Multiple tsconfigs support just like normal
- `imports/exports` fields support in `package.json`

## TOC <!-- omit in toc -->

- [Notice](#notice)
- [Installation](#installation)
- [Configuration](#configuration)
- [Options from `enhanced-resolve`](#options-from-enhanced-resolve)
- [`conditionNames`](#conditionnames)
- [`extensions`](#extensions)
- [`extensionAlias`](#extensionalias)
- [`mainFields`](#mainfields)
- [Other options](#other-options)
- [Default options](#default-options)
- [Contributing](#contributing)
- [Sponsors](#sponsors)
- [Backers](#backers)
Expand All @@ -35,9 +42,9 @@ This means you can:

## Notice

After version 2.0.0, `.d.ts` will take higher priority then normal `.js` files on resolving `node_modules` packages in favor of `@types/*` definitions.
After version 2.0.0, `.d.ts` will take higher priority then normal `.js`/`.jsx` files on resolving `node_modules` packages in favor of `@types/*` definitions or its own definition.

If you're facing some problems on rules `import/default` or `import/named` from [eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import), do not post any issue here, because they are just working exactly as [expected](https://github.com/import-js/eslint-import-resolver-typescript/issues/31#issuecomment-539751607) on our sides, take <https://github.com/benmosher/eslint-plugin-import/issues/1525> as reference or post a new issue to [eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import) instead.
If you're facing some problems on rules `import/default` or `import/named` from [`eslint-plugin-import`][], do not post any issue here, because they are just working exactly as [expected](https://github.com/import-js/eslint-import-resolver-typescript/issues/31#issuecomment-539751607) on our sides, take [import-js/eslint-plugin-import#1525](https://github.com/import-js/eslint-plugin-import/issues/1525) as reference or post a new issue to [`eslint-plugin-import`][] instead.

## Installation

Expand Down Expand Up @@ -98,6 +105,96 @@ Add the following to your `.eslintrc` config:
}
```

## Options from [`enhanced-resolve`][]

### `conditionNames`

Default:

```jsonc
[
"types",
"import",

// APF: https://angular.io/guide/angular-package-format
"esm2020",
"es2020",
"es2015",

"require",
"node",
"node-addons",
"browser",
"default"
]
```

### `extensions`

Default:

```jsonc
[
// `.mts`, `.cts`, `.d.mts`, `.d.cts`, `.mjs`, `.cjs` are not included because `.cjs` and `.mjs` must be used explicitly
".ts",
".tsx",
".d.ts",
".js",
".jsx",
".json",
".node"
]
```

### `extensionAlias`

Default:

```jsonc
{
".js": [
".ts",
// `.tsx` can also be compiled as `.js`
".tsx",
".d.ts",
".js"
],
".jsx": [".tsx", ".d.ts", ".jsx"],
".cjs": [".cts", ".d.cts", ".cjs"],
".mjs": [".mts", ".d.mts", ".mjs"]
}
```

### `mainFields`

Default:

```jsonc
[
"types",
"typings",

// APF: https://angular.io/guide/angular-package-format
"fesm2020",
"fesm2015",
"esm2020",
"es2020",

"module",
"jsnext:main",

"main"
]
```

### Other options

You can pass through other options of [`enhanced-resolve`] directly

### Default options

You can reuse `defaultConditionNames`, `defaultExtensions`, `defaultExtensionAlias` and `defaultMainFields` by `require/import` them directly

## Contributing

- Make sure your change is covered by a test import.
Expand Down Expand Up @@ -129,4 +226,7 @@ Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.m

[ISC][]

[`eslint-plugin-import`]: https://www.npmjs.com/package/eslint-plugin-import
[`enhanced-resolve`]: https://github.com/webpack/enhanced-resolve
[`typescript`]: https://www.typescriptlang.org
[isc]: https://opensource.org/licenses/ISC
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "eslint-import-resolver-typescript",
"version": "3.2.7",
"type": "module",
"description": "TypeScript `.cts`, `.mts`, `.ts`, `.tsx` module resolver for `eslint-plugin-import`",
"description": "TypeScript `.cts`/`.mts`/`.ts`/`.tsx`/`.d.cts`/`.d.mts`/`.d.ts` module resolver for `eslint-plugin-import`",
"repository": "git+https://github.com/import-js/eslint-import-resolver-typescript",
"author": "Alex Gorbatchev <alex.gorbatchev@gmail.com>",
"contributors": [
Expand Down
60 changes: 30 additions & 30 deletions src/index.ts
Expand Up @@ -27,40 +27,11 @@ export const globSync = createSyncFn<typeof import('globby').globby>(
path.resolve(_dirname, 'worker.mjs'),
)

/**
* .mts, .cts, .d.mts, .d.cts, .mjs, .cjs are not included because .cjs and .mjs must be used explicitly.
*/
export const defaultExtensions = [
'.ts',
'.tsx',
'.d.ts',
'.js',
'.jsx',
'.json',
'.node',
]

export const defaultMainFields = [
'types',
'typings',

// APF: https://angular.io/guide/angular-package-format
'fesm2020',
'fesm2015',
'esm2020',
'es2020',

'module',
'jsnext:main',

'main',
]

export const defaultConditionNames = [
'types',
'import',

// APF
// APF: https://angular.io/guide/angular-package-format
'esm2020',
'es2020',
'es2015',
Expand All @@ -72,6 +43,19 @@ export const defaultConditionNames = [
'default',
]

/**
* `.mts`, `.cts`, `.d.mts`, `.d.cts`, `.mjs`, `.cjs` are not included because `.cjs` and `.mjs` must be used explicitly
*/
export const defaultExtensions = [
'.ts',
'.tsx',
'.d.ts',
'.js',
'.jsx',
'.json',
'.node',
]

export const defaultExtensionAlias = {
'.js': [
'.ts',
Expand All @@ -85,6 +69,22 @@ export const defaultExtensionAlias = {
'.mjs': ['.mts', '.d.mts', '.mjs'],
}

export const defaultMainFields = [
'types',
'typings',

// APF: https://angular.io/guide/angular-package-format
'fesm2020',
'fesm2015',
'esm2020',
'es2020',

'module',
'jsnext:main',

'main',
]

export const interfaceVersion = 2

export interface TsResolverOptions
Expand Down

0 comments on commit 4bd60c3

Please sign in to comment.