@bromscandium/vite-plugin 1.0.0 → 1.0.2
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 +20 -20
- package/README.md +71 -71
- package/dist/codegen.js +21 -21
- package/package.json +43 -43
- package/src/codegen.ts +127 -127
- package/src/index.ts +9 -9
- package/src/plugin.ts +151 -151
- package/src/scanner.ts +283 -283
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 bromscandium
|
|
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
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 bromscandium
|
|
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
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
# @bromscandium/vite-plugin
|
|
2
|
-
|
|
3
|
-
Vite plugin for BromiumJS with file-based routing and HMR support.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @bromscandium/vite-plugin -D
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Usage
|
|
12
|
-
|
|
13
|
-
```ts
|
|
14
|
-
// vite.config.ts
|
|
15
|
-
import { defineConfig } from 'vite';
|
|
16
|
-
import bromium from '@bromscandium/vite-plugin';
|
|
17
|
-
|
|
18
|
-
export default defineConfig({
|
|
19
|
-
plugins: [
|
|
20
|
-
bromium({
|
|
21
|
-
pagesDir: 'src/pages',
|
|
22
|
-
routesOutput: 'src/routes.generated.ts',
|
|
23
|
-
generateTypes: true,
|
|
24
|
-
})
|
|
25
|
-
],
|
|
26
|
-
esbuild: {
|
|
27
|
-
jsx: 'automatic',
|
|
28
|
-
jsxImportSource: 'bromium'
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## File-based Routing
|
|
34
|
-
|
|
35
|
-
```
|
|
36
|
-
src/pages/
|
|
37
|
-
├── page.tsx → /
|
|
38
|
-
├── about.tsx → /about
|
|
39
|
-
├── users/
|
|
40
|
-
│ ├── page.tsx → /users
|
|
41
|
-
│ ├── [id].tsx → /users/:id
|
|
42
|
-
│ └── [id]/
|
|
43
|
-
│ └── settings.tsx → /users/:id/settings
|
|
44
|
-
├── posts/
|
|
45
|
-
│ └── [...slug].tsx → /posts/:slug* (catch-all)
|
|
46
|
-
└── (auth)/ → Route group (no URL segment)
|
|
47
|
-
├── login.tsx → /login
|
|
48
|
-
└── register.tsx → /register
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
| Pattern | Example | Description |
|
|
52
|
-
|---------|---------|-------------|
|
|
53
|
-
| `page.tsx` | `/` | Index route |
|
|
54
|
-
| `about.tsx` | `/about` | Static route |
|
|
55
|
-
| `[id].tsx` | `/users/:id` | Dynamic segment |
|
|
56
|
-
| `[...slug].tsx` | `/posts/:slug*` | Catch-all route |
|
|
57
|
-
| `(folder)/` | - | Route group |
|
|
58
|
-
| `layout.tsx` | - | Layout component |
|
|
59
|
-
|
|
60
|
-
## Options
|
|
61
|
-
|
|
62
|
-
| Option | Default | Description |
|
|
63
|
-
|--------|---------|-------------|
|
|
64
|
-
| `pagesDir` | `'src/pages'` | Pages directory |
|
|
65
|
-
| `routesOutput` | `'src/routes.generated.ts'` | Output file |
|
|
66
|
-
| `base` | `''` | Base path for routes |
|
|
67
|
-
| `generateTypes` | `true` | Generate TypeScript types |
|
|
68
|
-
|
|
69
|
-
## License
|
|
70
|
-
|
|
71
|
-
MIT
|
|
1
|
+
# @bromscandium/vite-plugin
|
|
2
|
+
|
|
3
|
+
Vite plugin for BromiumJS with file-based routing and HMR support.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @bromscandium/vite-plugin -D
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
// vite.config.ts
|
|
15
|
+
import { defineConfig } from 'vite';
|
|
16
|
+
import bromium from '@bromscandium/vite-plugin';
|
|
17
|
+
|
|
18
|
+
export default defineConfig({
|
|
19
|
+
plugins: [
|
|
20
|
+
bromium({
|
|
21
|
+
pagesDir: 'src/pages',
|
|
22
|
+
routesOutput: 'src/routes.generated.ts',
|
|
23
|
+
generateTypes: true,
|
|
24
|
+
})
|
|
25
|
+
],
|
|
26
|
+
esbuild: {
|
|
27
|
+
jsx: 'automatic',
|
|
28
|
+
jsxImportSource: 'bromium'
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## File-based Routing
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
src/pages/
|
|
37
|
+
├── page.tsx → /
|
|
38
|
+
├── about.tsx → /about
|
|
39
|
+
├── users/
|
|
40
|
+
│ ├── page.tsx → /users
|
|
41
|
+
│ ├── [id].tsx → /users/:id
|
|
42
|
+
│ └── [id]/
|
|
43
|
+
│ └── settings.tsx → /users/:id/settings
|
|
44
|
+
├── posts/
|
|
45
|
+
│ └── [...slug].tsx → /posts/:slug* (catch-all)
|
|
46
|
+
└── (auth)/ → Route group (no URL segment)
|
|
47
|
+
├── login.tsx → /login
|
|
48
|
+
└── register.tsx → /register
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
| Pattern | Example | Description |
|
|
52
|
+
|---------|---------|-------------|
|
|
53
|
+
| `page.tsx` | `/` | Index route |
|
|
54
|
+
| `about.tsx` | `/about` | Static route |
|
|
55
|
+
| `[id].tsx` | `/users/:id` | Dynamic segment |
|
|
56
|
+
| `[...slug].tsx` | `/posts/:slug*` | Catch-all route |
|
|
57
|
+
| `(folder)/` | - | Route group |
|
|
58
|
+
| `layout.tsx` | - | Layout component |
|
|
59
|
+
|
|
60
|
+
## Options
|
|
61
|
+
|
|
62
|
+
| Option | Default | Description |
|
|
63
|
+
|--------|---------|-------------|
|
|
64
|
+
| `pagesDir` | `'src/pages'` | Pages directory |
|
|
65
|
+
| `routesOutput` | `'src/routes.generated.ts'` | Output file |
|
|
66
|
+
| `base` | `''` | Base path for routes |
|
|
67
|
+
| `generateTypes` | `true` | Generate TypeScript types |
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
MIT
|
package/dist/codegen.js
CHANGED
|
@@ -43,22 +43,22 @@ export function generateRouteConfig(routes, options) {
|
|
|
43
43
|
layoutImport = `,\n layout: () => import('${layoutRelativePath}')`;
|
|
44
44
|
}
|
|
45
45
|
const routePath = route.routePath;
|
|
46
|
-
const routeObj = ` {
|
|
47
|
-
path: '${routePath}',
|
|
48
|
-
component: () => import('${relativePath}')${layoutImport},
|
|
46
|
+
const routeObj = ` {
|
|
47
|
+
path: '${routePath}',
|
|
48
|
+
component: () => import('${relativePath}')${layoutImport},
|
|
49
49
|
}`;
|
|
50
50
|
routeObjects.push(routeObj);
|
|
51
51
|
});
|
|
52
|
-
return `// Auto-generated by @bromscandium/vite-plugin
|
|
53
|
-
// Do not edit manually - changes will be overwritten
|
|
54
|
-
|
|
55
|
-
import type { Route } from '@bromscandium/router';
|
|
56
|
-
|
|
57
|
-
export const routes: Route[] = [
|
|
58
|
-
${routeObjects.join(',\n')}
|
|
59
|
-
];
|
|
60
|
-
|
|
61
|
-
export default routes;
|
|
52
|
+
return `// Auto-generated by @bromscandium/vite-plugin
|
|
53
|
+
// Do not edit manually - changes will be overwritten
|
|
54
|
+
|
|
55
|
+
import type { Route } from '@bromscandium/router';
|
|
56
|
+
|
|
57
|
+
export const routes: Route[] = [
|
|
58
|
+
${routeObjects.join(',\n')}
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
export default routes;
|
|
62
62
|
`;
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
@@ -84,14 +84,14 @@ export function generateRouteTypes(routes) {
|
|
|
84
84
|
return ` '${r.routePath}': {\n${params}\n };`;
|
|
85
85
|
})
|
|
86
86
|
.join('\n');
|
|
87
|
-
return `// Auto-generated route types
|
|
88
|
-
|
|
89
|
-
export type RoutePath =
|
|
90
|
-
${routePaths || " | '/'"}
|
|
91
|
-
|
|
92
|
-
export interface RouteParams {
|
|
93
|
-
${paramTypes || " '/': Record<string, never>;"}
|
|
94
|
-
}
|
|
87
|
+
return `// Auto-generated route types
|
|
88
|
+
|
|
89
|
+
export type RoutePath =
|
|
90
|
+
${routePaths || " | '/'"}
|
|
91
|
+
|
|
92
|
+
export interface RouteParams {
|
|
93
|
+
${paramTypes || " '/': Record<string, never>;"}
|
|
94
|
+
}
|
|
95
95
|
`;
|
|
96
96
|
}
|
|
97
97
|
//# sourceMappingURL=codegen.js.map
|
package/package.json
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@bromscandium/vite-plugin",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Vite plugin for BromiumJS file-based routing",
|
|
5
|
-
"author": "bromscandium",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/bromscandium/bromiumjs.git",
|
|
10
|
-
"directory": "packages/vite-plugin"
|
|
11
|
-
},
|
|
12
|
-
"homepage": "https://github.com/bromscandium/bromiumjs#readme",
|
|
13
|
-
"bugs": {
|
|
14
|
-
"url": "https://github.com/bromscandium/bromiumjs/issues"
|
|
15
|
-
},
|
|
16
|
-
"keywords": ["bromium", "bromiumjs", "vite", "vite-plugin", "file-based-routing"],
|
|
17
|
-
"type": "module",
|
|
18
|
-
"main": "./dist/index.js",
|
|
19
|
-
"module": "./dist/index.js",
|
|
20
|
-
"types": "./dist/index.d.ts",
|
|
21
|
-
"exports": {
|
|
22
|
-
".": {
|
|
23
|
-
"import": "./dist/index.js",
|
|
24
|
-
"types": "./dist/index.d.ts"
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"files": [
|
|
28
|
-
"dist",
|
|
29
|
-
"src"
|
|
30
|
-
],
|
|
31
|
-
"scripts": {
|
|
32
|
-
"build": "tsc",
|
|
33
|
-
"dev": "tsc --watch"
|
|
34
|
-
},
|
|
35
|
-
"peerDependencies": {
|
|
36
|
-
"vite": "^5.0.0 || ^6.0.0"
|
|
37
|
-
},
|
|
38
|
-
"devDependencies": {
|
|
39
|
-
"@types/node": "^25.0.9",
|
|
40
|
-
"typescript": "^5.9.3",
|
|
41
|
-
"vite": "^7.3.1"
|
|
42
|
-
}
|
|
43
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@bromscandium/vite-plugin",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Vite plugin for BromiumJS file-based routing",
|
|
5
|
+
"author": "bromscandium",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/bromscandium/bromiumjs.git",
|
|
10
|
+
"directory": "packages/vite-plugin"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/bromscandium/bromiumjs#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/bromscandium/bromiumjs/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": ["bromium", "bromiumjs", "vite", "vite-plugin", "file-based-routing"],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"module": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"src"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc",
|
|
33
|
+
"dev": "tsc --watch"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"vite": "^5.0.0 || ^6.0.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^25.0.9",
|
|
40
|
+
"typescript": "^5.9.3",
|
|
41
|
+
"vite": "^7.3.1"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/codegen.ts
CHANGED
|
@@ -1,127 +1,127 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Route code generation utilities.
|
|
3
|
-
* @module
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {ScannedRoute} from './scanner.js';
|
|
7
|
-
import * as path from 'path';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Options for route code generation.
|
|
11
|
-
*/
|
|
12
|
-
export interface CodegenOptions {
|
|
13
|
-
/** The pages directory path */
|
|
14
|
-
pagesDir: string;
|
|
15
|
-
/** The output file path for generated routes */
|
|
16
|
-
outputPath: string;
|
|
17
|
-
/** Optional base path for routes */
|
|
18
|
-
base?: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Generates TypeScript code for route configuration from scanned routes.
|
|
23
|
-
* The output exports a `routes` array compatible with the Bromium router.
|
|
24
|
-
*
|
|
25
|
-
* @param routes - Array of scanned route information
|
|
26
|
-
* @param options - Code generation options
|
|
27
|
-
* @returns Generated TypeScript source code
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```ts
|
|
31
|
-
* const routes = scanPages('./src/pages');
|
|
32
|
-
* const code = generateRouteConfig(routes, {
|
|
33
|
-
* pagesDir: './src/pages',
|
|
34
|
-
* outputPath: './src/routes.generated.ts',
|
|
35
|
-
* });
|
|
36
|
-
* fs.writeFileSync('./src/routes.generated.ts', code);
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
export function generateRouteConfig(
|
|
40
|
-
routes: ScannedRoute[],
|
|
41
|
-
options: CodegenOptions
|
|
42
|
-
): string {
|
|
43
|
-
const { outputPath } = options;
|
|
44
|
-
|
|
45
|
-
const outputDir = path.dirname(outputPath);
|
|
46
|
-
|
|
47
|
-
const routeObjects: string[] = [];
|
|
48
|
-
|
|
49
|
-
routes.forEach((route) => {
|
|
50
|
-
let relativePath = path.relative(outputDir, route.filePath);
|
|
51
|
-
relativePath = relativePath.replace(/\\/g, '/');
|
|
52
|
-
|
|
53
|
-
relativePath = relativePath.replace(/\.(tsx?|jsx?)$/, '');
|
|
54
|
-
|
|
55
|
-
if (!relativePath.startsWith('.') && !relativePath.startsWith('/')) {
|
|
56
|
-
relativePath = './' + relativePath;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
let layoutImport = '';
|
|
60
|
-
if (route.layoutPath) {
|
|
61
|
-
let layoutRelativePath = path.relative(outputDir, route.layoutPath);
|
|
62
|
-
layoutRelativePath = layoutRelativePath.replace(/\\/g, '/');
|
|
63
|
-
layoutRelativePath = layoutRelativePath.replace(/\.(tsx?|jsx?)$/, '');
|
|
64
|
-
if (!layoutRelativePath.startsWith('.') && !layoutRelativePath.startsWith('/')) {
|
|
65
|
-
layoutRelativePath = './' + layoutRelativePath;
|
|
66
|
-
}
|
|
67
|
-
layoutImport = `,\n layout: () => import('${layoutRelativePath}')`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const routePath = route.routePath;
|
|
71
|
-
|
|
72
|
-
const routeObj = ` {
|
|
73
|
-
path: '${routePath}',
|
|
74
|
-
component: () => import('${relativePath}')${layoutImport},
|
|
75
|
-
}`;
|
|
76
|
-
|
|
77
|
-
routeObjects.push(routeObj);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
return `// Auto-generated by @bromscandium/vite-plugin
|
|
81
|
-
// Do not edit manually - changes will be overwritten
|
|
82
|
-
|
|
83
|
-
import type { Route } from '@bromscandium/router';
|
|
84
|
-
|
|
85
|
-
export const routes: Route[] = [
|
|
86
|
-
${routeObjects.join(',\n')}
|
|
87
|
-
];
|
|
88
|
-
|
|
89
|
-
export default routes;
|
|
90
|
-
`;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Generates TypeScript type definitions for routes.
|
|
95
|
-
* Provides type-safe route paths and parameter types.
|
|
96
|
-
*
|
|
97
|
-
* @param routes - Array of scanned route information
|
|
98
|
-
* @returns Generated TypeScript declaration code
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```ts
|
|
102
|
-
* const routes = scanPages('./src/pages');
|
|
103
|
-
* const types = generateRouteTypes(routes);
|
|
104
|
-
* fs.writeFileSync('./src/routes.generated.d.ts', types);
|
|
105
|
-
* ```
|
|
106
|
-
*/
|
|
107
|
-
export function generateRouteTypes(routes: ScannedRoute[]): string {
|
|
108
|
-
const routePaths = routes.map(r => ` | '${r.routePath}'`).join('\n');
|
|
109
|
-
|
|
110
|
-
const paramTypes = routes
|
|
111
|
-
.filter(r => r.params.length > 0)
|
|
112
|
-
.map(r => {
|
|
113
|
-
const params = r.params.map(p => ` ${p}: string;`).join('\n');
|
|
114
|
-
return ` '${r.routePath}': {\n${params}\n };`;
|
|
115
|
-
})
|
|
116
|
-
.join('\n');
|
|
117
|
-
|
|
118
|
-
return `// Auto-generated route types
|
|
119
|
-
|
|
120
|
-
export type RoutePath =
|
|
121
|
-
${routePaths || " | '/'"}
|
|
122
|
-
|
|
123
|
-
export interface RouteParams {
|
|
124
|
-
${paramTypes || " '/': Record<string, never>;"}
|
|
125
|
-
}
|
|
126
|
-
`;
|
|
127
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Route code generation utilities.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {ScannedRoute} from './scanner.js';
|
|
7
|
+
import * as path from 'path';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Options for route code generation.
|
|
11
|
+
*/
|
|
12
|
+
export interface CodegenOptions {
|
|
13
|
+
/** The pages directory path */
|
|
14
|
+
pagesDir: string;
|
|
15
|
+
/** The output file path for generated routes */
|
|
16
|
+
outputPath: string;
|
|
17
|
+
/** Optional base path for routes */
|
|
18
|
+
base?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Generates TypeScript code for route configuration from scanned routes.
|
|
23
|
+
* The output exports a `routes` array compatible with the Bromium router.
|
|
24
|
+
*
|
|
25
|
+
* @param routes - Array of scanned route information
|
|
26
|
+
* @param options - Code generation options
|
|
27
|
+
* @returns Generated TypeScript source code
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const routes = scanPages('./src/pages');
|
|
32
|
+
* const code = generateRouteConfig(routes, {
|
|
33
|
+
* pagesDir: './src/pages',
|
|
34
|
+
* outputPath: './src/routes.generated.ts',
|
|
35
|
+
* });
|
|
36
|
+
* fs.writeFileSync('./src/routes.generated.ts', code);
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function generateRouteConfig(
|
|
40
|
+
routes: ScannedRoute[],
|
|
41
|
+
options: CodegenOptions
|
|
42
|
+
): string {
|
|
43
|
+
const { outputPath } = options;
|
|
44
|
+
|
|
45
|
+
const outputDir = path.dirname(outputPath);
|
|
46
|
+
|
|
47
|
+
const routeObjects: string[] = [];
|
|
48
|
+
|
|
49
|
+
routes.forEach((route) => {
|
|
50
|
+
let relativePath = path.relative(outputDir, route.filePath);
|
|
51
|
+
relativePath = relativePath.replace(/\\/g, '/');
|
|
52
|
+
|
|
53
|
+
relativePath = relativePath.replace(/\.(tsx?|jsx?)$/, '');
|
|
54
|
+
|
|
55
|
+
if (!relativePath.startsWith('.') && !relativePath.startsWith('/')) {
|
|
56
|
+
relativePath = './' + relativePath;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let layoutImport = '';
|
|
60
|
+
if (route.layoutPath) {
|
|
61
|
+
let layoutRelativePath = path.relative(outputDir, route.layoutPath);
|
|
62
|
+
layoutRelativePath = layoutRelativePath.replace(/\\/g, '/');
|
|
63
|
+
layoutRelativePath = layoutRelativePath.replace(/\.(tsx?|jsx?)$/, '');
|
|
64
|
+
if (!layoutRelativePath.startsWith('.') && !layoutRelativePath.startsWith('/')) {
|
|
65
|
+
layoutRelativePath = './' + layoutRelativePath;
|
|
66
|
+
}
|
|
67
|
+
layoutImport = `,\n layout: () => import('${layoutRelativePath}')`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const routePath = route.routePath;
|
|
71
|
+
|
|
72
|
+
const routeObj = ` {
|
|
73
|
+
path: '${routePath}',
|
|
74
|
+
component: () => import('${relativePath}')${layoutImport},
|
|
75
|
+
}`;
|
|
76
|
+
|
|
77
|
+
routeObjects.push(routeObj);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return `// Auto-generated by @bromscandium/vite-plugin
|
|
81
|
+
// Do not edit manually - changes will be overwritten
|
|
82
|
+
|
|
83
|
+
import type { Route } from '@bromscandium/router';
|
|
84
|
+
|
|
85
|
+
export const routes: Route[] = [
|
|
86
|
+
${routeObjects.join(',\n')}
|
|
87
|
+
];
|
|
88
|
+
|
|
89
|
+
export default routes;
|
|
90
|
+
`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Generates TypeScript type definitions for routes.
|
|
95
|
+
* Provides type-safe route paths and parameter types.
|
|
96
|
+
*
|
|
97
|
+
* @param routes - Array of scanned route information
|
|
98
|
+
* @returns Generated TypeScript declaration code
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* const routes = scanPages('./src/pages');
|
|
103
|
+
* const types = generateRouteTypes(routes);
|
|
104
|
+
* fs.writeFileSync('./src/routes.generated.d.ts', types);
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export function generateRouteTypes(routes: ScannedRoute[]): string {
|
|
108
|
+
const routePaths = routes.map(r => ` | '${r.routePath}'`).join('\n');
|
|
109
|
+
|
|
110
|
+
const paramTypes = routes
|
|
111
|
+
.filter(r => r.params.length > 0)
|
|
112
|
+
.map(r => {
|
|
113
|
+
const params = r.params.map(p => ` ${p}: string;`).join('\n');
|
|
114
|
+
return ` '${r.routePath}': {\n${params}\n };`;
|
|
115
|
+
})
|
|
116
|
+
.join('\n');
|
|
117
|
+
|
|
118
|
+
return `// Auto-generated route types
|
|
119
|
+
|
|
120
|
+
export type RoutePath =
|
|
121
|
+
${routePaths || " | '/'"}
|
|
122
|
+
|
|
123
|
+
export interface RouteParams {
|
|
124
|
+
${paramTypes || " '/': Record<string, never>;"}
|
|
125
|
+
}
|
|
126
|
+
`;
|
|
127
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// Vite plugin exports
|
|
2
|
-
|
|
3
|
-
import { bromiumPlugin } from './plugin.js';
|
|
4
|
-
|
|
5
|
-
export { bromiumPlugin, type BromiumPluginOptions } from './plugin.js';
|
|
6
|
-
export { scanPages, watchPages, type ScannedRoute } from './scanner.js';
|
|
7
|
-
export { generateRouteConfig, generateRouteTypes, type CodegenOptions } from './codegen.js';
|
|
8
|
-
|
|
9
|
-
export default bromiumPlugin;
|
|
1
|
+
// Vite plugin exports
|
|
2
|
+
|
|
3
|
+
import { bromiumPlugin } from './plugin.js';
|
|
4
|
+
|
|
5
|
+
export { bromiumPlugin, type BromiumPluginOptions } from './plugin.js';
|
|
6
|
+
export { scanPages, watchPages, type ScannedRoute } from './scanner.js';
|
|
7
|
+
export { generateRouteConfig, generateRouteTypes, type CodegenOptions } from './codegen.js';
|
|
8
|
+
|
|
9
|
+
export default bromiumPlugin;
|