Skip to content

Commit

Permalink
feat: allow specifying imports to be ignored during the DTS file gene…
Browse files Browse the repository at this point in the history
…ration (#412)
  • Loading branch information
minenwerfer committed Jan 24, 2024
1 parent c6b86f5 commit 35eb66f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -263,6 +263,13 @@ AutoImport({
type: true,
},
],

// Array of strings of regexes that contains imports meant to be filtered out.
ignore: [
'useMouse',
'useFetch'
],

// Enable auto import by filename for default module exports under directories
defaultExportByFilename: false,

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

// Array of strings of regexes that contains imports meant to be ignored during
// the declaration file generation. You may find this useful when you need to provide
// a custom signature for a function.
ignoreDts: [
'ignoredFunction',
/^ignore_/
],

// Auto import inside Vue template
// see https://github.com/unjs/unimport/pull/15 and https://github.com/unjs/unimport/pull/72
vueTemplate: false,
Expand Down
16 changes: 13 additions & 3 deletions src/core/ctx.ts
Expand Up @@ -80,10 +80,20 @@ ${dts}`.trim()}\n`
if (!imports.length && !resolvers.length && !dirs?.length)
console.warn('[auto-import] plugin installed but no imports has defined, see https://github.com/antfu/unplugin-auto-import#configurations for configurations')

const compare = (left: string|undefined, right: NonNullable<(Options['ignore'] | Options['ignoreDts'])>[number]) => {

Check failure on line 83 in src/core/ctx.ts

View workflow job for this annotation

GitHub Actions / lint

Operator '|' must be spaced
return right instanceof RegExp
? right.test(left!)
: right === left
}

options.ignore?.forEach((name) => {
const i = imports.find(i => i.as === name)
if (i)
i.disabled = true
const i = imports.find(i => compare(i.as, name))
if (i) i.disabled = true

Check failure on line 91 in src/core/ctx.ts

View workflow job for this annotation

GitHub Actions / lint

Expect newline after if
})

options.ignoreDts?.forEach((name) => {
const i = imports.find(i => compare(i.as, name))
if (i) i.dtsDisabled = true

Check failure on line 96 in src/core/ctx.ts

View workflow job for this annotation

GitHub Actions / lint

Expect newline after if
})

return unimport.getInternalContext().replaceImports(imports)
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Expand Up @@ -93,6 +93,11 @@ export interface Options {
*/
ignore?: (string | RegExp)[]

/**
* These identifiers won't be put on the DTS file
*/
ignoreDts?: (string | RegExp)[]

/**
* Inject the imports at the end of other imports
*
Expand Down
28 changes: 28 additions & 0 deletions test/dts.ignore.test.ts
@@ -0,0 +1,28 @@
import { join } from 'node:path'
import { expect, it } from 'vitest'
import { createContext } from '../src/core/ctx'

it('dts ignore', async () => {
const cwd = process.cwd()
const ctx = createContext({
imports: [{
custom: [
'shouldBePresent',
'shouldAlsoBePresent',
'shouldBeIgnored',
'ignoreme_shoudAlsoBeIgnored'

Check failure on line 13 in test/dts.ignore.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
]

Check failure on line 14 in test/dts.ignore.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
}],
ignoreDts: [
'shouldBeIgnored',
/^ignoreme_/

Check failure on line 18 in test/dts.ignore.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
]

Check failure on line 19 in test/dts.ignore.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
})

const dtsContent = await ctx.generateDTS(join(cwd, 'index.d.ts'))

expect(dtsContent).toContain('shouldBePresent')
expect(dtsContent).toContain('shouldAlsoBePresent')
expect(dtsContent).not.toContain('shouldBeIgnored')
expect(dtsContent).not.toContain('ignoreme_shoudAlsoBeIgnored')
})

0 comments on commit 35eb66f

Please sign in to comment.