Skip to content

Autoload Plugin

npmJSRJSR Score

Autoload commands plugin for GramIO with Bun.build support.

Installation

bash
npm install @gramio/autoload
bash
yarn add @gramio/autoload
bash
pnpm add @gramio/autoload
bash
bun install @gramio/autoload

Usage

full example

IMPORTANT

Please read about Lazy-load plugins

Register the plugin

ts
// index.ts
import { 
Bot
} from "gramio";
import {
autoload
} from "@gramio/autoload";
const
bot
= new
Bot
(
process
.
env
.
BOT_TOKEN
as string)
.
extend
(await
autoload
())
.
onStart
(
console
.
log
);
bot
.
start
();
export type
BotType
= typeof
bot
;

Create command

ts
// commands/command.ts
import type { BotType } from "..";

export default (bot: BotType) =>
    bot.command("start", (context) => context.send("hello!"));

Options

KeyTypeDefaultDescription
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?booleantrueThrows an error if no matches are found
skipImportErrors?booleanfalseSkip imports where needed export not defined
onLoad?(params: { absolute: string; relative: string }) => unknownHook that is called when loading a file
onFinish?(paths: { absolute: string; relative: string }[]) => unknown;Hook that is called after loading all files
fdir?OptionsOptions to configure fdir
picomatch?PicomatchOptionsOptions to configure picomatch

Bun build usage

You can use this plugin with Bun.build, thanks to 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.

Bun compile usage

You can bundle and then compile it into a single executable binary file

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)

Read more