Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ AutoImport({

// Options for scanning directories for auto import
dirsScanOptions: {
filePatterns: ['*.ts'], // Glob patterns for matching files
fileFilter: file => file.endsWith('.ts'), // Filter files
types: true // Enable auto import the types under the directories
},

Expand Down
13 changes: 2 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Arrayable, Awaitable } from '@antfu/utils'
import type { AddonVueDirectivesOptions, Import, InlinePreset, PackagePreset, UnimportOptions } from 'unimport'
import type { AddonVueDirectivesOptions, Import, InlinePreset, PackagePreset, ScanDirExportsOptions, UnimportOptions } from 'unimport'
import type { FilterPattern } from 'unplugin-utils'
import { PresetName } from './presets'

Expand Down Expand Up @@ -51,15 +51,6 @@ export type Resolver = ResolverFunction | ResolverResultObject
*/
export type ImportsMap = Record<string, (string | ImportNameAlias)[]>

export interface ScanDirExportsOptions {
/**
* Register type exports
*
* @default true
*/
types?: boolean
}

/**
* Directory to search for import
*/
Expand Down Expand Up @@ -140,7 +131,7 @@ export interface Options {
/**
* Options for scanning directories for auto import
*/
dirsScanOptions?: ScanDirExportsOptions
dirsScanOptions?: Omit<ScanDirExportsOptions, 'cwd'>

/**
* Path for directories to be auto imported
Expand Down
112 changes: 111 additions & 1 deletion test/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('search', () => {
})
})

describe('import the types from the dirs', () => {
describe('dirsScanOptions', () => {
it('should top level types enable work', async () => {
const ctx = createContext({
dts: false,
Expand Down Expand Up @@ -88,4 +88,114 @@ describe('import the types from the dirs', () => {
expect(data).not.toContain('TypeB')
expect(data).toContain('SpecialType')
})

it('should filePatterns work', async () => {
const ctx = createContext({
dts: false,
dirsScanOptions: {
filePatterns: ['*.{ts,tsx}'],
},
dirs: [
'src/views',
'src/types',
],
}, root)

await ctx.scanDirs()
const data = await ctx.generateDTS('')
expect(data).toContain('PageA')
expect(data).toContain('PageB')
expect(data).toContain('TypeA')
expect(data).toContain('TypeB')
expect(data).toContain('SpecialType')
})

it('should specific filePatterns work', async () => {
const ctx = createContext({
dts: false,
dirsScanOptions: {
types: true,
filePatterns: ['*.ts'],
},
dirs: [
'src/views',
'src/types',
],
}, root)

await ctx.scanDirs()
const data = await ctx.generateDTS('')
expect(data).not.toContain('PageA')
expect(data).not.toContain('PageB')
expect(data).not.toContain('TypeA')
expect(data).not.toContain('TypeB')
expect(data).toContain('SpecialType')
})

it('should fileFilter work', async () => {
const ctx = createContext({
dts: false,
dirsScanOptions: {
types: true,
fileFilter: (file: string) => file.includes('TypeA') || file.includes('PageA'),
},
dirs: [
'src/views',
'src/types',
],
}, root)

await ctx.scanDirs()
const data = await ctx.generateDTS('')
expect(data).toContain('TypeA')
expect(data).toContain('PageA')
expect(data).not.toContain('TypeB')
expect(data).not.toContain('PageB')
expect(data).not.toContain('SpecialType')
})

it('should fileFilter work when excluding all', async () => {
const ctx = createContext({
dts: false,
dirsScanOptions: {
types: true,
fileFilter: (_file: string) => false,
},
dirs: [
'src/views',
'src/types',
],
}, root)

await ctx.scanDirs()
const data = await ctx.generateDTS('')
expect(data).not.toContain('TypeA')
expect(data).not.toContain('PageA')
expect(data).not.toContain('TypeB')
expect(data).not.toContain('PageB')
expect(data).not.toContain('SpecialType')
})

it('should filePatterns and fileFilter work together', async () => {
const ctx = createContext({
dts: false,
dirsScanOptions: {
types: true,
filePatterns: ['*.{ts,tsx}'],
fileFilter: (file: string) => file.includes('PageA'),
},
dirs: [
'src/views',
'src/types',
],
}, root)

await ctx.scanDirs()
const data = await ctx.generateDTS('')
expect(data).toContain('PageA')
expect(data).toContain('TypeA')
expect(data).not.toContain('SpecialType')
expect(data).not.toContain('PageB')
expect(data).not.toContain('TypeB')
})
})