Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support to import the types from the dirs #544

Merged
merged 7 commits into from
Dec 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: support top level switch to enable/disable auto import types
noootwo committed Nov 29, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 1779bd78d2d3e7557d82ecd4baf24ae24bc0a354
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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: [
@@ -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.
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
@@ -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
@@ -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,
}
})
}
@@ -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)
@@ -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
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -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>
@@ -128,6 +128,13 @@ export interface Options {
*/
injectAtEnd?: boolean

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

/**
* Path for directories to be auto imported
*/
41 changes: 39 additions & 2 deletions test/search.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})