From 080711520a8c653febb74a14d31cbadcbf58c79c Mon Sep 17 00:00:00 2001 From: Vida Xie <38204901+9romise@users.noreply.github.com> Date: Sun, 30 Jun 2024 13:40:36 +0800 Subject: [PATCH] chore: update README about default options --- README.md | 6 ++-- src/index.ts | 80 +----------------------------------------- src/nomalizeOptions.ts | 80 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 82 deletions(-) create mode 100644 src/nomalizeOptions.ts diff --git a/README.md b/README.md index 3383e0b..c8f65f2 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,11 @@ Inside your `eslint.config.js`, pass the resolver to `eslint-plugin-import-x`: } ``` -This lib defaults to ESM, if you use CJS(not recommended), please wait for the subsequent release. Or you can configure it yourself according to the [documentation](https://github.com/oxc-project/oxc-resolver?tab=readme-ov-file#options). - ### Options -see [oxc-resolver](https://github.com/oxc-project/oxc-resolver?tab=readme-ov-file#options) +Default options see [normalizeOptions.ts](./src/nomalizeOptions.ts) + +More info see [oxc-resolver](https://github.com/oxc-project/oxc-resolver?tab=readme-ov-file#options) ## Motivation diff --git a/src/index.ts b/src/index.ts index c05891c..257173e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,84 +1,6 @@ -import { cwd } from 'node:process' import type { NapiResolveOptions } from 'oxc-resolver' import { ResolverFactory } from 'oxc-resolver' - -// @keep-sorted -/** - * some default options copy from - * https://github.com/import-js/eslint-import-resolver-typescript/blob/master/src/index.ts - * https://github.com/rolldown/rolldown/blob/main/crates/rolldown_resolver/src/resolver.rs - */ -const defaultOptions: NapiResolveOptions = { - aliasFields: [ - ['browser'], - ], - conditionNames: [ - 'default', - 'types', - - 'import', - 'require', - - 'node', - 'node-addons', - 'browser', - - // APF: https://angular.io/guide/angular-package-format - 'esm2020', - 'es2020', - 'es2015', - ], - extensionAlias: { - '.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'], - }, - extensions: [ - '.ts', - '.tsx', - '.d.ts', - '.js', - '.jsx', - '.json', - '.node', - ], - mainFields: [ - 'types', - 'typings', - - // APF: https://angular.io/guide/angular-package-format - 'fesm2020', - 'fesm2015', - 'esm2020', - 'es2020', - - 'main', - 'module', - 'browser', - 'jsnext:main', - ], - roots: [cwd()], -} - -function normalizeOptions(options: NapiResolveOptions): NapiResolveOptions { - if (!options.tsconfig) { - defaultOptions.tsconfig = { - configFile: './tsconfig.json', - references: 'auto', - } - } - return { - ...defaultOptions, - ...options, - } -} +import { normalizeOptions } from './nomalizeOptions' let resolver: ResolverFactory | undefined export function resolve(source: string, file: string, options: NapiResolveOptions = {}): { found: boolean, path?: string } { diff --git a/src/nomalizeOptions.ts b/src/nomalizeOptions.ts new file mode 100644 index 0000000..76aee4c --- /dev/null +++ b/src/nomalizeOptions.ts @@ -0,0 +1,80 @@ +import { cwd } from 'node:process' +import type { NapiResolveOptions } from 'oxc-resolver' + +// @keep-sorted +/** + * some default options copy from + * https://github.com/import-js/eslint-import-resolver-typescript/blob/master/src/index.ts + * https://github.com/rolldown/rolldown/blob/main/crates/rolldown_resolver/src/resolver.rs + */ +const defaultOptions: NapiResolveOptions = { + aliasFields: [ + ['browser'], + ], + conditionNames: [ + 'default', + 'types', + + 'import', + 'require', + + 'node', + 'node-addons', + 'browser', + + // APF: https://angular.io/guide/angular-package-format + 'esm2020', + 'es2020', + 'es2015', + ], + extensionAlias: { + '.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'], + }, + extensions: [ + '.ts', + '.tsx', + '.d.ts', + '.js', + '.jsx', + '.json', + '.node', + ], + mainFields: [ + 'types', + 'typings', + + // APF: https://angular.io/guide/angular-package-format + 'fesm2020', + 'fesm2015', + 'esm2020', + 'es2020', + + 'main', + 'module', + 'browser', + 'jsnext:main', + ], + roots: [cwd()], +} + +export function normalizeOptions(options: NapiResolveOptions = {}): NapiResolveOptions { + if (!options.tsconfig) { + defaultOptions.tsconfig = { + configFile: './tsconfig.json', + references: 'auto', + } + } + return { + ...defaultOptions, + ...options, + } +}