Skip to content

Commit 21333c4

Browse files
committed
Removed __dirname and use path.resolve.
Made PluginProps optional
1 parent 92a7208 commit 21333c4

File tree

5 files changed

+30
-36
lines changed

5 files changed

+30
-36
lines changed

.changeset/violet-paws-study.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'solid-start-typesafe-routes-plugin': patch
3+
---
4+
5+
Removed \_\_dirname and use path.resolve. Made PluginProps Optional

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"name": "solid-start-typesafe-routes-plugin",
3-
"version": "0.1.1",
4-
"description": "Solid-start Sitemap generator",
3+
"version": "0.1.2",
4+
"description": "Type-safe routes for solid-start file-routes",
55
"license": "MIT",
66
"author": "madaxen86",
77
"contributors": [],
88
"repository": {
99
"type": "git",
10-
"url": "git+https://github.com/madaxen86/solid-start-sitemap.git"
10+
"url": "git+https://github.com/madaxen86/solid-start-typesafe-routes-plugin.git"
1111
},
12-
"homepage": "https://github.com/madaxen86/solid-start-sitemap#readme",
12+
"homepage": "https://github.com/madaxen86/solid-start-typesafe-routes-plugin#readme",
1313
"bugs": {
14-
"url": "https://github.com/madaxen86/solid-start-sitemap/issues"
14+
"url": "https://github.com/madaxen86/solid-start-typesafe-routes-plugin/issues"
1515
},
1616
"files": [
1717
"dist"

src/plugin.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@ import { isValidFile } from './utils';
66

77
interface PluginProps {
88
routeDir: string;
9+
outDir: string;
910
}
1011

11-
export default function RouteManifestPlugin({ routeDir = 'src/routes' }: PluginProps): Plugin {
12+
export default function RouteManifestPlugin(
13+
{ routeDir, outDir }: PluginProps = { routeDir: 'src/routes', outDir: './src/RoutesManifest' },
14+
): Plugin {
15+
const targetDir = path.resolve(outDir);
1216
return {
1317
name: 'vite-plugin-route-manifest',
1418
enforce: 'post',
1519
async configResolved(config) {
1620
if (config.router.name === 'client') {
1721
// TODO: get routeDir from config and set routeRootPath
18-
await generateRouteManifest();
22+
await generateRouteManifest(targetDir);
1923
}
2024
},
2125
async handleHotUpdate({ file }) {
2226
if (!isValidFile(file, routeDir)) return;
2327

24-
await generateRouteManifest();
28+
await generateRouteManifest(targetDir);
2529
},
2630
async configureServer(server) {
2731
// Define the path to watch
@@ -37,15 +41,13 @@ export default function RouteManifestPlugin({ routeDir = 'src/routes' }: PluginP
3741
// Handle file creation
3842
watcher.on('add', async filePath => {
3943
if (!isValidFile(filePath, routeDir)) return;
40-
console.log(`File created: ${filePath}`);
41-
await generateRouteManifest();
44+
await generateRouteManifest(targetDir);
4245
});
4346

4447
// Handle file deletion
4548
watcher.on('unlink', async filePath => {
4649
if (!isValidFile(filePath, routeDir)) return;
47-
console.log(`File deleted: ${filePath}`);
48-
await generateRouteManifest();
50+
await generateRouteManifest(targetDir);
4951
});
5052

5153
server.httpServer?.on('close', () => {

src/routeManifest.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs from 'fs';
2-
import path from 'path';
3-
import { fileURLToPath } from 'url';
2+
import path, { resolve } from 'path';
43
import { getRoutes } from './utils';
54
type TreeNode = {
65
type: 'static' | 'param';
@@ -10,16 +9,10 @@ type TreeNode = {
109

1110
type Tree = Record<string, TreeNode>;
1211

13-
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
14-
const __dirname = path.dirname(__filename); // get the name of the directory
15-
16-
const targertDir = 'src/RouteManifest';
17-
1812
// Ensure the .route directory exists
19-
function ensureRouteDirectory() {
20-
const dir = path.join(__dirname, targertDir);
21-
if (!fs.existsSync(dir)) {
22-
fs.mkdirSync(dir);
13+
function ensureRouteDirectory(targertDir: string) {
14+
if (!fs.existsSync(targertDir)) {
15+
fs.mkdirSync(targertDir);
2316
}
2417
}
2518

@@ -123,7 +116,6 @@ function buildRoutesFromTree(tree: Tree, parentPath = '', depth = 2) {
123116
async function generateRoutesFunction(customRoutes: string[]) {
124117
const fileRoutes = await getRoutes();
125118
const routes = [...fileRoutes, ...customRoutes];
126-
console.log('routes', routes);
127119
const tree = buildRouteTree(routes); // Build the tree from routes
128120

129121
const outputJS = [];
@@ -162,9 +154,9 @@ async function generateRoutesFunction(customRoutes: string[]) {
162154
}
163155

164156
// Function to write the JS file
165-
async function generateJSFile(routes: string[]) {
166-
const jsFilePath = path.join(__dirname, targertDir, 'index.js');
167-
const dtsFilePath = path.join(__dirname, targertDir, 'index.d.ts');
157+
async function generateJSFile(outDir: string, routes: string[]) {
158+
const jsFilePath = path.join(outDir, 'index.js');
159+
const dtsFilePath = path.join(outDir, 'index.d.ts');
168160

169161
const [routesFunctionString, typeDeclarationString] = await generateRoutesFunction(routes);
170162

@@ -174,14 +166,12 @@ async function generateJSFile(routes: string[]) {
174166
if (!typeDeclarationString)
175167
throw new Error('Could not create type declaration for Routes function');
176168
fs.writeFileSync(dtsFilePath, typeDeclarationString, 'utf-8');
177-
178-
console.log(`Generated ${jsFilePath}`);
179169
}
180170

181171
// Main function to generate the route manifest
182-
async function generateRouteManifest(routes: string[] = []) {
183-
ensureRouteDirectory();
184-
await generateJSFile(routes);
172+
async function generateRouteManifest(outDir: string, routes: string[] = []) {
173+
ensureRouteDirectory(outDir);
174+
await generateJSFile(outDir, routes);
185175
}
186176

187177
export { generateRouteManifest, generateRoutesFunction };

src/utils.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export async function getRoutes(config?: ResolvedConfig): Promise<string[]> {
2121
const fileroutes = (await router.getRoutes()) as VinxiFileRoute[];
2222

2323
if (!fileroutes) throw new Error('Could not get router from vinxi app');
24-
console.log('fileroutes', fileroutes);
2524

2625
const filteredRoutes = fileroutes
2726
.filter(
@@ -50,8 +49,6 @@ export const isValidFile = (path: string, routeRootPath: string) =>
5049
path.match(/\.[tj]sx?$/gi);
5150

5251
export function isLayout(route: string, filePath: string, allRoutes: VinxiFileRoute[]): boolean {
53-
const check = allRoutes.some(r => r.path.startsWith(route + '/') && r.filePath !== filePath);
54-
console.log('check', check, route, filePath);
5552
// Check if any route in allRoutes starts with route + "/"
56-
return check;
53+
return allRoutes.some(r => r.path.startsWith(route + '/') && r.filePath !== filePath);
5754
}

0 commit comments

Comments
 (0)