@gramio/autoload
Version:
Autoload commands plugin for GramIO
95 lines (67 loc) • 4.87 kB
Markdown
[](https://www.npmjs.org/package/@gramio/autoload)
[](https://jsr.io/@gramio/autoload)
[](https://jsr.io/@gramio/autoload)
Autoload commands plugin for GramIO with [`Bun.build`](
> [full example](https://github.com/gramiojs/autoload/tree/main/example)
> [!IMPORTANT]
> Please read about [Lazy-load plugins](https://gramio.dev/plugins/official/autoload.html)
```ts
// index.ts
import { Bot } from "gramio";
import { autoload } from "@gramio/autoload";
const bot = new Bot(process.env.TOKEN as string)
.extend(await autoload())
.onStart(console.log);
bot.start();
export type BotType = typeof bot;
```
```ts
// commands/command.ts
import type { BotType } from "..";
export default (bot: BotType) =>
bot.command("start", (context) => context.send("hello!"));
```
| Key | Type | Default | Description |
| ----------------- | -------------------------------------------------------------------------------------------------- | -------------------------- | ------------------------------------------------------------------------- |
| pattern? | string \| string[] | "\*\*\/\*.{ts,js,cjs,mjs}" | [Glob patterns](<https://en.wikipedia.org/wiki/Glob_(programming)>) |
| path? | string | "./commands" | Path to the folder |
| import? | string \| (file: any) => string | "default" | Import a specific `export` from a file |
| failGlob? | boolean | true | Throws an error if no matches are found |
| skipImportErrors? | boolean | false | Skip imports where needed `export` not defined |
| onLoad? | (params: { absolute: string; relative: string }) => unknown | | Hook that is called when loading a file |
| onFinish? | (paths: { absolute: string; relative: string }[]) => unknown; | | Hook that is called after loading all files |
| fdir? | [Options](https://github.com/thecodrr/fdir/blob/HEAD/documentation.md#method-chaining-alternative) | | Options to configure [fdir](https://github.com/thecodrr/fdir) |
| picomatch? | [PicomatchOptions](https://github.com/micromatch/picomatch?tab=readme-ov-file#picomatch-options) | | Options to configure [picomatch](https://www.npmjs.com/package/picomatch) |
You can use this plugin with [`Bun.build`](https://bun.sh/docs/bundler), thanks to [esbuild-plugin-autoload](https://github.com/kravetsone/esbuild-plugin-autoload)!
```ts
// @filename: build.ts
import { autoload } from "esbuild-plugin-autoload"; // default import also supported
await Bun.build({
entrypoints: ["src/index.ts"],
target: "bun",
outdir: "out",
plugins: [autoload("./src/commands")],
}).then(console.log);
```
Then, build it with `bun build.ts` and run with `bun out/index.ts`.
You can bundle and then compile it into a [single executable binary file](https://bun.sh/docs/bundler/executables)
```ts
import { autoload } from "esbuild-plugin-autoload"; // default import also supported
await Bun.build({
entrypoints: ["src/index.ts"],
target: "bun",
outdir: "out",
plugins: [autoload("./src/commands")],
}).then(console.log);
await Bun.$`bun build --compile out/index.js`;
```
> [!WARNING]
> You cannot use it in `bun build --compile` mode without extra step ([Feature issue](https://github.com/oven-sh/bun/issues/11895))
[Read more](https://github.com/kravetsone/esbuild-plugin-autoload)