unplugin-auto-import
unplugin-auto-import copied to clipboard
Namespace dirs imports
Clear and concise description of the problem
Namespace all imports from a directory. Instead of using foo()
, use api.foo()
to import functions.
Why -
- Avoid name collisions
- Tree shaking
Suggested solution
Config:
dirs: [
'src/logic/composables',
['api', 'src/logic/requests']
]
src/logic/requests/index.ts
export function fetchBlogs() {}
export function fetchProducts() {}
Then in project files:
api.fetchBlogs()
api.fetchProducts()
Under the hood:
export function fetchBlogs() {}
export function fetchProducts() {}
import { fetchBlogs as api_fetchBlogs, fetchProducts as api_fetchProducts } from '...'
api_fetchBlogs()
api_fetchProducts()
Alternative
No response
Additional context
No response
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guide.
- [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
you can try this:
config:
dirs: [ 'src/apis/']
src/apis/index.ts
:
import * as api from "...";
export { api };
auto-imports.d.ts
:
// Generated by 'unplugin-auto-import'
export {}
declare global {
const api: typeof import('./src/apis/index')['api']
}