@datarose/unplugin-auto-import-collector
v0.3.0
Published
Extension for unplugin-auto-import & Collecting exports into an array from a file
Downloads
20
Maintainers
Readme
The unplugin-auto-import is capable of globally importing variables and functions from pre-declared packages or files, allowing you to optionally assign them aliases. They have a few pre-made and maintained templates, so you don't have to individually list commonly used functions, such as those in Vue. However, what if you want to import a custom file or a package not yet supported by the community? You'd have to manually list the required functions, which can be time-consuming.
The purpose of this addon is to gather all exports from a specified file path. We can provide aliases for these exports and list a few functions that we don't need globally, so we'd like to exclude them.
Currently, the addon only works with files. However, it can also be used with packages by specifying their file paths, allowing exports to be collected from them as well.
Installation
Supports
- Vite >=5
- Node >=21
npm install unplugin-auto-import @datarose/unplugin-auto-import-collector --save-dev
Startup
vite.config.js
// vite.config.ts
import AutoImport from 'unplugin-auto-import/vite'
import { getImportsMapFrom } from '@datarose/unplugin-auto-import-collector'
export default defineConfig({
plugins: [
AutoImport({
imports: [{
// ./src/functions.(js|ts)
'src/functions': getImportsMapFrom('./src/functions.js', [
['default', 'alis_for_default'], // set alias for export default {...}
['resolve', 'alias_for_resolve'], // set alias for export const resolve = () => {...}
['TITLE', ''] // exclude TITLE
]),
/*
RESULT (that matches the configuration: https://github.com/unplugin/unplugin-auto-import?tab=readme-ov-file#configuration)
'src/functions': [
'getPrettierDate',
'myAnotherFunction',
['default', 'alis_for_default'],
['resolve', 'alias_for_resolve'],
],
*/
}],
}),
],
})
Configuration
|Name|Type|Default|Description| |----|----|-------|-----------| |path |String||Where the file is located.| |alias |String[][]|[] (empty array)|Alias names to associate with exports.If the alias name is an empty string,it is treated as an exclusion.|
Example for Alias
[
['originalName', 'aliasName'], // set alias for export const originalName
['anotherOriginalName', 'anotherAliasName'], // set alis for export function anotherOriginalName
['deprecatedFunctionName', ''], // exclude export function deprecatedFunctionName
]
How to work?
First, we collect all export (default|const|let|var|function)
declarations from the file found at the provided path
.
export default {...}
export const constName = ...
export let letName = ...
export var varName = ...
export function functionName () {...}
Next, we extract their names and put them into an array.
RESULT
['default', 'constName', 'letName', 'varName', 'functionName']
We insert the aliases into the array. (If a given alias's "originalName" is not found, that alias is not included in the result.) We rename the elements to an empty string if we want to exclude them.
Config Alias Example
[
['default', ''], // exclude
['letName', 'TITLE'], // set alias
['exampleNotFoundExport', 'setAliasForNotFoundExport'], // example of renaming a missing export
]
We filter out the empty strings marked as excluded elements from the result.
Finally, we return the generated array.
RESULT
[
'constName',
['letName', 'TITLE'],
'varName',
'functionName',
]
Early Access
The package is still very primitive, and we have many more plans for the future. We aim to be able to map both files and packages alike.
Open Source Repository
While we haven't opened the plugin's repository to the public yet, we are actively working towards doing so and fostering an active community to improve the package's quality.
License
All rights reserved as specified in the LICENSE
file! This project can be considered reusable, copyable, and/or distributable, provided that the original public repository link is included in the source code and made visible to those who use the product that incorporates this source code/package.