Skip to content

Commit

Permalink
feat(scan-dirs): support exclude glob and set dirs exclude types (#395)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <github@antfu.me>
  • Loading branch information
noootwo and antfu authored Nov 29, 2024
1 parent eebdfa7 commit 5978277
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 76 deletions.
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,40 @@ Unimport.vite({
### Directory Auto Import

```ts
dirs: [
'./composables/*'
]
Unimport.vite({
dirs: [
'./composables/**/*',
{
glob: './composables/nested/**/*',
types: false // disable scan the type declarations
}
]
})
```

Named exports for modules under `./composables/*` will be registered for auto imports.
Named exports for modules under `./composables/**/*` will be registered for auto imports, and filter out the types in `./composables/nested/**/*`.

#### Directory Scan Options

You can also provide custom options for directory scan, for example:

```ts
Unimport.vite({
dirsScanOptions: {
filePatterns: ['*.ts'], // optional, default `['*.{ts,js,mjs,cjs,mts,cts}']`, glob patterns for matching files
fileFilter: file => file.endsWith('.ts'), // optional, default `() => true`, filter files
types: true, // optional, default `true`, enable/disable scan the type declarations
cwd: process.cwd(), // optional, default `process.cwd()`, custom cwd for directory scan
},
dirs: [
'./composables/**/*',
{
glob: './composables/nested/**/*',
types: false
}
]
})
```

### Opt-out Auto Import

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"fast-glob": "catalog:",
"local-pkg": "catalog:",
"magic-string": "catalog:",
"micromatch": "catalog:",
"mlly": "catalog:",
"pathe": "catalog:",
"pkg-types": "catalog:",
Expand All @@ -62,6 +63,7 @@
"devDependencies": {
"@antfu/eslint-config": "catalog:",
"@types/estree": "catalog:",
"@types/micromatch": "^4.0.9",
"@types/node": "catalog:",
"@vitest/coverage-v8": "catalog:",
"bumpp": "catalog:",
Expand Down
2 changes: 2 additions & 0 deletions playground/composables/nested/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export default function () {
return 'from nested composables'
}

export type CustomType3 = string
51 changes: 36 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ catalog:
lit: ^3.2.1
local-pkg: ^0.5.1
magic-string: ^0.30.14
micromatch: ^4.0.8
mlly: ^1.7.3
pathe: ^1.1.2
pkg-types: ^1.2.1
Expand Down
10 changes: 4 additions & 6 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
import { version } from '../package.json'
import { configureAddons } from './addons/addons'
import { detectImports } from './detect'
import { dedupeDtsExports, scanExports, scanFilesFromDir } from './node/scan-dirs'
import { scanDirExports, scanExports } from './node/scan-dirs'
import { resolveBuiltinPresets } from './preset'
import { addImportToCode, dedupeImports, getMagicString, normalizeImports, stripFileExtension, toExports, toTypeDeclarationFile, toTypeReExports } from './utils'

Expand Down Expand Up @@ -48,11 +48,9 @@ export function createUnimport(opts: Partial<UnimportOptions>): Unimport {
}

async function scanImportsFromDir(dirs = ctx.options.dirs || [], options = ctx.options.dirsScanOptions) {
const files = await scanFilesFromDir(dirs, options)
const includeTypes = options?.types ?? true
const imports = (await Promise.all(files.map(dir => scanExports(dir, includeTypes)))).flat()
const deduped = dedupeDtsExports(imports)
await ctx.modifyDynamicImports(imports => imports.filter(i => !files.includes(i.from)).concat(deduped))
const imports = await scanDirExports(dirs, options)
const files = new Set(imports.map(f => f.from))
await ctx.modifyDynamicImports(i => i.filter(i => !files.has(i.from)).concat(imports))
return imports
}

Expand Down
Loading

0 comments on commit 5978277

Please sign in to comment.