@samadhi1311/router 1.0.4 → 1.0.6

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/dist/index.cjs CHANGED
@@ -35,9 +35,10 @@ __export(index_exports, {
35
35
  module.exports = __toCommonJS(index_exports);
36
36
  var import_fs = __toESM(require("fs"), 1);
37
37
  var import_path = __toESM(require("path"), 1);
38
+ var import_esbuild = require("esbuild");
38
39
  function router(options = {}) {
39
40
  let root;
40
- const appDir = options.appDir || "app";
41
+ const appDir = options.appDir || "src";
41
42
  const virtualModuleId = "virtual:router";
42
43
  const resolvedVirtualModuleId = "\0" + virtualModuleId;
43
44
  const resolveFile = (p) => import_path.default.posix.join(root.replace(/\\/g, "/"), appDir, p);
@@ -100,29 +101,19 @@ function router(options = {}) {
100
101
  return routes;
101
102
  }
102
103
  function generateRouteCode(routes) {
103
- let imports = 'import { Suspense } from "react";\nimport { Outlet } from "react-router";\n';
104
+ let imports = 'import { Outlet, RouteObject } from "react-router-dom";\n';
104
105
  let routeConfigs = "";
105
106
  let importCounter = 0;
106
107
  function processRoute(route, depth = 0) {
107
108
  const indent = " ".repeat(depth + 1);
108
109
  const componentId = route.componentPath ? `Page${importCounter++}` : null;
109
110
  const layoutId = route.layoutPath ? `Layout${importCounter++}` : null;
110
- const loadingId = route.loadingPath ? `Loading${importCounter++}` : null;
111
- const notFoundId = route.notFoundPath ? `NotFound${importCounter++}` : null;
112
111
  if (componentId && route.componentPath) {
113
112
  imports += `import ${componentId} from '${resolveFile(route.componentPath)}';
114
113
  `;
115
114
  }
116
115
  if (layoutId && route.layoutPath) {
117
116
  imports += `import ${layoutId} from '${resolveFile(route.layoutPath)}';
118
- `;
119
- }
120
- if (loadingId && route.loadingPath) {
121
- imports += `import ${loadingId} from '${resolveFile(route.loadingPath)}';
122
- `;
123
- }
124
- if (notFoundId && route.notFoundPath) {
125
- imports += `import ${notFoundId} from '${resolveFile(route.notFoundPath)}';
126
117
  `;
127
118
  }
128
119
  let config = `${indent}{
@@ -130,18 +121,13 @@ function router(options = {}) {
130
121
  config += `${indent} path: '${route.path}',
131
122
  `;
132
123
  if (layoutId) {
133
- config += `${indent} element: <${layoutId}><Outlet /></${layoutId}>,
124
+ config += `${indent} element: ${layoutId},
134
125
  `;
135
126
  } else if (componentId) {
136
- const element = loadingId ? `<Suspense fallback={<${loadingId} />}><${componentId} /></Suspense>` : `<${componentId} />`;
137
- config += `${indent} element: ${element},
127
+ config += `${indent} element: ${componentId},
138
128
  `;
139
129
  } else {
140
- config += `${indent} element: <Outlet />,
141
- `;
142
- }
143
- if (notFoundId) {
144
- config += `${indent} errorElement: <${notFoundId} />,
130
+ config += `${indent} element: Outlet,
145
131
  `;
146
132
  }
147
133
  if (route.children && route.children.length > 0) {
@@ -161,7 +147,7 @@ function router(options = {}) {
161
147
  routeConfigs += processRoute(route, 0);
162
148
  });
163
149
  return `${imports}
164
- export const routes = [
150
+ export const routes: RouteObject[] = [
165
151
  ${routeConfigs}];
166
152
  `;
167
153
  }
@@ -180,8 +166,20 @@ ${routeConfigs}];
180
166
  if (id === resolvedVirtualModuleId) {
181
167
  const routes = scanDirectory("");
182
168
  return {
183
- code: generateRouteCode(routes),
184
- lang: "tsx"
169
+ code: generateRouteCode(routes)
170
+ };
171
+ }
172
+ },
173
+ async transform(code, id) {
174
+ if (id === resolvedVirtualModuleId) {
175
+ const result = await (0, import_esbuild.transform)(code, {
176
+ loader: "tsx",
177
+ jsx: "automatic",
178
+ target: "esnext"
179
+ });
180
+ return {
181
+ code: result.code,
182
+ map: null
185
183
  };
186
184
  }
187
185
  },
package/dist/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  // src/index.ts
2
2
  import fs from "fs";
3
3
  import path from "path";
4
+ import { transform } from "esbuild";
4
5
  function router(options = {}) {
5
6
  let root;
6
- const appDir = options.appDir || "app";
7
+ const appDir = options.appDir || "src";
7
8
  const virtualModuleId = "virtual:router";
8
9
  const resolvedVirtualModuleId = "\0" + virtualModuleId;
9
10
  const resolveFile = (p) => path.posix.join(root.replace(/\\/g, "/"), appDir, p);
@@ -66,29 +67,19 @@ function router(options = {}) {
66
67
  return routes;
67
68
  }
68
69
  function generateRouteCode(routes) {
69
- let imports = 'import { Suspense } from "react";\nimport { Outlet } from "react-router";\n';
70
+ let imports = 'import { Outlet, RouteObject } from "react-router-dom";\n';
70
71
  let routeConfigs = "";
71
72
  let importCounter = 0;
72
73
  function processRoute(route, depth = 0) {
73
74
  const indent = " ".repeat(depth + 1);
74
75
  const componentId = route.componentPath ? `Page${importCounter++}` : null;
75
76
  const layoutId = route.layoutPath ? `Layout${importCounter++}` : null;
76
- const loadingId = route.loadingPath ? `Loading${importCounter++}` : null;
77
- const notFoundId = route.notFoundPath ? `NotFound${importCounter++}` : null;
78
77
  if (componentId && route.componentPath) {
79
78
  imports += `import ${componentId} from '${resolveFile(route.componentPath)}';
80
79
  `;
81
80
  }
82
81
  if (layoutId && route.layoutPath) {
83
82
  imports += `import ${layoutId} from '${resolveFile(route.layoutPath)}';
84
- `;
85
- }
86
- if (loadingId && route.loadingPath) {
87
- imports += `import ${loadingId} from '${resolveFile(route.loadingPath)}';
88
- `;
89
- }
90
- if (notFoundId && route.notFoundPath) {
91
- imports += `import ${notFoundId} from '${resolveFile(route.notFoundPath)}';
92
83
  `;
93
84
  }
94
85
  let config = `${indent}{
@@ -96,18 +87,13 @@ function router(options = {}) {
96
87
  config += `${indent} path: '${route.path}',
97
88
  `;
98
89
  if (layoutId) {
99
- config += `${indent} element: <${layoutId}><Outlet /></${layoutId}>,
90
+ config += `${indent} element: ${layoutId},
100
91
  `;
101
92
  } else if (componentId) {
102
- const element = loadingId ? `<Suspense fallback={<${loadingId} />}><${componentId} /></Suspense>` : `<${componentId} />`;
103
- config += `${indent} element: ${element},
93
+ config += `${indent} element: ${componentId},
104
94
  `;
105
95
  } else {
106
- config += `${indent} element: <Outlet />,
107
- `;
108
- }
109
- if (notFoundId) {
110
- config += `${indent} errorElement: <${notFoundId} />,
96
+ config += `${indent} element: Outlet,
111
97
  `;
112
98
  }
113
99
  if (route.children && route.children.length > 0) {
@@ -127,7 +113,7 @@ function router(options = {}) {
127
113
  routeConfigs += processRoute(route, 0);
128
114
  });
129
115
  return `${imports}
130
- export const routes = [
116
+ export const routes: RouteObject[] = [
131
117
  ${routeConfigs}];
132
118
  `;
133
119
  }
@@ -146,8 +132,20 @@ ${routeConfigs}];
146
132
  if (id === resolvedVirtualModuleId) {
147
133
  const routes = scanDirectory("");
148
134
  return {
149
- code: generateRouteCode(routes),
150
- lang: "tsx"
135
+ code: generateRouteCode(routes)
136
+ };
137
+ }
138
+ },
139
+ async transform(code, id) {
140
+ if (id === resolvedVirtualModuleId) {
141
+ const result = await transform(code, {
142
+ loader: "tsx",
143
+ jsx: "automatic",
144
+ target: "esnext"
145
+ });
146
+ return {
147
+ code: result.code,
148
+ map: null
151
149
  };
152
150
  }
153
151
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samadhi1311/router",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "A simple router for Vite inspired by Next.JS App Router",
5
5
  "author": {
6
6
  "name": "Samadhi Gunasinghe",
@@ -51,5 +51,8 @@
51
51
  "devDependencies": {
52
52
  "tsup": "^8.5.1",
53
53
  "typescript": "^5.9.3"
54
+ },
55
+ "dependencies": {
56
+ "esbuild": "^0.27.2"
54
57
  }
55
58
  }