bun-router 0.7.4-experimental.2 → 0.7.4-experimental.4
Sign up to get free protection for your applications and to get access to all the features.
- package/examples/static.ts +1 -1
- package/lib/logger/logger.ts +1 -1
- package/lib/resolver/resolver.test.ts +5 -0
- package/lib/resolver/{resolveTSX.ts → resolver.ts} +18 -3
- package/lib/resolver/testdir/foo.tsx +5 -0
- package/lib/router/router.ts +8 -8
- package/package.json +4 -2
- package/tsconfig.json +2 -2
package/examples/static.ts
CHANGED
package/lib/logger/logger.ts
CHANGED
@@ -9,7 +9,7 @@ _ _
|
|
9
9
|
|___|___|_|_| |_| |___|___|_| |___|_|
|
10
10
|
|
11
11
|
`;
|
12
|
-
const VERSION = '0.7.4-experimental';
|
12
|
+
const VERSION = '0.7.4-experimental.4';
|
13
13
|
const Logger = (): BunLogger => {
|
14
14
|
return {
|
15
15
|
info: async (statusCode: number, routePath: string, method: string, message?: string) => {
|
@@ -13,16 +13,31 @@ async function createFileTree(root: string) {
|
|
13
13
|
}
|
14
14
|
|
15
15
|
async function resolveModules(root: string) {
|
16
|
+
|
17
|
+
if (!await doesExist(root)) {
|
18
|
+
throw new Error(`Directory ${root} does not exist`);
|
19
|
+
}
|
20
|
+
|
16
21
|
const tree = await createFileTree(root);
|
17
22
|
const files = tree.getFilesByExtension('.tsx');
|
18
23
|
const modules: ComponentType[] = [];
|
19
24
|
|
20
25
|
for (const file of files) {
|
21
|
-
const module = await import(file)
|
26
|
+
const module = await import(file);
|
22
27
|
modules.push(module);
|
23
28
|
}
|
24
|
-
|
25
29
|
return modules;
|
26
30
|
}
|
27
31
|
|
28
|
-
|
32
|
+
async function doesExist(root: string): Promise<boolean> {
|
33
|
+
const file = Bun.file(root);
|
34
|
+
return await file.exists();
|
35
|
+
}
|
36
|
+
|
37
|
+
async function isDir(root: string): Promise<boolean> {
|
38
|
+
const file = Bun.file(root);
|
39
|
+
return await file.isDir();
|
40
|
+
}
|
41
|
+
|
42
|
+
|
43
|
+
export { resolveModules };
|
package/lib/router/router.ts
CHANGED
@@ -31,21 +31,21 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
|
|
31
31
|
// the root directory is traversed recursively
|
32
32
|
static: async (pattern: string, root: string) => {
|
33
33
|
await readDir(root, async (fp) => {
|
34
|
-
const
|
35
|
-
const ext = path.extname(pure);
|
34
|
+
const ext = path.extname(fp);
|
36
35
|
|
37
|
-
let base = path.basename(
|
36
|
+
let base = path.basename(fp);
|
38
37
|
|
39
38
|
//FIXME: this can be improved
|
40
|
-
if (ext === '.html') base = base.replace(ext, '');
|
41
|
-
if (ext === '.tsx') base = base.replace(ext, '');
|
39
|
+
if (ext === '.html' || ext === '.tsx') base = base.replace(ext, '');
|
42
40
|
|
43
41
|
if (pattern[0] !== '/') pattern = '/' + pattern;
|
44
42
|
|
45
|
-
let patternPath = pattern + base;
|
43
|
+
let patternPath = pattern + '/' + base;
|
46
44
|
|
47
45
|
if (base === 'index') patternPath = pattern;
|
48
46
|
|
47
|
+
const purePath = path.join(root, patternPath);
|
48
|
+
|
49
49
|
const route: Route = {
|
50
50
|
children: new Map(),
|
51
51
|
dynamicPath: '',
|
@@ -54,10 +54,10 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
|
|
54
54
|
method: 'GET',
|
55
55
|
handler: async () => {
|
56
56
|
if (ext === '.tsx') {
|
57
|
-
const component = await loadComponent(
|
57
|
+
const component = await loadComponent(purePath.split('.')[0]);
|
58
58
|
return await http.render(component());
|
59
59
|
} else {
|
60
|
-
return await http.file(200,
|
60
|
+
return await http.file(200, fp);
|
61
61
|
}
|
62
62
|
},
|
63
63
|
};
|
package/package.json
CHANGED
@@ -14,8 +14,10 @@
|
|
14
14
|
"peerDependencies": {
|
15
15
|
"typescript": "^5.0.0"
|
16
16
|
},
|
17
|
-
"version": "0.7.4-experimental.
|
17
|
+
"version": "0.7.4-experimental.4",
|
18
18
|
"dependencies": {
|
19
|
-
"
|
19
|
+
"@types/react": "^18.2.22",
|
20
|
+
"eslint-plugin-react-hooks": "^4.6.0",
|
21
|
+
"react": "^18.2.0"
|
20
22
|
}
|
21
23
|
}
|