Skip to content

Commit

Permalink
feat: add overload for pass directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Jun 16, 2024
1 parent 76046f7 commit a59fb1f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ await Bun.$`bun build --compile out/index.js`;
| pattern? | string | "\*\*\/\*.{ts,tsx,js,jsx,mjs,cjs}" | [Glob patterns](<https://en.wikipedia.org/wiki/Glob_(programming)>) |
| 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
Expand Down
6 changes: 1 addition & 5 deletions example/gramio/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@ await Bun.build({
entrypoints: ["./index.ts"],
outdir: "out",
target: "bun",
plugins: [
autoload({
directory: "./commands",
}),
],
plugins: [autoload("./commands")],
}).then(console.log);
17 changes: 14 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a59fb1f

Please sign in to comment.