unplugin-auto-import icon indicating copy to clipboard operation
unplugin-auto-import copied to clipboard

Namespace dirs imports

Open DaPotatoMan opened this issue 2 years ago • 1 comments

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.

DaPotatoMan avatar Sep 13 '22 07:09 DaPotatoMan

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']
}

ddosakura avatar Feb 08 '23 03:02 ddosakura