npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@datarose/unplugin-auto-import-collector

v0.3.0

Published

Extension for unplugin-auto-import & Collecting exports into an array from a file

Downloads

20

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.