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