Skip to content

Commit 35eb66f

Browse files
authoredJan 24, 2024
feat: allow specifying imports to be ignored during the DTS file generation (#412)
1 parent c6b86f5 commit 35eb66f

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed
 

‎README.md

+15
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ AutoImport({
263263
type: true,
264264
},
265265
],
266+
267+
// Array of strings of regexes that contains imports meant to be filtered out.
268+
ignore: [
269+
'useMouse',
270+
'useFetch'
271+
],
272+
266273
// Enable auto import by filename for default module exports under directories
267274
defaultExportByFilename: false,
268275

@@ -280,6 +287,14 @@ AutoImport({
280287
// Set `false` to disable.
281288
dts: './auto-imports.d.ts',
282289

290+
// Array of strings of regexes that contains imports meant to be ignored during
291+
// the declaration file generation. You may find this useful when you need to provide
292+
// a custom signature for a function.
293+
ignoreDts: [
294+
'ignoredFunction',
295+
/^ignore_/
296+
],
297+
283298
// Auto import inside Vue template
284299
// see https://github.com/unjs/unimport/pull/15 and https://github.com/unjs/unimport/pull/72
285300
vueTemplate: false,

‎src/core/ctx.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,20 @@ ${dts}`.trim()}\n`
8080
if (!imports.length && !resolvers.length && !dirs?.length)
8181
console.warn('[auto-import] plugin installed but no imports has defined, see https://github.com/antfu/unplugin-auto-import#configurations for configurations')
8282

83+
const compare = (left: string|undefined, right: NonNullable<(Options['ignore'] | Options['ignoreDts'])>[number]) => {
84+
return right instanceof RegExp
85+
? right.test(left!)
86+
: right === left
87+
}
88+
8389
options.ignore?.forEach((name) => {
84-
const i = imports.find(i => i.as === name)
85-
if (i)
86-
i.disabled = true
90+
const i = imports.find(i => compare(i.as, name))
91+
if (i) i.disabled = true
92+
})
93+
94+
options.ignoreDts?.forEach((name) => {
95+
const i = imports.find(i => compare(i.as, name))
96+
if (i) i.dtsDisabled = true
8797
})
8898

8999
return unimport.getInternalContext().replaceImports(imports)

‎src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ export interface Options {
9393
*/
9494
ignore?: (string | RegExp)[]
9595

96+
/**
97+
* These identifiers won't be put on the DTS file
98+
*/
99+
ignoreDts?: (string | RegExp)[]
100+
96101
/**
97102
* Inject the imports at the end of other imports
98103
*

‎test/dts.ignore.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { join } from 'node:path'
2+
import { expect, it } from 'vitest'
3+
import { createContext } from '../src/core/ctx'
4+
5+
it('dts ignore', async () => {
6+
const cwd = process.cwd()
7+
const ctx = createContext({
8+
imports: [{
9+
custom: [
10+
'shouldBePresent',
11+
'shouldAlsoBePresent',
12+
'shouldBeIgnored',
13+
'ignoreme_shoudAlsoBeIgnored'
14+
]
15+
}],
16+
ignoreDts: [
17+
'shouldBeIgnored',
18+
/^ignoreme_/
19+
]
20+
})
21+
22+
const dtsContent = await ctx.generateDTS(join(cwd, 'index.d.ts'))
23+
24+
expect(dtsContent).toContain('shouldBePresent')
25+
expect(dtsContent).toContain('shouldAlsoBePresent')
26+
expect(dtsContent).not.toContain('shouldBeIgnored')
27+
expect(dtsContent).not.toContain('ignoreme_shoudAlsoBeIgnored')
28+
})

0 commit comments

Comments
 (0)
Please sign in to comment.