This plugin allows you to import modules using glob patterns.
To install the plugin, run:
bun add -d bun-plugin-glob-import
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()],
});
To use the plugin as a runtime plugin, register it in your bunfig.toml
file:
preload = [
"bun-plugin-glob-import/register"
]
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] },
] */
To avoid TypeScript errors when importing with a glob pattern, add the types to your tsconfig.json
:
{
"compilerOptions": {
"types": ["bun-plugin-glob-import/types"]
}
}