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

unplugin-export-collector

v0.4.3

Published

Recursively get all named export from a file and out-of-the-box support unplugin-auto-import

Downloads

347

Readme

:tada: unplugin-export-collector

English | 简体中文

ESM export collector with out-of-the-box support unplugin-auto-import.

:hammer: Install

$ pnpm i -D unplugin-export-collector

:rocket: Feature

Recursively collect all named exports from an ESM file.

Not support re-export from a alias path now. Like export * from '@core/index'.

Consumed in src/index.ts is:

// src/index.ts
export const one = 1
export * from './func1' // export from another file.
export * from 'vue' // reExport from deps will be ignored.

in src/func1.ts is:

// src/func1.ts
function func1() {}
export { func1 as funcRe }

Just get named export list:

import { expCollector } from 'unplugin-export-collector/core'

console.log(await expCollector('./src/index.ts'))
// ['one', 'funcRe']

Or support unplugin-auto-import. In src/index.ts after building:

// src/index.ts
export const one = 1
export * from './func1' // export from another file.
export * from 'vue' // reExport from deps will be ignored.

// --- Auto-Generated By Unplugin-Export-Collector ---

const __UnExportList = ['one', 'funcRe'] as const

export function autoImport(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>): Record<string, (string | [string, string])[]> {
  return (name: string) => {
    if (!__UnExportList.includes(name as any))
      return

    return map && (map as any)[name]
      ? {
          name,
          as: (map as any)[name],
          from: 'unplugin-export-collector',
        }
      : {
          name,
          from: 'unplugin-export-collector',
        }
  }
}

// --- Auto-Generated By Unplugin-Export-Collector ---

Use the value in project.

import { defineConfig } from 'vite'
import { autoImport as Resolver } from 'my-project'

export default defineConfig({
  plugins: [
    AutoImport({
      resolvers: [
        Resolver(),
      ],
    }),
  ]
})

:wrench: Usage

More details see the unit test in test folder.

Generate autoImport function

// vite.config.ts
import ExportCollector from 'unplugin-export-collector/vite'

export default defineConfig({
  plugins: [
    ExportCollector({ /* options */ }),
  ],
})

// rollup.config.js
import ExportCollector from 'unplugin-export-collector/rollup'

export default {
  plugins: [
    ExportCollector({ /* options */ }),
    // other plugins
  ],
}

// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-export-collector/webpack').default({ /* options */ }),
  ],
}

// esbuild.config.js
import { build } from 'esbuild'
import ExportCollector from 'unplugin-export-collector/esbuild'

build({
  /* ... */
  plugins: [
    ExportCollector({
      /* options */
    }),
  ],
})

Options:

Please move to the type declaration, all options are well commented.

Just get export list

Use like :

import { expCollector } from 'unplugin-export-collector/core'

const val = await expCollector('./src/index.ts') // base on root as default.

console.log(val)
// ['one', 'funcRe']

Or customize the base path.

// ...
const val = await expCollector(
  './index.ts',
  fileURLToPath(new URL('./src/index.ts', import.meta.url))
)
// the value will be same as above example.

The core exports a series of methods, briefly described as follows:

  • expGenerator: Reads a file, generates the autoImport method, and writes it.
  • expGeneratorData: Reads a file but does not write, returns the string of the autoImport method.
  • expCollector: Reads a file, returns an array of named exports.
  • parser: Reads a string, returns an array of named exports at one level and an array of re-export paths.