diff --git a/.gitignore b/.gitignore index a5defc6..d4165a5 100644 --- a/.gitignore +++ b/.gitignore @@ -81,3 +81,5 @@ dist .idea auto-imports.d.ts .eslintrc-auto-import.json + +.vscode \ No newline at end of file diff --git a/examples/vite-react/src/App.tsx b/examples/vite-react/src/App.tsx index 609290a..1e08306 100644 --- a/examples/vite-react/src/App.tsx +++ b/examples/vite-react/src/App.tsx @@ -1,7 +1,4 @@ import React from 'react' -import MainLayout from './layouts/MainLayout' -import PageA from './views/PageA' -import PageB from './views/PageB' import './i18n' function App() { diff --git a/examples/vite-react/vite.config.ts b/examples/vite-react/vite.config.ts index 9c60ad7..e4c8441 100644 --- a/examples/vite-react/vite.config.ts +++ b/examples/vite-react/vite.config.ts @@ -15,6 +15,11 @@ export default defineConfig({ AutoImport({ imports: ['react', 'react-router-dom', 'react-i18next', 'ahooks'], dts: './src/auto-imports.d.ts', + dirs: ['src/layouts', 'src/views'], + eslintrc: { + enabled: true, + }, + defaultExportByFilename: true, resolvers: [ IconsResolver({ componentPrefix: 'Icon', diff --git a/src/core/ctx.ts b/src/core/ctx.ts index 7b4ab61..f9579ed 100644 --- a/src/core/ctx.ts +++ b/src/core/ctx.ts @@ -66,7 +66,7 @@ export function createContext(options: Options = {}, root = process.cwd()) { return unimport.generateTypeDeclarations({ resolvePath: (i) => { if (i.from.startsWith('.') || isAbsolute(i.from)) { - const related = slash(relative(dir, i.from).replace(/\.ts$/, '')) + const related = slash(relative(dir, i.from).replace(/\.ts(x)?$/, '')) return !related.startsWith('.') ? `./${related}` : related @@ -112,12 +112,14 @@ export function createContext(options: Options = {}, root = process.cwd()) { async function scanDirs() { if (dirs?.length) { await unimport.modifyDynamicImports(async (imports) => { - const exports = await scanDirExports(dirs) as ImportExtended[] + const exports = await scanDirExports(dirs, { + filePatterns: ['*.{tsx,jsx,ts,js,mjs,cjs,mts,cts}'], + }) as ImportExtended[] exports.forEach(i => i.__source = 'dir') - return [ + return modifyDefaultExportsAlias([ ...imports.filter((i: ImportExtended) => i.__source !== 'dir'), ...exports, - ] as Import[] + ], options) }) } writeConfigFilesThrottled() @@ -192,3 +194,14 @@ export function flattenImports(map: Options['imports'], overriding = false): Imp return Object.values(flat) } + +function modifyDefaultExportsAlias(imports: ImportExtended[], options: Options): Import[] { + if (options.defaultExportByFilename) { + imports.forEach((i) => { + if (i.name === 'default') + i.as = i.from.split('/').pop()?.split('.')?.shift() ?? i.as + }) + } + + return imports as Import[] +} diff --git a/src/types.ts b/src/types.ts index f2cc865..62a24a5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -113,6 +113,13 @@ export interface Options { */ vueTemplate?: boolean + /** + * Set default export alias by file name + * + * @default false + */ + defaultExportByFilename?: boolean + /** * Allow overriding imports sources from multiple presets. *