From 6a214579181b512ed4e793bebbec88c097be713a Mon Sep 17 00:00:00 2001 From: NoTwoBoy <1244476905@qq.com> Date: Fri, 22 Nov 2024 00:16:59 +0800 Subject: [PATCH] chore: enhance options --- README.md | 10 ++++++---- src/core/ctx.ts | 12 +++++++----- src/types.ts | 22 +++++++++++++++------- test/search.test.ts | 10 +++++----- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 2641e159..3d043279 100644 --- a/README.md +++ b/README.md @@ -273,8 +273,10 @@ AutoImport({ // Enable auto import by filename for default module exports under directories defaultExportByFilename: false, - // Enable auto import the types under the directories - types: true, + // Options for scanning directories for auto import + dirsScanOptions: { + types: true // Enable auto import the types under the directories + }, // Auto import for module exports under directories // by default it only scan one level of modules under the directory @@ -286,11 +288,11 @@ AutoImport({ { glob: './hooks', - types: true, // enable import the types + types: true // enable import the types }, { glob: './composables', - types: false, // If top level types importing enabled, just only disable this directory + types: false // If top level dirsScanOptions.types importing enabled, just only disable this directory } // ... ], diff --git a/src/core/ctx.ts b/src/core/ctx.ts index f2007e3a..0e5aef4b 100644 --- a/src/core/ctx.ts +++ b/src/core/ctx.ts @@ -1,5 +1,5 @@ import type { Import, InlinePreset } from 'unimport' -import type { BiomeLintrc, ESLintGlobalsPropValue, ESLintrc, ImportDir, ImportExtended, NormalizedImportDir, Options } from '../types' +import type { BiomeLintrc, ESLintGlobalsPropValue, ESLintrc, ImportExtended, NormalizedScanDir, Options, ScanDir } from '../types' import { existsSync, promises as fs } from 'node:fs' import { dirname, isAbsolute, join, relative, resolve } from 'node:path' import process from 'node:process' @@ -20,11 +20,11 @@ function resolveGlobsExclude(root: string, glob: string) { return `${excludeReg.test(glob) ? '!' : ''}${resolve(root, glob.replace(excludeReg, ''))}` } -export function normalizeImportDirs(dirs: (string | ImportDir)[], topLevelTypes = false, root = process.cwd()): NormalizedImportDir[] { +export function normalizeImportDirs(dirs: (string | ScanDir)[], topLevelTypes = false, root = process.cwd()): NormalizedScanDir[] { return dirs.map((dir) => { const isString = typeof dir === 'string' const glob = slash(resolveGlobsExclude(root, join(isString ? dir : dir.glob, '*.{tsx,jsx,ts,js,mjs,cjs,mts,cts}'))) - const types = isString ? topLevelTypes : (dir.types ?? false) + const types = isString ? topLevelTypes : (dir.types ?? topLevelTypes) return { glob, types, @@ -32,7 +32,7 @@ export function normalizeImportDirs(dirs: (string | ImportDir)[], topLevelTypes }) } -async function scanDirExports(dirs: NormalizedImportDir[], root: string) { +async function scanDirExports(dirs: NormalizedScanDir[], root: string) { const dirPatterns = dirs.map(dir => dir.glob) const result = await fg(dirPatterns, { absolute: true, @@ -53,11 +53,13 @@ export function createContext(options: Options = {}, root = process.cwd()) { const { dts: preferDTS = isPackageExists('typescript'), - types: topLevelTypes = false, + dirsScanOptions, vueDirectives, vueTemplate, } = options + const topLevelTypes = dirsScanOptions?.types ?? false + const dirs = normalizeImportDirs(options.dirs || [], topLevelTypes, root) const eslintrc: ESLintrc = options.eslintrc || {} diff --git a/src/types.ts b/src/types.ts index 828c0f93..45d80823 100644 --- a/src/types.ts +++ b/src/types.ts @@ -51,15 +51,25 @@ export type Resolver = ResolverFunction | ResolverResultObject */ export type ImportsMap = Record + +export interface ScanDirExportsOptions { + /** + * Register type exports + * + * @default true + */ + types?: boolean +} + /** * Directory to search for import */ -export interface ImportDir { +export interface ScanDir { glob: string types?: boolean } -export type NormalizedImportDir = Required +export type NormalizedScanDir = Required export type ESLintGlobalsPropValue = boolean | 'readonly' | 'readable' | 'writable' | 'writeable' @@ -129,16 +139,14 @@ export interface Options { injectAtEnd?: boolean /** - * Auto import the types - * - * @default false + * Options for scanning directories for auto import */ - types?: boolean + dirsScanOptions?: ScanDirExportsOptions /** * Path for directories to be auto imported */ - dirs?: (string | ImportDir)[] + dirs?: (string | ScanDir)[] /** * Pass a custom function to resolve the component importing path from the component name. diff --git a/test/search.test.ts b/test/search.test.ts index adb8304e..56255a84 100644 --- a/test/search.test.ts +++ b/test/search.test.ts @@ -39,7 +39,7 @@ describe('import the types from the dirs', () => { it('should top level types enable work', async () => { const ctx = createContext({ dts: false, - types: true, + dirsScanOptions: { types: true }, dirs: ['src/**'], }, root) @@ -53,7 +53,7 @@ describe('import the types from the dirs', () => { it('should specific dirs types enable work', async () => { const ctx = createContext({ dts: false, - types: false, + dirsScanOptions: { types: true }, dirs: [ { glob: 'src/views', @@ -72,11 +72,11 @@ describe('import the types from the dirs', () => { it('should specific dirs types disable work', async () => { const ctx = createContext({ dts: false, - types: true, + dirsScanOptions: { types: true }, dirs: [ - 'src/**', + 'src/types', { - glob: '!src/views', + glob: 'src/views', types: false, }, ],