Problem router vite

This commit is contained in:
2026-03-17 14:44:58 +01:00
parent 2abeaedfb2
commit abf549c05f
2 changed files with 23 additions and 31 deletions

View File

@@ -1 +1 @@
import fs from"fs";import path from"path";export default function sigproRouter(){const e="virtual:sigpro-routes",t="\0"+e;function r(e){let t=[];if(!fs.existsSync(e))return t;return fs.readdirSync(e).forEach((o=>{const s=path.resolve(e,o),n=fs.statSync(s);n&&n.isDirectory()?t=t.concat(r(s)):(o.endsWith(".js")||o.endsWith(".jsx"))&&t.push(s)})),t}return{name:"sigpro-router",resolveId(r){if(r===e)return t},load(e){if(e===t){const e=path.resolve(process.cwd(),"src/pages");let t=r(e);t=t.sort(((t,r)=>{const o=path.relative(e,t),s=path.relative(e,r),n=o.split(path.sep).length,c=s.split(path.sep).length;if(n!==c)return c-n;const a=o.includes("[");return a!==s.includes("[")?a?1:-1:o.localeCompare(s)}));let o="",s="export const routes = [\n";return console.log("\n🚀 [SigPro Router] Routes generated:"),t.forEach(((t,r)=>{const n=path.relative(e,t).replace(/\\/g,"/"),c=`Page_${r}`;let a=function(e){let t=e.replace(/\.jsx?$/,"");return t=t.replace(/\[([^\]]+)\]/g,":$1"),t.endsWith("/index")&&(t=t.slice(0,-6)),"/"+t.toLowerCase()}(n);const i=a.includes(":");o+=`import ${c} from '${t}';\n`,console.log(` ${i?"🔗":"📄"} ${a.padEnd(30)} -> ${n}`),s+=` { path: '${a}', component: ${c} },\n`})),s+="];",`${o}\n${s}`}}}}
import fs from"fs";import path from"path";export default function sigproRouter(){const e="virtual:sigpro-routes",t="\0"+e;function r(e){let t=[];if(!fs.existsSync(e))return t;return fs.readdirSync(e).forEach((n=>{const o=path.resolve(e,n),s=fs.statSync(o);s&&s.isDirectory()?t=t.concat(r(o)):(n.endsWith(".js")||n.endsWith(".jsx"))&&t.push(o)})),t}return{name:"sigpro-router",resolveId(r){if(r===e)return t},load(e){if(e===t){const e=path.resolve(process.cwd(),"src/pages");let t=r(e);t=t.sort(((t,r)=>{const n=path.relative(e,t).replace(/\\/g,"/"),o=path.relative(e,r).replace(/\\/g,"/"),s=n.includes("[")||n.includes(":");return s!==(o.includes("[")||o.includes(":"))?s?1:-1:o.length-n.length}));let n="",o="export const routes = [\n";return console.log("\n🚀 [SigPro Router] Routes generated:"),t.forEach(((t,r)=>{const s=t.replace(/\\/g,"/"),c=path.relative(e,t).replace(/\\/g,"/"),l=`Page_${r}`;let a=function(e){let t=e.replace(/\\/g,"/").replace(/\.jsx?$/,"");return"index"===t?"/":(t.endsWith("/index")&&(t=t.slice(0,-6)),t=t.replace(/\[([^\]]+)\]/g,":$1"),("/"+t.toLowerCase()).replace(/\/+/g,"/").replace(/\/$/,"")||"/")}(c);const i=a.includes(":");n+=`import ${l} from '${s}';\n`,console.log(` ${i?"🔗":"📄"} ${a.padEnd(30)} -> ${c}`),o+=` { path: '${a}', component: ${l} },\n`})),o+="];",`${n}\n${o}`}}}}

View File

@@ -22,21 +22,22 @@ export default function sigproRouter() {
return results;
}
// Convierte path de archivo a URL con :params
function filePathToUrl(relativePath) {
// Remover extensión .js
let url = relativePath.replace(/\.jsx?$/, '');
let url = relativePath.replace(/\\/g, '/').replace(/\.jsx?$/, '');
// Convertir [param] a :param
url = url.replace(/\[([^\]]+)\]/g, ':$1');
// Index files become parent path
if (url.endsWith('/index')) {
url = url.slice(0, -6); // Remove '/index'
if (url === 'index') {
return '/';
}
// Ensure leading slash
return '/' + url.toLowerCase();
if (url.endsWith('/index')) {
url = url.slice(0, -6);
}
url = url.replace(/\[([^\]]+)\]/g, ':$1');
let finalPath = '/' + url.toLowerCase();
return finalPath.replace(/\/+/g, '/').replace(/\/$/, '') || '/';
}
return {
@@ -49,23 +50,16 @@ export default function sigproRouter() {
const pagesDir = path.resolve(process.cwd(), 'src/pages');
let files = getFiles(pagesDir);
// Sort: static routes first, then dynamic (more specific first)
files = files.sort((a, b) => {
const aPath = path.relative(pagesDir, a);
const bPath = path.relative(pagesDir, b);
const aDepth = aPath.split(path.sep).length;
const bDepth = bPath.split(path.sep).length;
const aRel = path.relative(pagesDir, a).replace(/\\/g, '/');
const bRel = path.relative(pagesDir, b).replace(/\\/g, '/');
// Deeper paths first (more specific)
if (aDepth !== bDepth) return bDepth - aDepth;
const aDynamic = aRel.includes('[') || aRel.includes(':');
const bDynamic = bRel.includes('[') || bRel.includes(':');
// Static before dynamic
const aDynamic = aPath.includes('[');
const bDynamic = bPath.includes('[');
if (aDynamic !== bDynamic) return aDynamic ? 1 : -1;
// Alphabetical
return aPath.localeCompare(bPath);
return bRel.length - aRel.length;
});
let imports = '';
@@ -74,16 +68,14 @@ export default function sigproRouter() {
console.log('\n🚀 [SigPro Router] Routes generated:');
files.forEach((fullPath, i) => {
const importPath = fullPath.replace(/\\/g, '/');
const relativePath = path.relative(pagesDir, fullPath).replace(/\\/g, '/');
const varName = `Page_${i}`;
// Generate URL path from file structure
let urlPath = filePathToUrl(relativePath);
// Check if it's dynamic (contains ':')
const isDynamic = urlPath.includes(':');
imports += `import ${varName} from '${fullPath}';\n`;
imports += `import ${varName} from '${importPath}';\n`;
console.log(` ${isDynamic ? '🔗' : '📄'} ${urlPath.padEnd(30)} -> ${relativePath}`);