bertui 0.1.3 → 0.1.4
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.
- package/index.js +1 -1
- package/package.json +1 -1
- package/src/router/router.js +34 -15
package/index.js
CHANGED
package/package.json
CHANGED
package/src/router/router.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/router/router.js
|
|
2
2
|
import { join, relative, parse } from 'path';
|
|
3
3
|
import { readdirSync, statSync, existsSync } from 'fs';
|
|
4
|
-
import logger from '
|
|
4
|
+
import logger from 'ernest-logger'; // Assuming Ernest Logger is used
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Scans the pages directory and generates route definitions
|
|
@@ -27,28 +27,45 @@ export function generateRoutes(root) {
|
|
|
27
27
|
scanDirectory(fullPath, join(basePath, entry));
|
|
28
28
|
} else if (stat.isFile() && /\.(jsx?|tsx?)$/.test(entry)) {
|
|
29
29
|
const parsed = parse(entry);
|
|
30
|
-
|
|
30
|
+
let fileName = parsed.name;
|
|
31
31
|
|
|
32
|
-
// Skip non-page files
|
|
33
|
-
if (fileName.startsWith('
|
|
32
|
+
// Skip non-page files (e.g., those starting with a dot)
|
|
33
|
+
if (fileName.startsWith('.') || fileName.startsWith('~')) {
|
|
34
34
|
continue;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
// --- ROUTE PATH GENERATION ---
|
|
38
|
+
|
|
39
|
+
// Handle dynamic routes: _filename -> :filename
|
|
40
|
+
let isDynamic = false;
|
|
41
|
+
if (fileName.startsWith('_')) {
|
|
42
|
+
fileName = fileName.slice(1); // Remove the underscore
|
|
43
|
+
isDynamic = true;
|
|
44
|
+
}
|
|
45
|
+
|
|
37
46
|
// Generate route path
|
|
38
47
|
let routePath = join(basePath, fileName === 'index' ? '' : fileName);
|
|
39
|
-
routePath = '/' + routePath.replace(/\\/g, '/');
|
|
48
|
+
routePath = '/' + routePath.replace(/\\/g, '/'); // Use forward slashes
|
|
40
49
|
|
|
41
|
-
//
|
|
42
|
-
|
|
50
|
+
// Apply dynamic parameter if detected
|
|
51
|
+
if (isDynamic) {
|
|
52
|
+
// Replace the last part of the route path with the dynamic parameter (e.g., /blog/slug -> /blog/:slug)
|
|
53
|
+
routePath = routePath.replace(new RegExp(`/${fileName}$`), `/:${fileName}`);
|
|
54
|
+
}
|
|
43
55
|
|
|
44
|
-
//
|
|
56
|
+
// Handle the root path, ensuring it's just '/'
|
|
57
|
+
if (routePath === '//') {
|
|
58
|
+
routePath = '/';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Get relative path from pages dir (used for imports in generated code)
|
|
45
62
|
const relativePath = relative(pagesDir, fullPath);
|
|
46
63
|
|
|
47
64
|
routes.push({
|
|
48
65
|
path: routePath,
|
|
49
66
|
file: relativePath,
|
|
50
67
|
component: fullPath,
|
|
51
|
-
isDynamic: routePath.includes(':')
|
|
68
|
+
isDynamic: isDynamic || routePath.includes(':')
|
|
52
69
|
});
|
|
53
70
|
}
|
|
54
71
|
}
|
|
@@ -56,8 +73,10 @@ export function generateRoutes(root) {
|
|
|
56
73
|
|
|
57
74
|
scanDirectory(pagesDir);
|
|
58
75
|
|
|
59
|
-
// Sort routes:
|
|
76
|
+
// Sort routes: Root path first, then static, then dynamic
|
|
60
77
|
routes.sort((a, b) => {
|
|
78
|
+
if (a.path === '/') return -1;
|
|
79
|
+
if (b.path === '/') return 1;
|
|
61
80
|
if (a.isDynamic && !b.isDynamic) return 1;
|
|
62
81
|
if (!a.isDynamic && b.isDynamic) return -1;
|
|
63
82
|
return a.path.localeCompare(b.path);
|
|
@@ -102,14 +121,14 @@ export function Router() {
|
|
|
102
121
|
const matchedRoute = routes.find(route => {
|
|
103
122
|
if (route.path === currentPath) return true;
|
|
104
123
|
|
|
105
|
-
// Handle dynamic routes
|
|
124
|
+
// Handle dynamic routes (simple static matching for now)
|
|
106
125
|
const routeParts = route.path.split('/');
|
|
107
126
|
const pathParts = currentPath.split('/');
|
|
108
127
|
|
|
109
128
|
if (routeParts.length !== pathParts.length) return false;
|
|
110
129
|
|
|
111
130
|
return routeParts.every((part, i) => {
|
|
112
|
-
if (part.startsWith(':')) return true;
|
|
131
|
+
if (part.startsWith(':')) return true; // Match any value for dynamic part
|
|
113
132
|
return part === pathParts[i];
|
|
114
133
|
});
|
|
115
134
|
});
|
|
@@ -182,9 +201,9 @@ export function logRoutes(routes) {
|
|
|
182
201
|
if (routes.length === 0) {
|
|
183
202
|
logger.warn('No routes found in src/pages/');
|
|
184
203
|
logger.info('Create files in src/pages/ to define routes:');
|
|
185
|
-
logger.info('
|
|
186
|
-
logger.info('
|
|
187
|
-
logger.info('
|
|
204
|
+
logger.info(' src/pages/index.jsx → /');
|
|
205
|
+
logger.info(' src/pages/about.jsx → /about');
|
|
206
|
+
logger.info(' src/pages/user/_id.jsx → /user/:id'); // Updated tip
|
|
188
207
|
return;
|
|
189
208
|
}
|
|
190
209
|
|