From a59fb1f33c690616741f240c3b6d1ddbdea1c8a7 Mon Sep 17 00:00:00 2001 From: Kravets <57632712+kravetsone@users.noreply.github.com> Date: Sun, 16 Jun 2024 23:47:15 +0300 Subject: [PATCH] feat: add overload for pass directory --- README.md | 11 +++++++++++ example/gramio/build.ts | 6 +----- src/index.ts | 17 ++++++++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 11ddca7..efc46e7 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,17 @@ await Bun.$`bun build --compile out/index.js`; | pattern? | string | "\*\*\/\*.{ts,tsx,js,jsx,mjs,cjs}" | [Glob patterns]() | | directory? | string | "./src/routes" | The folder where something that will be autoloaded are located | +You can also pass the directory by the first argument instead of an object with full options + +```ts +await Bun.build({ + entrypoints: ["src/index.ts"], + target: "bun", + outdir: "out", + plugins: [autoload("./src/commands")], +}).then(console.log); +``` + ### [esbuild](https://esbuild.github.io/) usage ```ts diff --git a/example/gramio/build.ts b/example/gramio/build.ts index 99153d2..fe15914 100644 --- a/example/gramio/build.ts +++ b/example/gramio/build.ts @@ -4,9 +4,5 @@ await Bun.build({ entrypoints: ["./index.ts"], outdir: "out", target: "bun", - plugins: [ - autoload({ - directory: "./commands", - }), - ], + plugins: [autoload("./commands")], }).then(console.log); diff --git a/src/index.ts b/src/index.ts index 4b84be5..8000db3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,9 +22,20 @@ const fsUsageMock = /* ts */ `{ } }`; -export function autoload(options?: AutoloadOptions) { - const pattern = options?.pattern ?? "**/*.{ts,tsx,js,jsx,mjs,cjs}"; - const directory = options?.directory ?? "./example/routes"; +const DEFAULT_PATTERN = "**/*.{ts,tsx,js,jsx,mjs,cjs}"; +const DEFAULT_DIRECTORY = "./example/routes"; + +export function autoload(options?: AutoloadOptions): BunPlugin; +export function autoload(options?: string): BunPlugin; +export function autoload(options?: AutoloadOptions | string) { + const pattern = + typeof options === "object" + ? options?.pattern ?? DEFAULT_PATTERN + : DEFAULT_PATTERN; + const directory = + typeof options === "string" + ? options + : options?.directory ?? DEFAULT_DIRECTORY; return { name: "autoload",