Giter VIP home page Giter VIP logo

unplugin-export-collector's Introduction

๐ŸŽ‰ unplugin-export-collector

English | ็ฎ€ไฝ“ไธญๆ–‡

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

๐Ÿ”จ Install

$ pnpm i -D unplugin-export-collector

๐Ÿš€ 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(),
      ],
    }),
  ]
})

๐Ÿ”ง Usage

More details see the unit test in test folder.

Generate autoImport function

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

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


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

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


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


esbuild
// 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.

unplugin-export-collector's People

Contributors

s3xysteak avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.