@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/src/plugin.ts
CHANGED
|
@@ -1,151 +1,151 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Main Vite plugin for BromiumJS file-based routing.
|
|
3
|
-
* @module
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { Plugin, ResolvedConfig, ViteDevServer } from 'vite';
|
|
7
|
-
import * as path from 'path';
|
|
8
|
-
import * as fs from 'fs';
|
|
9
|
-
import { scanPages, watchPages } from './scanner.js';
|
|
10
|
-
import { generateRouteConfig, generateRouteTypes } from './codegen.js';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Configuration options for the Bromium Vite plugin.
|
|
14
|
-
*/
|
|
15
|
-
export interface BromiumPluginOptions {
|
|
16
|
-
/**
|
|
17
|
-
* Directory containing page components.
|
|
18
|
-
* @default 'src/pages'
|
|
19
|
-
*/
|
|
20
|
-
pagesDir?: string;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Output path for generated routes file.
|
|
24
|
-
* @default 'src/routes.generated.ts'
|
|
25
|
-
*/
|
|
26
|
-
routesOutput?: string;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Base path for all routes (e.g., '/app/').
|
|
30
|
-
* @default ''
|
|
31
|
-
*/
|
|
32
|
-
base?: string;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Generate TypeScript type definitions for routes.
|
|
36
|
-
* @default true
|
|
37
|
-
*/
|
|
38
|
-
generateTypes?: boolean;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Creates the Bromium Vite plugin for file-based routing.
|
|
43
|
-
* Automatically scans the pages directory and generates route configuration.
|
|
44
|
-
*
|
|
45
|
-
* @param options - Plugin configuration options
|
|
46
|
-
* @returns A Vite plugin instance
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```ts
|
|
50
|
-
* // vite.config.ts
|
|
51
|
-
* import { defineConfig } from 'vite';
|
|
52
|
-
* import { bromiumPlugin } from '@bromscandium/vite-plugin';
|
|
53
|
-
*
|
|
54
|
-
* export default defineConfig({
|
|
55
|
-
* plugins: [
|
|
56
|
-
* bromiumPlugin({
|
|
57
|
-
* pagesDir: 'src/pages',
|
|
58
|
-
* base: '/app/',
|
|
59
|
-
* generateTypes: true,
|
|
60
|
-
* }),
|
|
61
|
-
* ],
|
|
62
|
-
* });
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
export function bromiumPlugin(options: BromiumPluginOptions = {}): Plugin {
|
|
66
|
-
const {
|
|
67
|
-
pagesDir = 'src/pages',
|
|
68
|
-
routesOutput = 'src/routes.generated.ts',
|
|
69
|
-
base = '',
|
|
70
|
-
generateTypes = true,
|
|
71
|
-
} = options;
|
|
72
|
-
|
|
73
|
-
let root: string;
|
|
74
|
-
let fullPagesDir: string;
|
|
75
|
-
let fullRoutesOutput: string;
|
|
76
|
-
let stopWatcher: (() => void) | null = null;
|
|
77
|
-
|
|
78
|
-
async function generateRoutes(): Promise<void> {
|
|
79
|
-
if (!fs.existsSync(fullPagesDir)) {
|
|
80
|
-
fs.mkdirSync(fullPagesDir, { recursive: true });
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const routes = scanPages(fullPagesDir);
|
|
84
|
-
|
|
85
|
-
const code = generateRouteConfig(routes, {
|
|
86
|
-
pagesDir: fullPagesDir,
|
|
87
|
-
outputPath: fullRoutesOutput,
|
|
88
|
-
base,
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
const outputDir = path.dirname(fullRoutesOutput);
|
|
92
|
-
if (!fs.existsSync(outputDir)) {
|
|
93
|
-
fs.mkdirSync(outputDir, { recursive: true });
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
fs.writeFileSync(fullRoutesOutput, code, 'utf-8');
|
|
97
|
-
|
|
98
|
-
if (generateTypes) {
|
|
99
|
-
const typesPath = fullRoutesOutput.replace(/\.ts$/, '.d.ts');
|
|
100
|
-
const typesCode = generateRouteTypes(routes);
|
|
101
|
-
fs.writeFileSync(typesPath, typesCode, 'utf-8');
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
console.log(`[bromium] Generated routes from ${routes.length} pages`);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return {
|
|
108
|
-
name: 'bromium-plugin',
|
|
109
|
-
|
|
110
|
-
configResolved(config: ResolvedConfig) {
|
|
111
|
-
root = config.root;
|
|
112
|
-
fullPagesDir = path.resolve(root, pagesDir);
|
|
113
|
-
fullRoutesOutput = path.resolve(root, routesOutput);
|
|
114
|
-
},
|
|
115
|
-
|
|
116
|
-
async buildStart() {
|
|
117
|
-
await generateRoutes();
|
|
118
|
-
},
|
|
119
|
-
|
|
120
|
-
configureServer(server: ViteDevServer) {
|
|
121
|
-
stopWatcher = watchPages(fullPagesDir, async () => {
|
|
122
|
-
await generateRoutes();
|
|
123
|
-
const module = server.moduleGraph.getModuleById(fullRoutesOutput);
|
|
124
|
-
if (module) {
|
|
125
|
-
server.moduleGraph.invalidateModule(module);
|
|
126
|
-
server.ws.send({
|
|
127
|
-
type: 'full-reload',
|
|
128
|
-
path: '*',
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
},
|
|
133
|
-
|
|
134
|
-
buildEnd() {
|
|
135
|
-
if (stopWatcher) {
|
|
136
|
-
stopWatcher();
|
|
137
|
-
stopWatcher = null;
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
|
|
141
|
-
transform(_code: string, id: string) {
|
|
142
|
-
if (!id.endsWith('.tsx') && !id.endsWith('.jsx')) {
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return null;
|
|
147
|
-
},
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export default bromiumPlugin;
|
|
1
|
+
/**
|
|
2
|
+
* Main Vite plugin for BromiumJS file-based routing.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Plugin, ResolvedConfig, ViteDevServer } from 'vite';
|
|
7
|
+
import * as path from 'path';
|
|
8
|
+
import * as fs from 'fs';
|
|
9
|
+
import { scanPages, watchPages } from './scanner.js';
|
|
10
|
+
import { generateRouteConfig, generateRouteTypes } from './codegen.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Configuration options for the Bromium Vite plugin.
|
|
14
|
+
*/
|
|
15
|
+
export interface BromiumPluginOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Directory containing page components.
|
|
18
|
+
* @default 'src/pages'
|
|
19
|
+
*/
|
|
20
|
+
pagesDir?: string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Output path for generated routes file.
|
|
24
|
+
* @default 'src/routes.generated.ts'
|
|
25
|
+
*/
|
|
26
|
+
routesOutput?: string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Base path for all routes (e.g., '/app/').
|
|
30
|
+
* @default ''
|
|
31
|
+
*/
|
|
32
|
+
base?: string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Generate TypeScript type definitions for routes.
|
|
36
|
+
* @default true
|
|
37
|
+
*/
|
|
38
|
+
generateTypes?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Creates the Bromium Vite plugin for file-based routing.
|
|
43
|
+
* Automatically scans the pages directory and generates route configuration.
|
|
44
|
+
*
|
|
45
|
+
* @param options - Plugin configuration options
|
|
46
|
+
* @returns A Vite plugin instance
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* // vite.config.ts
|
|
51
|
+
* import { defineConfig } from 'vite';
|
|
52
|
+
* import { bromiumPlugin } from '@bromscandium/vite-plugin';
|
|
53
|
+
*
|
|
54
|
+
* export default defineConfig({
|
|
55
|
+
* plugins: [
|
|
56
|
+
* bromiumPlugin({
|
|
57
|
+
* pagesDir: 'src/pages',
|
|
58
|
+
* base: '/app/',
|
|
59
|
+
* generateTypes: true,
|
|
60
|
+
* }),
|
|
61
|
+
* ],
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export function bromiumPlugin(options: BromiumPluginOptions = {}): Plugin {
|
|
66
|
+
const {
|
|
67
|
+
pagesDir = 'src/pages',
|
|
68
|
+
routesOutput = 'src/routes.generated.ts',
|
|
69
|
+
base = '',
|
|
70
|
+
generateTypes = true,
|
|
71
|
+
} = options;
|
|
72
|
+
|
|
73
|
+
let root: string;
|
|
74
|
+
let fullPagesDir: string;
|
|
75
|
+
let fullRoutesOutput: string;
|
|
76
|
+
let stopWatcher: (() => void) | null = null;
|
|
77
|
+
|
|
78
|
+
async function generateRoutes(): Promise<void> {
|
|
79
|
+
if (!fs.existsSync(fullPagesDir)) {
|
|
80
|
+
fs.mkdirSync(fullPagesDir, { recursive: true });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const routes = scanPages(fullPagesDir);
|
|
84
|
+
|
|
85
|
+
const code = generateRouteConfig(routes, {
|
|
86
|
+
pagesDir: fullPagesDir,
|
|
87
|
+
outputPath: fullRoutesOutput,
|
|
88
|
+
base,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const outputDir = path.dirname(fullRoutesOutput);
|
|
92
|
+
if (!fs.existsSync(outputDir)) {
|
|
93
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fs.writeFileSync(fullRoutesOutput, code, 'utf-8');
|
|
97
|
+
|
|
98
|
+
if (generateTypes) {
|
|
99
|
+
const typesPath = fullRoutesOutput.replace(/\.ts$/, '.d.ts');
|
|
100
|
+
const typesCode = generateRouteTypes(routes);
|
|
101
|
+
fs.writeFileSync(typesPath, typesCode, 'utf-8');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
console.log(`[bromium] Generated routes from ${routes.length} pages`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
name: 'bromium-plugin',
|
|
109
|
+
|
|
110
|
+
configResolved(config: ResolvedConfig) {
|
|
111
|
+
root = config.root;
|
|
112
|
+
fullPagesDir = path.resolve(root, pagesDir);
|
|
113
|
+
fullRoutesOutput = path.resolve(root, routesOutput);
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
async buildStart() {
|
|
117
|
+
await generateRoutes();
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
configureServer(server: ViteDevServer) {
|
|
121
|
+
stopWatcher = watchPages(fullPagesDir, async () => {
|
|
122
|
+
await generateRoutes();
|
|
123
|
+
const module = server.moduleGraph.getModuleById(fullRoutesOutput);
|
|
124
|
+
if (module) {
|
|
125
|
+
server.moduleGraph.invalidateModule(module);
|
|
126
|
+
server.ws.send({
|
|
127
|
+
type: 'full-reload',
|
|
128
|
+
path: '*',
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
buildEnd() {
|
|
135
|
+
if (stopWatcher) {
|
|
136
|
+
stopWatcher();
|
|
137
|
+
stopWatcher = null;
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
transform(_code: string, id: string) {
|
|
142
|
+
if (!id.endsWith('.tsx') && !id.endsWith('.jsx')) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return null;
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export default bromiumPlugin;
|