From 8dd64e8a75ab8cf5c9f84394758af23711a4fb85 Mon Sep 17 00:00:00 2001 From: Kravets <57632712+kravetsone@users.noreply.github.com> Date: Sat, 15 Jun 2024 22:24:51 +0300 Subject: [PATCH] feat: add `Bun.build` support --- README.md | 21 ++++++++++++++++++++- example/index.ts | 2 +- package.json | 2 +- src/index.ts | 22 ++++++++++++---------- src/utils.ts | 9 +++++---- tsup.config.ts | 2 ++ 6 files changed, 41 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 06dd415..4e1e949 100644 --- a/README.md +++ b/README.md @@ -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!!** @@ -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 diff --git a/example/index.ts b/example/index.ts index e013312..8c0f79f 100644 --- a/example/index.ts +++ b/example/index.ts @@ -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; diff --git a/package.json b/package.json index fea868e..80743b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "elysia-autoload", - "version": "0.2.4", + "version": "1.0.0", "author": "kravetsone", "type": "module", "types": "./dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 162f162..e8ad02c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -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({ @@ -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); diff --git a/src/utils.ts b/src/utils.ts index 47022d8..9057c8f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 diff --git a/tsup.config.ts b/tsup.config.ts index dd8cfd0..8e88e6d 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -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 });