Skip to content

Commit 8dd64e8

Browse files
committed
feat: add Bun.build support
1 parent 32cd413 commit 8dd64e8

File tree

6 files changed

+41
-17
lines changed

6 files changed

+41
-17
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# elysia-autoload
22

3-
Plugin for [Elysia](https://elysiajs.com/) which autoload all routes in directory and code-generate types for [Eden](https://elysiajs.com/eden/overview.html)
3+
Plugin for [Elysia](https://elysiajs.com/) which autoload all routes in directory and code-generate types for [Eden](https://elysiajs.com/eden/overview.html) with [`Bun.build`](https://bun.sh/docs/bundler) support!
44

55
**Currently, Eden types generation is broken!!**
66

@@ -131,6 +131,25 @@ console.log(data);
131131

132132
Example of app with types code-generation you can see in [example](https://github.com/kravetsone/elysia-autoload/tree/main/example)
133133

134+
### Bun build usage
135+
136+
You can use this plugin with `Bun.build`, thanks to [esbuild-plugin-autoload](https://github.com/kravetsone/esbuild-plugin-autoload)!
137+
138+
```ts
139+
// @filename: build.ts
140+
import { autoload } from "esbuild-plugin-autoload"; // default import also supported
141+
142+
await Bun.build({
143+
entrypoints: ["src/index.ts"],
144+
outdir: "out",
145+
plugins: [autoload()],
146+
}).then(console.log);
147+
```
148+
149+
Then, build it with `bun build.ts` and run with `bun out/index.ts`.
150+
151+
[Read more](https://github.com/kravetsone/esbuild-plugin-autoload)
152+
134153
### Usage of schema handler
135154

136155
```ts

example/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { swagger } from "@elysiajs/swagger";
22
import { Elysia } from "elysia";
3-
import { autoload } from "../src";
3+
import { autoload } from "elysia-autoload";
44

55
const prefix = "/api/" as const;
66

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "elysia-autoload",
3-
"version": "0.2.4",
3+
"version": "1.0.0",
44
"author": "kravetsone",
55
"type": "module",
66
"types": "./dist/index.d.ts",

src/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { existsSync, statSync } from "node:fs";
2-
import { join } from "node:path";
1+
import fs from "node:fs";
2+
import path from "node:path";
33
import {
44
Elysia,
55
type InputSchema,
@@ -48,13 +48,15 @@ const TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
4848
const TYPES_TYPENAME_DEFAULT = "Routes";
4949

5050
export async function autoload(options: IAutoloadOptions = {}) {
51+
// autoload-plugin-sources
52+
const fileSources = {};
53+
5154
const { pattern, dir, prefix, schema, types } = options;
52-
5355
const directoryPath = getPath(dir || "./routes");
5456

55-
if (!existsSync(directoryPath))
57+
if (!fs.existsSync(directoryPath))
5658
throw new Error(`Directory ${directoryPath} doesn't exists`);
57-
if (!statSync(directoryPath).isDirectory())
59+
if (!fs.statSync(directoryPath).isDirectory())
5860
throw new Error(`${directoryPath} isn't a directory`);
5961

6062
const plugin = new Elysia({
@@ -78,16 +80,16 @@ export async function autoload(options: IAutoloadOptions = {}) {
7880

7981
const paths: string[] = [];
8082

81-
for await (const path of sortByNestedParams(files)) {
82-
const fullPath = join(directoryPath, path);
83+
for await (const filePath of sortByNestedParams(files)) {
84+
const fullPath = path.join(directoryPath, filePath);
8385

8486
const file = await import(fullPath);
8587

8688
if (!file.default)
87-
throw new Error(`${path} doesn't provide default export`);
88-
const url = transformToUrl(path);
89+
throw new Error(`${filePath} doesn't provide default export`);
90+
const url = transformToUrl(filePath);
8991

90-
const groupOptions = schema ? schema({ path, url }) : {};
92+
const groupOptions = schema ? schema({ path: filePath, url }) : {};
9193
// TODO: fix later
9294
// @ts-expect-error
9395
plugin.group(url, groupOptions, file.default);

src/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { isAbsolute, join } from "node:path";
1+
import path from "node:path";
22

33
export function getPath(dir: string) {
4-
if (isAbsolute(dir)) return dir;
5-
if (isAbsolute(process.argv[1])) return join(process.argv[1], "..", dir);
4+
if (path.isAbsolute(dir)) return dir;
5+
if (path.isAbsolute(process.argv[1]))
6+
return path.join(process.argv[1], "..", dir);
67

7-
return join(process.cwd(), process.argv[1], "..", dir);
8+
return path.join(process.cwd(), process.argv[1], "..", dir);
89
}
910

1011
// Inspired by https://github.com/wobsoriano/elysia-autoroutes/blob/main/src/utils/transformPathToUrl.ts#L4C31-L4C31

tsup.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { defineConfig } from "tsup";
33
export default defineConfig({
44
entry: ["src/index.ts"],
55
format: ["esm", "cjs"],
6+
67
target: "node20",
78
outDir: "dist",
89
dts: true,
10+
minify: false
911
});

0 commit comments

Comments
 (0)