Autoload commands plugin for GramIO with Bun.build
support.
Important
Please read about Lazy-load plugins
// 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;
// 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 |
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 | Options to configure fdir | |
picomatch? | PicomatchOptions | Options to configure picomatch |
Bun build usage
You can use this plugin with Bun.build
, thanks to esbuild-plugin-autoload!
// @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
.
Bun compile usage
You can bundle and then compile it into a single executable binary file
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)