From 4bd60c37ab27d87be0e554d2563f0fea59fd4058 Mon Sep 17 00:00:00 2001 From: JounQin Date: Tue, 19 Jul 2022 23:56:01 +0800 Subject: [PATCH] docs: document options from `enhanced-resolve` (#156) --- .changeset/twenty-ways-yell.md | 5 ++ README.md | 116 ++++++++++++++++++++++++++++++--- package.json | 2 +- src/index.ts | 60 ++++++++--------- 4 files changed, 144 insertions(+), 39 deletions(-) create mode 100644 .changeset/twenty-ways-yell.md diff --git a/.changeset/twenty-ways-yell.md b/.changeset/twenty-ways-yell.md new file mode 100644 index 0000000..d02e9c4 --- /dev/null +++ b/.changeset/twenty-ways-yell.md @@ -0,0 +1,5 @@ +--- +"eslint-import-resolver-typescript": patch +--- + +docs: document options from `enhanced-resolve` diff --git a/README.md b/README.md index 820d10e..3cb4624 100644 --- a/README.md +++ b/README.md @@ -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 - [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) @@ -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 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 @@ -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. @@ -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 diff --git a/package.json b/package.json index 480edd1..0c1bf53 100644 --- a/package.json +++ b/package.json @@ -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 ", "contributors": [ diff --git a/src/index.ts b/src/index.ts index fdcd212..4404230 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,40 +27,11 @@ export const globSync = createSyncFn( 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', @@ -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', @@ -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