Skip to content

Commit

Permalink
feat: support top level switch to enable/disable auto import types
Browse files Browse the repository at this point in the history
  • Loading branch information
noootwo committed Nov 20, 2024
1 parent a5765d9 commit 49c2042
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ AutoImport({
// Enable auto import by filename for default module exports under directories
defaultExportByFilename: false,

// Enable auto import the types under the directories
types: true,

// Auto import for module exports under directories
// by default it only scan one level of modules under the directory
dirs: [
Expand All @@ -283,8 +286,13 @@ AutoImport({

{
glob: './hooks',
includeTypes: true, // And export the types
types: true, // enable import the types
},
{
glob: './composables',
types: false, // If top level types importing enabled, just only disable this directory
}
// ...
],

// Filepath to generate corresponding .d.ts file.
Expand Down
1 change: 1 addition & 0 deletions examples/vite-react/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type SpecialType = string;
2 changes: 2 additions & 0 deletions examples/vite-react/src/views/PageB.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ function PageB() {
)
}

export type TypeB = number;

export default PageB
11 changes: 6 additions & 5 deletions src/core/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ function resolveGlobsExclude(root: string, glob: string) {
return `${excludeReg.test(glob) ? '!' : ''}${resolve(root, glob.replace(excludeReg, ''))}`
}

export function normalizeImportDirs(dirs: (string | ImportDir)[], root = process.cwd()): NormalizedImportDir[] {
export function normalizeImportDirs(dirs: (string | ImportDir)[], topLevelTypes = false, root = process.cwd()): NormalizedImportDir[] {
return dirs.map((dir) => {
const isString = typeof dir === 'string'
const glob = slash(resolveGlobsExclude(root, join(isString ? dir : dir.glob, '*.{tsx,jsx,ts,js,mjs,cjs,mts,cts}')))
const includeTypes = isString ? false : (dir.includeTypes ?? false)
const types = isString ? topLevelTypes : (dir.types ?? false)
return {
glob,
includeTypes,
types,
}
})
}
Expand All @@ -41,7 +41,7 @@ async function scanDirExports(dirs: NormalizedImportDir[], root: string) {
followSymbolicLinks: true,
})

const includeTypesDirs = dirs.filter(dir => !excludeReg.test(dir.glob) && dir.includeTypes)
const includeTypesDirs = dirs.filter(dir => !excludeReg.test(dir.glob) && dir.types)
const isIncludeTypes = (file: string) => includeTypesDirs.some(dir => minimatch(slash(file), slash(dir.glob)))

const files = Array.from(new Set(result.flat())).map(slash)
Expand All @@ -53,11 +53,12 @@ export function createContext(options: Options = {}, root = process.cwd()) {

const {
dts: preferDTS = isPackageExists('typescript'),
types: topLevelTypes = false,
vueDirectives,
vueTemplate,
} = options

const dirs = normalizeImportDirs(options.dirs || [], root)
const dirs = normalizeImportDirs(options.dirs || [], topLevelTypes, root)

const eslintrc: ESLintrc = options.eslintrc || {}
eslintrc.enabled = eslintrc.enabled === undefined ? false : eslintrc.enabled
Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type ImportsMap = Record<string, (string | ImportNameAlias)[]>
*/
export interface ImportDir {
glob: string
includeTypes?: boolean
types?: boolean
}

export type NormalizedImportDir = Required<ImportDir>
Expand Down Expand Up @@ -128,6 +128,13 @@ export interface Options {
*/
injectAtEnd?: boolean

/**
* Auto import the types
*
* @default false
*/
types?: boolean

/**
* Path for directories to be auto imported
*/
Expand Down
41 changes: 39 additions & 2 deletions test/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,56 @@ describe('search', () => {
})

describe('import the types from the dirs', () => {
it('should import types work', async () => {
it('should top level types enable work', async () => {
const ctx = createContext({
dts: false,
types: true,
dirs: ['src/**'],
}, root)

await ctx.scanDirs()
const data = await ctx.generateDTS('')
expect(data).toContain('TypeA')
expect(data).toContain('TypeB')
expect(data).toContain('SpecialType')
})

it('should specific dirs types enable work', async () => {
const ctx = createContext({
dts: false,
types: false,
dirs: [
{
glob: 'src/views',
includeTypes: true,
types: true,
},
],
}, root)

await ctx.scanDirs()
const data = await ctx.generateDTS('')
expect(data).toContain('TypeA')
expect(data).toContain('TypeB')
expect(data).not.toContain('SpecialType')
})

it('should specific dirs types disable work', async () => {
const ctx = createContext({
dts: false,
types: true,
dirs: [
'src/**',
{
glob: '!src/views',
types: false,
},
],
}, root)

await ctx.scanDirs()
const data = await ctx.generateDTS('')
expect(data).not.toContain('TypeA')
expect(data).not.toContain('TypeB')
expect(data).toContain('SpecialType')
})
})

0 comments on commit 49c2042

Please sign in to comment.