Replies: 1 comment
-
|
I faced the same problem. I created a custom resolver to allow the duplicate routes. Unique path values are then assigned in postprocessing in /**
* Custom React Resolver for vite-plugin-pages - No Route Deduplication
*
* WHAT IT DOES:
* - Removes the duplicate route filtering from the standard React resolver
* - Allows multiple files with the same route path (e.g., multiple test.jsx files)
* - Generates routes for ALL files found in pageRouteMap without skipping any
* - Preserves all original vite-plugin-pages functionality (extendRoute, onRoutesGenerated, etc.)
*
* HOW TO USE:
* 1. Save this file as: vite.pages.resolver.fixed.js
* 2. In your vite.config.ts, import and use:
* import { fixedReactResolver } from './vite.pages.resolver.fixed.js'
*
* Pages({
* dirs: ['src/widgets'],
* resolver: fixedReactResolver(),
* onRoutesGenerated(routes) {
* // You'll now get ALL routes, including duplicates
* // Handle them as needed in your logic
* return routes;
* }
* })
*
* EXAMPLE:
* Before: src/widgets/folder1/test.jsx and src/widgets/folder2/test.jsx
* → Only one route for /test
* After: Both files generate separate routes for /test with different elements
* → Two routes both with path="/test" but different element paths
*/
import { reactResolver } from 'vite-plugin-pages';
export function fixedReactResolver() {
const baseResolver = reactResolver();
return {
...baseResolver,
async resolveRoutes(ctx) {
// Build our own routes from all files in pageRouteMap - NO DEDUPLICATION
const allFiles = [...ctx.pageRouteMap.values()];
//console.log(`Found ${allFiles.length} total files:`, allFiles.map(f => f.path));
// Generate simple routes for all files
const allRoutes = allFiles.map((pageData, index) => {
const fileName = pageData.path.split('/').pop().replace(/\.(jsx?|tsx?)$/, '');
const element = ctx.options.importPath === 'relative'
? pageData.path.replace(ctx.root, '')
: pageData.path;
return {
path: fileName === 'index' ? '/' : `/${fileName}`,
element: element,
caseSensitive: ctx.options.caseSensitive || false,
// Add debug info
_debug: {
originalRoute: pageData.route,
filePath: pageData.path,
index: index
}
};
});
// Apply user customizations
let finalRoutes = allRoutes;
if (ctx.options.extendRoute) {
finalRoutes = finalRoutes.map(route => {
const extended = ctx.options.extendRoute(route) || {};
return { ...route, ...extended };
});
}
// Call onRoutesGenerated safely
if (ctx.options.onRoutesGenerated) {
try {
finalRoutes = await ctx.options.onRoutesGenerated(finalRoutes) || finalRoutes;
} catch (error) {
console.error('Error in onRoutesGenerated:', error);
// Continue with original routes if error
}
}
// Build the client code manually (simple version - no internal imports needed)
const imports = finalRoutes.map((route, index) =>
`const Component${index} = React.lazy(() => import("${route.element}"));`
).join('\n');
const routesArray = finalRoutes.map((route, index) => {
// Clean up the route object for export
const { _debug, ...cleanRoute } = route;
return {
...cleanRoute,
element: `React.createElement(Component${index})`
};
});
const routesString = JSON.stringify(routesArray, null, 2)
.replace(/"React\.createElement\(Component(\d+)\)"/g, 'React.createElement(Component$1)');
const clientCode = `import React from "react";
${imports}
const routes = ${routesString};
export default routes;`;
//console.log('Generated routes:', finalRoutes.map(r => ({ path: r.path, element: r.element })));
if (ctx.options.onClientGenerated) {
return await ctx.options.onClientGenerated(clientCode) || clientCode;
}
return clientCode;
}
};
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
A route cannot be generated for files with the same name in different directories
The/workbench route and/workbench/list route are missing here
Reproduction
System Info
Used Package Manager
pnpm
Validations
Beta Was this translation helpful? Give feedback.
All reactions