Skip to content

Commit f48c980

Browse files
authored
feat: use ScanDirExportsOptions from unimport (#570)
1 parent f73f86a commit f48c980

File tree

3 files changed

+115
-12
lines changed

3 files changed

+115
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ AutoImport({
276276

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

src/types.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Arrayable, Awaitable } from '@antfu/utils'
2-
import type { AddonVueDirectivesOptions, Import, InlinePreset, PackagePreset, UnimportOptions } from 'unimport'
2+
import type { AddonVueDirectivesOptions, Import, InlinePreset, PackagePreset, ScanDirExportsOptions, UnimportOptions } from 'unimport'
33
import type { FilterPattern } from 'unplugin-utils'
44
import type { PresetName } from './presets'
55

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

54-
export interface ScanDirExportsOptions {
55-
/**
56-
* Register type exports
57-
*
58-
* @default true
59-
*/
60-
types?: boolean
61-
}
62-
6354
/**
6455
* Directory to search for import
6556
*/
@@ -140,7 +131,7 @@ export interface Options {
140131
/**
141132
* Options for scanning directories for auto import
142133
*/
143-
dirsScanOptions?: ScanDirExportsOptions
134+
dirsScanOptions?: Omit<ScanDirExportsOptions, 'cwd'>
144135

145136
/**
146137
* Path for directories to be auto imported

test/search.test.ts

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('search', () => {
3535
})
3636
})
3737

38-
describe('import the types from the dirs', () => {
38+
describe('dirsScanOptions', () => {
3939
it('should top level types enable work', async () => {
4040
const ctx = createContext({
4141
dts: false,
@@ -88,4 +88,114 @@ describe('import the types from the dirs', () => {
8888
expect(data).not.toContain('TypeB')
8989
expect(data).toContain('SpecialType')
9090
})
91+
92+
it('should filePatterns work', async () => {
93+
const ctx = createContext({
94+
dts: false,
95+
dirsScanOptions: {
96+
filePatterns: ['*.{ts,tsx}'],
97+
},
98+
dirs: [
99+
'src/views',
100+
'src/types',
101+
],
102+
}, root)
103+
104+
await ctx.scanDirs()
105+
const data = await ctx.generateDTS('')
106+
expect(data).toContain('PageA')
107+
expect(data).toContain('PageB')
108+
expect(data).toContain('TypeA')
109+
expect(data).toContain('TypeB')
110+
expect(data).toContain('SpecialType')
111+
})
112+
113+
it('should specific filePatterns work', async () => {
114+
const ctx = createContext({
115+
dts: false,
116+
dirsScanOptions: {
117+
types: true,
118+
filePatterns: ['*.ts'],
119+
},
120+
dirs: [
121+
'src/views',
122+
'src/types',
123+
],
124+
}, root)
125+
126+
await ctx.scanDirs()
127+
const data = await ctx.generateDTS('')
128+
expect(data).not.toContain('PageA')
129+
expect(data).not.toContain('PageB')
130+
expect(data).not.toContain('TypeA')
131+
expect(data).not.toContain('TypeB')
132+
expect(data).toContain('SpecialType')
133+
})
134+
135+
it('should fileFilter work', async () => {
136+
const ctx = createContext({
137+
dts: false,
138+
dirsScanOptions: {
139+
types: true,
140+
fileFilter: (file: string) => file.includes('TypeA') || file.includes('PageA'),
141+
},
142+
dirs: [
143+
'src/views',
144+
'src/types',
145+
],
146+
}, root)
147+
148+
await ctx.scanDirs()
149+
const data = await ctx.generateDTS('')
150+
expect(data).toContain('TypeA')
151+
expect(data).toContain('PageA')
152+
expect(data).not.toContain('TypeB')
153+
expect(data).not.toContain('PageB')
154+
expect(data).not.toContain('SpecialType')
155+
})
156+
157+
it('should fileFilter work when excluding all', async () => {
158+
const ctx = createContext({
159+
dts: false,
160+
dirsScanOptions: {
161+
types: true,
162+
fileFilter: (_file: string) => false,
163+
},
164+
dirs: [
165+
'src/views',
166+
'src/types',
167+
],
168+
}, root)
169+
170+
await ctx.scanDirs()
171+
const data = await ctx.generateDTS('')
172+
expect(data).not.toContain('TypeA')
173+
expect(data).not.toContain('PageA')
174+
expect(data).not.toContain('TypeB')
175+
expect(data).not.toContain('PageB')
176+
expect(data).not.toContain('SpecialType')
177+
})
178+
179+
it('should filePatterns and fileFilter work together', async () => {
180+
const ctx = createContext({
181+
dts: false,
182+
dirsScanOptions: {
183+
types: true,
184+
filePatterns: ['*.{ts,tsx}'],
185+
fileFilter: (file: string) => file.includes('PageA'),
186+
},
187+
dirs: [
188+
'src/views',
189+
'src/types',
190+
],
191+
}, root)
192+
193+
await ctx.scanDirs()
194+
const data = await ctx.generateDTS('')
195+
expect(data).toContain('PageA')
196+
expect(data).toContain('TypeA')
197+
expect(data).not.toContain('SpecialType')
198+
expect(data).not.toContain('PageB')
199+
expect(data).not.toContain('TypeB')
200+
})
91201
})

0 commit comments

Comments
 (0)