Skip to content

Commit

Permalink
feat: support ignore by kind
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Sep 6, 2024
1 parent d781872 commit de68583
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ Unused({
/**
* Ignore some dependencies.
*/
ignore: ['vue'],
ignore: {
peerDependencies: ['vue'],
},
// Or ignore all kinds of dependencies.
// ignore: ['vue'],

/**
* Dependency kinds to check.
*/
Expand Down
6 changes: 4 additions & 2 deletions src/core/options.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { FilterPattern } from '@rollup/pluginutils'

export type DepKind = 'dependencies' | 'devDependencies' | 'peerDependencies'

export interface Options {
root?: string
include?: FilterPattern
exclude?: FilterPattern
ignore?: string[]
ignore?: string[] | Record<DepKind, string[]>
/**
* @default 'warning'
*/
level?: 'warning' | 'error'
/**
* @default ['dependencies', 'peerDependencies']
*/
depKinds?: Array<'dependencies' | 'devDependencies' | 'peerDependencies'>
depKinds?: Array<DepKind>
}

type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
for (const kind of options.depKinds) {
const dependencies = Object.keys(pkg[kind] || {})
for (const dep of dependencies) {
if (options.ignore.includes(dep) || deps.has(dep)) continue
const ignore = Array.isArray(options.ignore)
? options.ignore
: options.ignore[kind]
if (ignore.includes(dep) || deps.has(dep)) continue
deps.add(dep)
depsRegex[dep] = new RegExp(`["']${escapeStringRegexp(dep)}['"\\/]`)
}
Expand Down

0 comments on commit de68583

Please sign in to comment.