bertui 0.1.8 → 0.2.0

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 CHANGED
@@ -32,5 +32,5 @@ export default {
32
32
  buildCSS,
33
33
  copyCSS,
34
34
  program,
35
- version: "0.1.8"
35
+ version: "0.2.0"
36
36
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bertui",
3
- "version": "0.1.8",
3
+ "version": "0.2.0",
4
4
  "description": "Lightning-fast React dev server powered by Bun and Elysia",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -40,7 +40,7 @@ export async function compileProject(root) {
40
40
  const stats = await compileDirectory(srcDir, outDir, root);
41
41
  const duration = Date.now() - startTime;
42
42
 
43
- // Generate router AFTER compilation so we reference .js files
43
+ // Generate router AFTER compilation
44
44
  if (routes.length > 0) {
45
45
  await generateRouter(routes, outDir, root);
46
46
  logger.info('Generated router.js');
@@ -104,7 +104,6 @@ async function discoverRoutes(pagesDir) {
104
104
  async function generateRouter(routes, outDir, root) {
105
105
  const imports = routes.map((route, i) => {
106
106
  const componentName = `Page${i}`;
107
- // CRITICAL FIX: Import .js files (compiled), not .jsx
108
107
  const importPath = `./pages/${route.file.replace(/\.(jsx|tsx|ts)$/, '.js')}`;
109
108
  return `import ${componentName} from '${importPath}';`;
110
109
  }).join('\n');
@@ -198,14 +197,14 @@ async function compileFile(srcPath, outDir, filename, relativePath) {
198
197
  try {
199
198
  let code = await Bun.file(srcPath).text();
200
199
 
201
- // Remove bertui/styles imports completely
200
+ // Remove bertui/styles imports
202
201
  code = code.replace(/import\s+['"]bertui\/styles['"]\s*;?\s*/g, '');
203
202
 
204
- // Replace bertui/router imports with CDN React Router (we'll use a simpler approach)
205
- // For now, keep the import as-is and handle it differently
206
-
207
203
  const transpiler = new Bun.Transpiler({ loader });
208
- const compiled = await transpiler.transform(code);
204
+ let compiled = await transpiler.transform(code);
205
+
206
+ // CRITICAL FIX: Add .js extensions to all relative imports
207
+ compiled = fixRelativeImports(compiled);
209
208
 
210
209
  const outFilename = filename.replace(/\.(jsx|tsx|ts)$/, '.js');
211
210
  const outPath = join(outDir, outFilename);
@@ -216,4 +215,22 @@ async function compileFile(srcPath, outDir, filename, relativePath) {
216
215
  logger.error(`Failed to compile ${relativePath}: ${error.message}`);
217
216
  throw error;
218
217
  }
218
+ }
219
+
220
+ function fixRelativeImports(code) {
221
+ // Match import statements with relative paths that don't already have extensions
222
+ // Matches: import X from './path' or import X from '../path'
223
+ // But NOT: import X from './path.js' or import X from 'package'
224
+
225
+ const importRegex = /from\s+['"](\.\.[\/\\]|\.\/)((?:[^'"]+?)(?<!\.js|\.jsx|\.ts|\.tsx|\.json))['"];?/g;
226
+
227
+ code = code.replace(importRegex, (match, prefix, path) => {
228
+ // Don't add .js if path already has an extension or ends with /
229
+ if (path.endsWith('/') || /\.\w+$/.test(path)) {
230
+ return match;
231
+ }
232
+ return `from '${prefix}${path}.js';`;
233
+ });
234
+
235
+ return code;
219
236
  }
@@ -1,7 +1,5 @@
1
- // src/router/Router.jsx
2
1
  import { useState, useEffect, createContext, useContext } from 'react';
3
2
 
4
- // Router context
5
3
  const RouterContext = createContext(null);
6
4
 
7
5
  export function useRouter() {
@@ -12,20 +10,13 @@ export function useRouter() {
12
10
  return context;
13
11
  }
14
12
 
15
- export function useParams() {
16
- const { params } = useRouter();
17
- return params;
18
- }
19
-
20
- export function Router({ routes, children }) {
13
+ export function Router({ routes }) {
21
14
  const [currentRoute, setCurrentRoute] = useState(null);
22
15
  const [params, setParams] = useState({});
23
16
 
24
17
  useEffect(() => {
25
- // Match initial route
26
18
  matchAndSetRoute(window.location.pathname);
27
19
 
28
- // Handle browser navigation
29
20
  const handlePopState = () => {
30
21
  matchAndSetRoute(window.location.pathname);
31
22
  };
@@ -35,7 +26,6 @@ export function Router({ routes, children }) {
35
26
  }, []);
36
27
 
37
28
  function matchAndSetRoute(pathname) {
38
- // Try exact match first (static routes)
39
29
  for (const route of routes) {
40
30
  if (route.type === 'static' && route.path === pathname) {
41
31
  setCurrentRoute(route);
@@ -44,7 +34,6 @@ export function Router({ routes, children }) {
44
34
  }
45
35
  }
46
36
 
47
- // Try dynamic routes
48
37
  for (const route of routes) {
49
38
  if (route.type === 'dynamic') {
50
39
  const pattern = route.path.replace(/\[([^\]]+)\]/g, '([^/]+)');
@@ -52,7 +41,6 @@ export function Router({ routes, children }) {
52
41
  const match = pathname.match(regex);
53
42
 
54
43
  if (match) {
55
- // Extract params
56
44
  const paramNames = [...route.path.matchAll(/\[([^\]]+)\]/g)].map(m => m[1]);
57
45
  const extractedParams = {};
58
46
  paramNames.forEach((name, i) => {
@@ -66,7 +54,6 @@ export function Router({ routes, children }) {
66
54
  }
67
55
  }
68
56
 
69
- // No match found - 404
70
57
  setCurrentRoute(null);
71
58
  setParams({});
72
59
  }
@@ -83,18 +70,16 @@ export function Router({ routes, children }) {
83
70
  pathname: window.location.pathname
84
71
  };
85
72
 
73
+ const Component = currentRoute?.component;
74
+
86
75
  return (
87
76
  <RouterContext.Provider value={routerValue}>
88
- {currentRoute ? (
89
- <currentRoute.component />
90
- ) : (
91
- children || <NotFound />
92
- )}
77
+ {Component ? <Component params={params} /> : <NotFound />}
93
78
  </RouterContext.Provider>
94
79
  );
95
80
  }
96
81
 
97
- export function Link({ to, children, className, ...props }) {
82
+ export function Link({ to, children, ...props }) {
98
83
  const { navigate } = useRouter();
99
84
 
100
85
  function handleClick(e) {
@@ -103,7 +88,7 @@ export function Link({ to, children, className, ...props }) {
103
88
  }
104
89
 
105
90
  return (
106
- <a href={to} onClick={handleClick} className={className} {...props}>
91
+ <a href={to} onClick={handleClick} {...props}>
107
92
  {children}
108
93
  </a>
109
94
  );
@@ -117,7 +102,7 @@ function NotFound() {
117
102
  alignItems: 'center',
118
103
  justifyContent: 'center',
119
104
  minHeight: '100vh',
120
- fontFamily: 'system-ui, sans-serif'
105
+ fontFamily: 'system-ui'
121
106
  }}>
122
107
  <h1 style={{ fontSize: '6rem', margin: 0 }}>404</h1>
123
108
  <p style={{ fontSize: '1.5rem', color: '#666' }}>Page not found</p>