bun-plugin-glob-import
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

bun-plugin-glob-import

This plugin allows you to import modules using glob patterns.

Installation

To install the plugin, run:

bun add -d bun-plugin-glob-import

Usage

As a Bundler Plugin

To use the plugin as a bundler plugin, add it to the build options:

import { globImportPlugin } from 'bun-plugin-glob-import';

await Bun.build({
  plugins: [globImportPlugin()],
});

As a Runtime Plugin

To use the plugin as a runtime plugin, register it in your bunfig.toml file:

preload = [
    "bun-plugin-glob-import/register"
]

Example

Suppose you have the following file structure:

src/
├── commands
│   ├── special
│   │   ├── list.ts
│   │   └── ping.ts
│   ├── create.ts
│   ├── delete.ts
│   ├── get.ts
│   └── update.ts
└── index.ts

The following is the code for create.ts and other commands follow a similar structure:

export default function create() {
  console.log('Created');
}

You can import files using glob patterns in your index.ts file. The way you perform the import depends on whether you are using the plugin as a bundler plugin or a runtime plugin.

// Only supported when using as a bundler plugin
import commands from './commands/**/*.ts';

// Supported for both runtime and bundler plugin usage
const { default: commands } = await import('./commands/**/*.ts');

console.log(commands);
/* => [
  { default: [Function: update] },
  { default: [Function: get] },
  { default: [Function: create] },
  { default: [Function: delete] },
  { default: [Function: list] },
  { default: [Function: ping] },
] */

Type Definitions

To avoid TypeScript errors when importing with a glob pattern, add the types to your tsconfig.json:

{
  "compilerOptions": {
    "types": ["bun-plugin-glob-import/types"]
  }
}

Package Sidebar

Install

npm i bun-plugin-glob-import

Weekly Downloads

6

Version

1.0.1

License

MIT

Unpacked Size

7.81 kB

Total Files

14

Last publish

Collaborators

  • ttempaa