Skip to content

Commit 5a0ec34

Browse files
committed
feat: use relative imports instead of absolute in types-generation
1 parent 9664a9d commit 5a0ec34

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

src/index.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "elysia";
1010
import type { BaseMacro } from "elysia/dist/types";
1111
import {
12+
addRelativeIfNotDot,
1213
fixSlashes,
1314
getPath,
1415
sortByNestedParams,
@@ -44,6 +45,7 @@ export interface IAutoloadOptions {
4445
types?: ITypesOptions | true;
4546
}
4647

48+
const DIR_ROUTES_DEFAULT = "./routes";
4749
const TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
4850
const TYPES_TYPENAME_DEFAULT = "Routes";
4951
const TYPES_OBJECT_DEFAULT = {
@@ -55,8 +57,9 @@ export async function autoload(options: IAutoloadOptions = {}) {
5557
// autoload-plugin-sources
5658
const fileSources = {};
5759

58-
const { pattern, dir, prefix, schema } = options;
60+
const { pattern, prefix, schema } = options;
5961

62+
const dir = options.dir ?? DIR_ROUTES_DEFAULT;
6063
// some strange code to provide defaults
6164
const types: Omit<ITypesOptions, "output"> & { output: string[] } =
6265
options.types && options.types !== true
@@ -72,7 +75,7 @@ export async function autoload(options: IAutoloadOptions = {}) {
7275
}
7376
: TYPES_OBJECT_DEFAULT;
7477

75-
const directoryPath = getPath(dir || "./routes");
78+
const directoryPath = getPath(dir);
7679

7780
if (!fs.existsSync(directoryPath))
7881
throw new Error(`Directory ${directoryPath} doesn't exists`);
@@ -117,26 +120,30 @@ export async function autoload(options: IAutoloadOptions = {}) {
117120
if (types) paths.push(fullPath.replace(directoryPath, ""));
118121
}
119122

120-
// esbuild-plugin-autoload remove-start
121123
if (types) {
122-
const imports: string[] = paths.map(
123-
(x, index) =>
124-
`import type Route${index} from "${(
125-
directoryPath + x.replace(".ts", "").replace(".tsx", "")
126-
).replace(/\\/gu, "/")}";`,
127-
);
128-
129124
for await (const outputPath of types.output) {
125+
const outputAbsolutePath = getPath(outputPath);
126+
127+
const imports: string[] = paths.map(
128+
(x, index) =>
129+
`import type Route${index} from "${addRelativeIfNotDot(
130+
path
131+
.relative(
132+
path.dirname(outputAbsolutePath),
133+
directoryPath + x.replace(".ts", "").replace(".tsx", ""),
134+
)
135+
.replace(/\\/gu, "/"),
136+
)}";`,
137+
);
138+
130139
await Bun.write(
131-
getPath(outputPath),
140+
outputAbsolutePath,
132141
[
133142
`import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
134143
imports.join("\n"),
135144
"",
136145
!types.useExport ? "declare global {" : "",
137-
` export type ${
138-
!types.typeName ? TYPES_TYPENAME_DEFAULT : types.typeName
139-
} = ${paths
146+
` export type ${types.typeName} = ${paths
140147
.map(
141148
(x, index) =>
142149
`ElysiaWithBaseUrl<"${
@@ -151,7 +158,6 @@ export async function autoload(options: IAutoloadOptions = {}) {
151158
}
152159
}
153160

154-
// esbuild-plugin-autoload remove-end
155161
return plugin;
156162
}
157163
export * from "./types";

src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,9 @@ export function fixSlashes(prefix?: string) {
5959

6060
return prefix.slice(0, -1);
6161
}
62+
63+
export function addRelativeIfNotDot(path: string) {
64+
if (path.at(0) !== ".") return `./${path}`;
65+
66+
return path;
67+
}

tests/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { autoload } from "../src/index";
55
import { sortByNestedParams, transformToUrl } from "../src/utils";
66

77
const app_with_prefix = new Elysia({
8-
prefix: "/api",
8+
prefix: "/api", // BROKEN FOR NOW
99
}).use(
1010
autoload({
1111
pattern: "**/*.{ts,js}",

tests/types/routes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ElysiaWithBaseUrl } from "elysia-autoload";
2-
import type Route0 from "Z:/PROJECTS/node-ts/elysia-autoload/tests/routes/index";
3-
import type Route1 from "Z:/PROJECTS/node-ts/elysia-autoload/tests/routes/users/[id]";
2+
import type Route0 from "../routes/index";
3+
import type Route1 from "../routes/users/[id]";
44

55
declare global {
6-
export type Routes = ElysiaWithBaseUrl<"/api", ReturnType<typeof Route0>>
7-
& ElysiaWithBaseUrl<"/api/users/:id", ReturnType<typeof Route1>>
6+
export type Routes = ElysiaWithBaseUrl<"/", ReturnType<typeof Route0>>
7+
& ElysiaWithBaseUrl<"/users/:id", ReturnType<typeof Route1>>
88
}

0 commit comments

Comments
 (0)