upcoming.js 1.0.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/LICENSE +21 -0
- package/generator.js +20 -0
- package/index.js +80 -0
- package/package.json +42 -0
- package/readme.md +319 -0
- package/runtime/Router.jsx +18 -0
- package/scanner.js +87 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Amir.Mahdi Sultani
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/generator.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function generateRoutesModule(routes) {
|
|
2
|
+
|
|
3
|
+
const imports = routes.map((route, index) => {
|
|
4
|
+
return `const Page${index} = lazy(() => import('${route.file}'))`
|
|
5
|
+
}).join('\n')
|
|
6
|
+
|
|
7
|
+
const routeItems = routes.map((route, index) => {
|
|
8
|
+
return ` { path: '${route.path}', element: React.createElement(Page${index}) }`
|
|
9
|
+
}).join(',\n')
|
|
10
|
+
|
|
11
|
+
return `
|
|
12
|
+
import React, { lazy } from 'react'
|
|
13
|
+
|
|
14
|
+
${imports}
|
|
15
|
+
|
|
16
|
+
export const routes = [
|
|
17
|
+
${routeItems}
|
|
18
|
+
]
|
|
19
|
+
`.trim()
|
|
20
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import { scanRoutes } from './scanner.js'
|
|
3
|
+
import { generateRoutesModule } from './generator.js'
|
|
4
|
+
|
|
5
|
+
const VIRTUAL_MODULE_ID = 'virtual:upcoming'
|
|
6
|
+
const RESOLVED_ID = '\0virtual:upcoming'
|
|
7
|
+
|
|
8
|
+
export default function upcoming(options = {}) {
|
|
9
|
+
|
|
10
|
+
const routesDir = options.routesDir
|
|
11
|
+
? path.resolve(process.cwd(), options.routesDir)
|
|
12
|
+
: path.resolve(process.cwd(), 'src')
|
|
13
|
+
|
|
14
|
+
let generatedModule = ''
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
name: 'upcoming.js',
|
|
18
|
+
|
|
19
|
+
buildStart() {
|
|
20
|
+
const routes = scanRoutes(routesDir)
|
|
21
|
+
generatedModule = generateRoutesModule(routes)
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
resolveId(id) {
|
|
25
|
+
if (id === VIRTUAL_MODULE_ID) {
|
|
26
|
+
return RESOLVED_ID
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
load(id) {
|
|
31
|
+
if (id === RESOLVED_ID) {
|
|
32
|
+
return generatedModule
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
configureServer(server) {
|
|
38
|
+
const reload = (filePath) => {
|
|
39
|
+
const isPageFile = /page\.(jsx?|tsx?)$/.test(filePath)
|
|
40
|
+
const isNotFoundFile = /notfound\.(jsx?|tsx?)$/.test(filePath)
|
|
41
|
+
|
|
42
|
+
if (isPageFile || isNotFoundFile) {
|
|
43
|
+
const routes = scanRoutes(routesDir)
|
|
44
|
+
generatedModule = generateRoutesModule(routes)
|
|
45
|
+
|
|
46
|
+
const virtualModule = server.moduleGraph.getModuleById(RESOLVED_ID)
|
|
47
|
+
if (virtualModule) {
|
|
48
|
+
server.moduleGraph.invalidateModule(virtualModule)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
server.ws.send({ type: 'full-reload' })
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
server.watcher.on('add', reload)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
server.watcher.on('unlink', reload)
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
handleHotUpdate({ file, server }) {
|
|
63
|
+
const isInRoutesDir = file.startsWith(routesDir)
|
|
64
|
+
const isPageFile = /page\.(jsx?|tsx?)$/.test(file)
|
|
65
|
+
const isNotFoundFile = /notfound\.(jsx?|tsx?)$/.test(file)
|
|
66
|
+
|
|
67
|
+
if (isInRoutesDir && (isPageFile || isNotFoundFile)) {
|
|
68
|
+
const routes = scanRoutes(routesDir)
|
|
69
|
+
generatedModule = generateRoutesModule(routes)
|
|
70
|
+
|
|
71
|
+
const virtualModule = server.moduleGraph.getModuleById(RESOLVED_ID)
|
|
72
|
+
if (virtualModule) {
|
|
73
|
+
server.moduleGraph.invalidateModule(virtualModule)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
server.ws.send({ type: 'full-reload' })
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "upcoming.js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.js",
|
|
8
|
+
"./runtime": "./runtime/Router.jsx"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"vite",
|
|
15
|
+
"react",
|
|
16
|
+
"file-based-routing",
|
|
17
|
+
"router",
|
|
18
|
+
"next.js",
|
|
19
|
+
"upcoming",
|
|
20
|
+
"upcoming.js",
|
|
21
|
+
"next",
|
|
22
|
+
"js",
|
|
23
|
+
"javascript",
|
|
24
|
+
"node",
|
|
25
|
+
"node.js",
|
|
26
|
+
"react-router",
|
|
27
|
+
"react-router-dom",
|
|
28
|
+
"next.js-routing"
|
|
29
|
+
],
|
|
30
|
+
"author": "Amir.Mahdi Sultani",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"description": "a plugin package for react vite for adding the file and folder based routing feature based according to nextjs",
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"react": ">=18",
|
|
35
|
+
"react-dom": ">=18",
|
|
36
|
+
"react-router": "^7.13.1",
|
|
37
|
+
"vite": ">=4"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"chalk": "^5.6.2"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<br />
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
██╗ ██╗██████╗ ██████╗ ██████╗ ███╗ ███╗██╗███╗ ██╗ ██████╗
|
|
8
|
+
██║ ██║██╔══██╗██╔════╝██╔═══██╗████╗ ████║██║████╗ ██║██╔════╝
|
|
9
|
+
██║ ██║██████╔╝██║ ██║ ██║██╔████╔██║██║██╔██╗ ██║██║ ███╗
|
|
10
|
+
██║ ██║██╔═══╝ ██║ ██║ ██║██║╚██╔╝██║██║██║╚██╗██║██║ ██║
|
|
11
|
+
╚██████╔╝██║ ╚██████╗╚██████╔╝██║ ╚═╝ ██║██║██║ ╚████║╚██████╔╝
|
|
12
|
+
╚═════╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝ ╚═════╝
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### File-based routing for React + Vite. Inspired by Next.js. Built for those who love React.
|
|
19
|
+
|
|
20
|
+
<br />
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
[](https://www.npmjs.com/package/upcoming.js)
|
|
24
|
+
[](https://www.npmjs.com/package/upcoming.js)
|
|
25
|
+
[](https://github.com/amirmahdi390/upcoming.js/blob/main/LICENSE)
|
|
26
|
+
[](https://github.com/amirmahdi390/upcoming.js)
|
|
27
|
+
|
|
28
|
+
<br />
|
|
29
|
+
|
|
30
|
+
</div>
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Why I Built This
|
|
41
|
+
|
|
42
|
+
I love Next.js's file-based routing. The idea of just creating a folder and a file and having a route automatically appear — no configuration, no imports, no `<Route>` declarations — is genuinely brilliant.
|
|
43
|
+
|
|
44
|
+
**But I don't want to use Next.js.**
|
|
45
|
+
|
|
46
|
+
I want React. I want Vite. I want the speed, the freedom, and the simplicity of a plain React project — without being forced into a full framework just to get one feature I love.
|
|
47
|
+
|
|
48
|
+
So I built **upcoming.js** — Next.js-style file routing, for React + Vite. Nothing more, nothing less.
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## What is upcoming.js?
|
|
53
|
+
|
|
54
|
+
**upcoming.js** is a Vite plugin that watches your folder structure and automatically generates routes for React Router — no setup, no manual route declarations, no `<BrowserRouter>` wiring.
|
|
55
|
+
|
|
56
|
+
You create folders. It creates routes.
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
src/
|
|
60
|
+
├── page.jsx → /
|
|
61
|
+
├── about/
|
|
62
|
+
│ └── page.jsx → /about
|
|
63
|
+
├── blog/
|
|
64
|
+
│ ├── page.jsx → /blog
|
|
65
|
+
│ └── [id]/
|
|
66
|
+
│ └── page.jsx → /blog/:id
|
|
67
|
+
└── notfound.jsx → 404
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
That's it. That's the whole idea.
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Features
|
|
75
|
+
|
|
76
|
+
* 📁 **Folder-based routing** — your file structure is your route structure
|
|
77
|
+
* ⚡ **Vite plugin** — zero config, plugs directly into your existing Vite setup
|
|
78
|
+
* 🔄 **Live route updates** — add or delete a `page.jsx` and the routes update instantly, no server restart needed
|
|
79
|
+
* 🔀 **Dynamic routes** — `[param]` folders become `:param` in the URL
|
|
80
|
+
* 🗂️ **Route groups** — `(group)` folders organize your code without affecting the URL
|
|
81
|
+
* 🙈 **Private folders** — `_folder` folders are completely ignored by the router
|
|
82
|
+
* 🚫 **404 pages** — just create `notfound.jsx` at the root of your routes folder
|
|
83
|
+
* 💤 **Lazy loading** — every page is automatically code-split and lazy loaded
|
|
84
|
+
* 🤝 **React Router powered** — all navigation, params, and hooks work exactly as you already know
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Installation
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
npm install upcoming.js
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
> **upcoming.js** will automatically install `react-router` as a dependency.
|
|
95
|
+
> Make sure you have `react`, `react-dom`, and `vite` already in your project.
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Setup
|
|
100
|
+
|
|
101
|
+
### 1. Add the plugin to `vite.config.js`
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
import { defineConfig } from 'vite'
|
|
105
|
+
import react from '@vitejs/plugin-react'
|
|
106
|
+
import upcoming from 'upcoming.js'
|
|
107
|
+
|
|
108
|
+
export default defineConfig({
|
|
109
|
+
plugins: [
|
|
110
|
+
react(),
|
|
111
|
+
upcoming()
|
|
112
|
+
],
|
|
113
|
+
resolve: {
|
|
114
|
+
dedupe: ['react', 'react-dom', 'react-router']
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
> The `dedupe` line prevents duplicate React instances — always include it.
|
|
120
|
+
|
|
121
|
+
### 2. Replace your `src/main.jsx`
|
|
122
|
+
|
|
123
|
+
```jsx
|
|
124
|
+
import ReactDOM from 'react-dom/client'
|
|
125
|
+
import { UpcomingRouter } from 'upcoming.js/runtime'
|
|
126
|
+
|
|
127
|
+
ReactDOM.createRoot(document.getElementById('root')).render(
|
|
128
|
+
<UpcomingRouter />
|
|
129
|
+
)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Done. No `App.jsx`, no `<BrowserRouter>`, no route declarations. Just run `npm run dev`.
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Folder & File Conventions
|
|
137
|
+
|
|
138
|
+
### `page.jsx` — defines a route
|
|
139
|
+
|
|
140
|
+
Every route is a `page.jsx` (or `page.js`) file. The folder path becomes the URL.
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
src/page.jsx → /
|
|
144
|
+
src/about/page.jsx → /about
|
|
145
|
+
src/settings/account/page.jsx → /settings/account
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
### `[param]` — dynamic segments
|
|
151
|
+
|
|
152
|
+
Wrap a folder name in square brackets to make it a dynamic route parameter.
|
|
153
|
+
|
|
154
|
+
```
|
|
155
|
+
src/blog/[id]/page.jsx → /blog/:id
|
|
156
|
+
src/shop/[category]/page.jsx → /shop/:category
|
|
157
|
+
src/user/[id]/posts/page.jsx → /user/:id/posts
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Accessing the param works exactly like React Router:
|
|
161
|
+
|
|
162
|
+
```jsx
|
|
163
|
+
import { useParams } from 'react-router'
|
|
164
|
+
|
|
165
|
+
export default function BlogPost() {
|
|
166
|
+
const { id } = useParams()
|
|
167
|
+
return <h1>Post ID: {id}</h1>
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
### `(group)` — route groups
|
|
174
|
+
|
|
175
|
+
Wrap a folder name in parentheses to group routes without affecting the URL. Great for organizing by feature or layout without polluting the URL.
|
|
176
|
+
|
|
177
|
+
```
|
|
178
|
+
src/(auth)/login/page.jsx → /login
|
|
179
|
+
src/(auth)/signup/page.jsx → /signup
|
|
180
|
+
src/(dashboard)/home/page.jsx → /home
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
### `_folder` — private folders
|
|
186
|
+
|
|
187
|
+
Prefix a folder name with `_` to exclude it from routing entirely. Use this for shared components, utilities, or anything that lives inside your routes folder but is not a route.
|
|
188
|
+
|
|
189
|
+
```
|
|
190
|
+
src/_components/Navbar.jsx → ignored
|
|
191
|
+
src/_utils/helpers.js → ignored
|
|
192
|
+
src/about/page.jsx → /about ✓
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
### `notfound.jsx` — 404 page
|
|
198
|
+
|
|
199
|
+
Place a `notfound.jsx` at the root of your routes folder to handle unmatched routes.
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
src/notfound.jsx → * (catches all unmatched routes)
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
```jsx
|
|
206
|
+
export default function NotFound() {
|
|
207
|
+
return <h1>404 — Page not found</h1>
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## Navigation
|
|
214
|
+
|
|
215
|
+
Navigation works exactly like React Router. **upcoming.js** is just an API on top of it — all hooks and components are the same.
|
|
216
|
+
|
|
217
|
+
```jsx
|
|
218
|
+
import { Link, useNavigate, useParams, useLocation } from 'react-router'
|
|
219
|
+
|
|
220
|
+
// Link component
|
|
221
|
+
<Link to="/about">About</Link>
|
|
222
|
+
|
|
223
|
+
// Programmatic navigation
|
|
224
|
+
const navigate = useNavigate()
|
|
225
|
+
navigate('/blog/123')
|
|
226
|
+
|
|
227
|
+
// Route params
|
|
228
|
+
const { id } = useParams()
|
|
229
|
+
|
|
230
|
+
// Current location
|
|
231
|
+
const location = useLocation()
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
## Configuration Options
|
|
237
|
+
|
|
238
|
+
```js
|
|
239
|
+
upcoming({
|
|
240
|
+
routesDir: 'src' // the folder to scan for routes — default is 'src'
|
|
241
|
+
})
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
If you use a feature-based architecture or want routes in a different folder:
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
upcoming({
|
|
248
|
+
routesDir: 'src/pages'
|
|
249
|
+
})
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
## Custom Loading State
|
|
255
|
+
|
|
256
|
+
You can pass a custom loading component that shows while lazy-loaded pages are being fetched:
|
|
257
|
+
|
|
258
|
+
```jsx
|
|
259
|
+
<UpcomingRouter loading={<div>Loading...</div>} />
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
## Full Example Structure
|
|
265
|
+
|
|
266
|
+
```
|
|
267
|
+
src/
|
|
268
|
+
├── page.jsx → /
|
|
269
|
+
├── notfound.jsx → 404
|
|
270
|
+
├── about/
|
|
271
|
+
│ └── page.jsx → /about
|
|
272
|
+
├── blog/
|
|
273
|
+
│ ├── page.jsx → /blog
|
|
274
|
+
│ └── [id]/
|
|
275
|
+
│ └── page.jsx → /blog/:id
|
|
276
|
+
├── (auth)/
|
|
277
|
+
│ ├── login/
|
|
278
|
+
│ │ └── page.jsx → /login
|
|
279
|
+
│ └── signup/
|
|
280
|
+
│ └── page.jsx → /signup
|
|
281
|
+
├── shop/
|
|
282
|
+
│ └── [category]/
|
|
283
|
+
│ └── [product]/
|
|
284
|
+
│ └── page.jsx → /shop/:category/:product
|
|
285
|
+
└── _components/
|
|
286
|
+
└── Navbar.jsx → ignored
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
## Requirements
|
|
292
|
+
|
|
293
|
+
| Dependency | Version |
|
|
294
|
+
| ---------- | ------- |
|
|
295
|
+
| React | ≥ 18 |
|
|
296
|
+
| React DOM | ≥ 18 |
|
|
297
|
+
| Vite | ≥ 4 |
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
## Links
|
|
302
|
+
|
|
303
|
+
* 📦 [npm](https://www.npmjs.com/package/upcoming.js)
|
|
304
|
+
* 🐙 [GitHub](https://github.com/amirmahdi390/upcoming.js)
|
|
305
|
+
|
|
306
|
+
---
|
|
307
|
+
|
|
308
|
+
## License
|
|
309
|
+
|
|
310
|
+
MIT © [Amir.Mahdi Sultani](https://github.com/amirmahdi390)
|
|
311
|
+
|
|
312
|
+
---
|
|
313
|
+
|
|
314
|
+
<div align="center">
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
**If upcoming.js saved you time, give it a ⭐ on [GitHub](https://github.com/amirmahdi390/upcoming.js)**
|
|
318
|
+
|
|
319
|
+
</div>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Suspense } from 'react'
|
|
2
|
+
import { BrowserRouter, useRoutes } from 'react-router'
|
|
3
|
+
import { routes } from 'virtual:upcoming'
|
|
4
|
+
|
|
5
|
+
function AppRoutes() {
|
|
6
|
+
const element = useRoutes(routes)
|
|
7
|
+
return element
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function UpcomingRouter({ loading = <div>Loading...</div> }) {
|
|
11
|
+
return (
|
|
12
|
+
<BrowserRouter>
|
|
13
|
+
<Suspense fallback={loading}>
|
|
14
|
+
<AppRoutes />
|
|
15
|
+
</Suspense>
|
|
16
|
+
</BrowserRouter>
|
|
17
|
+
)
|
|
18
|
+
}
|
package/scanner.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import fs from "fs"
|
|
2
|
+
import path from "path"
|
|
3
|
+
import chalk from "chalk"
|
|
4
|
+
export function scanRoutes(routesDir) {
|
|
5
|
+
|
|
6
|
+
let routesObject = [
|
|
7
|
+
|
|
8
|
+
]
|
|
9
|
+
|
|
10
|
+
function folderChecker(folderPath) {
|
|
11
|
+
|
|
12
|
+
let folders = fs.readdirSync(folderPath)
|
|
13
|
+
|
|
14
|
+
folders.forEach(folder => {
|
|
15
|
+
|
|
16
|
+
let fullPath = path.join(folderPath, folder)
|
|
17
|
+
|
|
18
|
+
let stat = fs.statSync(fullPath)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
if (stat.isDirectory()) {
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
if (!folder.startsWith("_")) {
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
folderChecker(fullPath)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
} else {
|
|
31
|
+
let fileName = path.basename(fullPath).toLowerCase()
|
|
32
|
+
if (fileName == "page.jsx"|| fileName == "page.js") {
|
|
33
|
+
|
|
34
|
+
let route = `${path.relative(routesDir,path.dirname(fullPath)).replace(/\\/g,"/")}`;
|
|
35
|
+
|
|
36
|
+
let sp = route.split("/")
|
|
37
|
+
let routes = sp.map((r)=>{
|
|
38
|
+
if (r.includes("(") && r.includes(")")) {
|
|
39
|
+
return null
|
|
40
|
+
}
|
|
41
|
+
if (r.includes("[")&& r.includes("]")) {
|
|
42
|
+
let a = `:${r.replace(/\[|\]/g,"")}`
|
|
43
|
+
return a
|
|
44
|
+
}else{
|
|
45
|
+
return r
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
}).filter(Boolean)
|
|
50
|
+
let finallr =`/${ routes.join("/")}`
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
let routeOBJ = {
|
|
54
|
+
path:finallr,
|
|
55
|
+
file:fullPath.replace(/\\/g, "/")
|
|
56
|
+
}
|
|
57
|
+
routesObject.push(routeOBJ)
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if(fileName == "notfound.js" || fileName == "notfound.jsx" ){
|
|
62
|
+
let filePath = path.dirname(fullPath)
|
|
63
|
+
let rel = path.relative(routesDir,filePath).replace(/\\/g, "/")
|
|
64
|
+
if (rel ==""){
|
|
65
|
+
let notfoundOBJ = {
|
|
66
|
+
path : "*",
|
|
67
|
+
file:fullPath.replace(/\\/g, "/")
|
|
68
|
+
}
|
|
69
|
+
routesObject.push(notfoundOBJ)
|
|
70
|
+
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
folderChecker(routesDir)
|
|
82
|
+
console.log(chalk.blue("routes created successfully"));
|
|
83
|
+
|
|
84
|
+
return routesObject
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
}
|