bun-router 0.7.4-experimental.6 → 0.7.4-experimental.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,6 @@ import { Router } from '../../';
2
2
 
3
3
  const router = Router();
4
4
 
5
- router.static('/', './examples/ssr/pages');
5
+ router.static('/page', './examples/ssr/pages');
6
6
 
7
7
  router.serve();
@@ -2,5 +2,6 @@ import { Router } from '..';
2
2
 
3
3
  const r = Router(3001);
4
4
 
5
- r.static('/', './pages');
5
+ r.static('/page', './pages');
6
+
6
7
  r.serve();
@@ -9,7 +9,7 @@ _ _
9
9
  |___|___|_|_| |_| |___|___|_| |___|_|
10
10
 
11
11
  `;
12
- const VERSION = '0.7.4-experimental.6';
12
+ const VERSION = '0.7.4-experimental.8';
13
13
  const Logger = (): BunLogger => {
14
14
  return {
15
15
  info: async (statusCode: number, routePath: string, method: string, message?: string) => {
@@ -12,12 +12,24 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
12
12
  const { addRoute, findRoute } = RouteTree();
13
13
  const logger = Logger();
14
14
 
15
- async function loadComponent(name: string) {
16
- const modulePath = path.join(process.cwd(), name);
17
- const module = await import(modulePath);
15
+ async function loadComponent(root: string, name: string) {
16
+ const module = await import(path.join(process.cwd(), root, name));
18
17
  return module.default;
19
18
  }
20
19
 
20
+ function normalizePath(pattern: string, pathname: string) {
21
+ const extension = path.extname(pathname);
22
+ let base = path.basename(pathname);
23
+
24
+ if (extension === '.html' || extension === '.tsx') base = base.replace(extension, '');
25
+
26
+ let patternPath = [pattern, base].join('/');
27
+
28
+ if (base === 'index') patternPath = pattern;
29
+
30
+ return { patternPath, extension, base }
31
+ }
32
+
21
33
  return {
22
34
  // add a route to the router tree
23
35
  add: (pattern, method, callback) => { addRoute(pattern, method, callback); },
@@ -33,29 +45,17 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
33
45
  static: async (pattern: string, root: string) => {
34
46
  if (!exists(root)) console.log(`Cannot find directory ${root}`);
35
47
  await readDir(root, async (fp) => {
36
- const ext = path.extname(fp);
37
-
38
- let base = path.basename(fp);
39
-
40
- if (ext === '.html' || ext === '.tsx') base = base.replace(ext, '');
41
-
42
- if (pattern[0] !== '/') pattern = '/' + pattern;
43
-
44
- let patternPath = pattern + '/' + base;
45
-
46
- if (base === 'index') patternPath = pattern;
47
-
48
- const purePath = path.join(root, patternPath);
48
+ const { patternPath, extension, base } = normalizePath(pattern, fp);
49
49
 
50
50
  const route: Route = {
51
51
  children: new Map(),
52
52
  dynamicPath: '',
53
53
  isLast: true,
54
- path: patternPath.slice(1),
54
+ path: patternPath.slice(1), // remove the leading '/'
55
55
  method: 'GET',
56
56
  handler: async () => {
57
- if (ext === '.tsx') {
58
- const component = await loadComponent(purePath.split('.')[0]);
57
+ if (extension === '.tsx') {
58
+ const component = await loadComponent(root, base);
59
59
  return await http.render(component());
60
60
  } else {
61
61
  return await http.file(200, fp);
@@ -63,8 +63,6 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
63
63
  },
64
64
  };
65
65
 
66
- console.log(route.path);
67
-
68
66
  addRoute(route.path, 'GET', route.handler);
69
67
  });
70
68
  },
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "peerDependencies": {
15
15
  "typescript": "^5.0.0"
16
16
  },
17
- "version": "0.7.4-experimental.6",
17
+ "version": "0.7.4-experimental.8",
18
18
  "dependencies": {
19
19
  "@types/react": "^18.2.22",
20
20
  "eslint-plugin-react-hooks": "^4.6.0",