Skip to content

Commit

Permalink
feat: add Bun.build support
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Jun 15, 2024
1 parent 32cd413 commit 8dd64e8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# elysia-autoload

Plugin for [Elysia](https://elysiajs.com/) which autoload all routes in directory and code-generate types for [Eden](https://elysiajs.com/eden/overview.html)
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!

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

Expand Down Expand Up @@ -131,6 +131,25 @@ console.log(data);

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

### Bun build usage

You can use this plugin with `Bun.build`, thanks to [esbuild-plugin-autoload](https://github.com/kravetsone/esbuild-plugin-autoload)!

```ts
// @filename: build.ts
import { autoload } from "esbuild-plugin-autoload"; // default import also supported

await Bun.build({
entrypoints: ["src/index.ts"],
outdir: "out",
plugins: [autoload()],
}).then(console.log);
```

Then, build it with `bun build.ts` and run with `bun out/index.ts`.

[Read more](https://github.com/kravetsone/esbuild-plugin-autoload)

### Usage of schema handler

```ts
Expand Down
2 changes: 1 addition & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { swagger } from "@elysiajs/swagger";
import { Elysia } from "elysia";
import { autoload } from "../src";
import { autoload } from "elysia-autoload";

const prefix = "/api/" as const;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elysia-autoload",
"version": "0.2.4",
"version": "1.0.0",
"author": "kravetsone",
"type": "module",
"types": "./dist/index.d.ts",
Expand Down
22 changes: 12 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync, statSync } from "node:fs";
import { join } from "node:path";
import fs from "node:fs";
import path from "node:path";
import {
Elysia,
type InputSchema,
Expand Down Expand Up @@ -48,13 +48,15 @@ const TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
const TYPES_TYPENAME_DEFAULT = "Routes";

export async function autoload(options: IAutoloadOptions = {}) {
// autoload-plugin-sources
const fileSources = {};

const { pattern, dir, prefix, schema, types } = options;

const directoryPath = getPath(dir || "./routes");

if (!existsSync(directoryPath))
if (!fs.existsSync(directoryPath))
throw new Error(`Directory ${directoryPath} doesn't exists`);
if (!statSync(directoryPath).isDirectory())
if (!fs.statSync(directoryPath).isDirectory())
throw new Error(`${directoryPath} isn't a directory`);

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

const paths: string[] = [];

for await (const path of sortByNestedParams(files)) {
const fullPath = join(directoryPath, path);
for await (const filePath of sortByNestedParams(files)) {
const fullPath = path.join(directoryPath, filePath);

const file = await import(fullPath);

if (!file.default)
throw new Error(`${path} doesn't provide default export`);
const url = transformToUrl(path);
throw new Error(`${filePath} doesn't provide default export`);
const url = transformToUrl(filePath);

const groupOptions = schema ? schema({ path, url }) : {};
const groupOptions = schema ? schema({ path: filePath, url }) : {};
// TODO: fix later
// @ts-expect-error
plugin.group(url, groupOptions, file.default);
Expand Down
9 changes: 5 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { isAbsolute, join } from "node:path";
import path from "node:path";

export function getPath(dir: string) {
if (isAbsolute(dir)) return dir;
if (isAbsolute(process.argv[1])) return join(process.argv[1], "..", dir);
if (path.isAbsolute(dir)) return dir;
if (path.isAbsolute(process.argv[1]))
return path.join(process.argv[1], "..", dir);

return join(process.cwd(), process.argv[1], "..", dir);
return path.join(process.cwd(), process.argv[1], "..", dir);
}

// Inspired by https://github.com/wobsoriano/elysia-autoroutes/blob/main/src/utils/transformPathToUrl.ts#L4C31-L4C31
Expand Down
2 changes: 2 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { defineConfig } from "tsup";
export default defineConfig({
entry: ["src/index.ts"],
format: ["esm", "cjs"],

target: "node20",
outDir: "dist",
dts: true,
minify: false
});

0 comments on commit 8dd64e8

Please sign in to comment.