From 35eb66f91601cdafd348f294a4d292385b4ce8c5 Mon Sep 17 00:00:00 2001 From: minenwerfer <80406377+minenwerfer@users.noreply.github.com> Date: Wed, 24 Jan 2024 09:16:13 -0300 Subject: [PATCH] feat: allow specifying imports to be ignored during the DTS file generation (#412) --- README.md | 15 +++++++++++++++ src/core/ctx.ts | 16 +++++++++++++--- src/types.ts | 5 +++++ test/dts.ignore.test.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 test/dts.ignore.test.ts diff --git a/README.md b/README.md index 3aa978f..5787856 100644 --- a/README.md +++ b/README.md @@ -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, @@ -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, diff --git a/src/core/ctx.ts b/src/core/ctx.ts index 8b54581..4cabaf9 100644 --- a/src/core/ctx.ts +++ b/src/core/ctx.ts @@ -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]) => { + 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 + }) + + options.ignoreDts?.forEach((name) => { + const i = imports.find(i => compare(i.as, name)) + if (i) i.dtsDisabled = true }) return unimport.getInternalContext().replaceImports(imports) diff --git a/src/types.ts b/src/types.ts index 13ff55d..2ea8ff6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 * diff --git a/test/dts.ignore.test.ts b/test/dts.ignore.test.ts new file mode 100644 index 0000000..5524918 --- /dev/null +++ b/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' + ] + }], + ignoreDts: [ + 'shouldBeIgnored', + /^ignoreme_/ + ] + }) + + 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') +})