vite
This commit is contained in:
4
vite/index.js
Normal file
4
vite/index.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
// src/vite/index.js
|
||||||
|
import sigproRouterPlugin from './sigproRouter.js';
|
||||||
|
export const sigproRouter = sigproRouterPlugin;
|
||||||
|
export default sigproRouterPlugin;
|
||||||
@@ -5,7 +5,28 @@ export default function sigproRouter() {
|
|||||||
const virtualModuleId = 'virtual:sigpro-routes';
|
const virtualModuleId = 'virtual:sigpro-routes';
|
||||||
const resolvedVirtualModuleId = '\0' + virtualModuleId;
|
const resolvedVirtualModuleId = '\0' + virtualModuleId;
|
||||||
|
|
||||||
// ... (tus funciones getFiles y pathToUrl se quedan igual)
|
const getFiles = (dir) => {
|
||||||
|
if (!fs.existsSync(dir)) return [];
|
||||||
|
return fs.readdirSync(dir, { recursive: true })
|
||||||
|
.filter(file => /\.(js|jsx)$/.test(file) && !path.basename(file).startsWith('_'))
|
||||||
|
.map(file => path.resolve(dir, file));
|
||||||
|
};
|
||||||
|
|
||||||
|
const pathToUrl = (pagesDir, filePath) => {
|
||||||
|
let relative = path.relative(pagesDir, filePath)
|
||||||
|
.replace(/\\/g, '/')
|
||||||
|
.replace(/\.(js|jsx)$/, '')
|
||||||
|
.replace(/\/index$/, '')
|
||||||
|
.replace(/^index$/, '');
|
||||||
|
|
||||||
|
let url = '/' + relative;
|
||||||
|
|
||||||
|
return url
|
||||||
|
.replace(/\/+/g, '/')
|
||||||
|
.replace(/\[\.\.\.([^\]]+)\]/g, '*')
|
||||||
|
.replace(/\[([^\]]+)\]/g, ':$1')
|
||||||
|
.replace(/\/$/, '') || '/';
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: 'sigpro-router',
|
name: 'sigpro-router',
|
||||||
@@ -16,19 +37,24 @@ export default function sigproRouter() {
|
|||||||
if (id !== resolvedVirtualModuleId) return;
|
if (id !== resolvedVirtualModuleId) return;
|
||||||
|
|
||||||
const pagesDir = path.resolve(process.cwd(), 'src/pages');
|
const pagesDir = path.resolve(process.cwd(), 'src/pages');
|
||||||
const files = getFiles(pagesDir).sort((a, b) => b.length - a.length);
|
const files = getFiles(pagesDir).sort((a, b) => {
|
||||||
|
const urlA = pathToUrl(pagesDir, a);
|
||||||
|
const urlB = pathToUrl(pagesDir, b);
|
||||||
|
if (urlA.includes(':') && !urlB.includes(':')) return 1;
|
||||||
|
if (!urlA.includes(':') && urlB.includes(':')) return -1;
|
||||||
|
return urlB.length - urlA.length;
|
||||||
|
});
|
||||||
|
|
||||||
let routeEntries = '';
|
let routeEntries = '';
|
||||||
|
|
||||||
files.forEach((fullPath, i) => {
|
files.forEach((fullPath) => {
|
||||||
const urlPath = pathToUrl(pagesDir, fullPath);
|
const urlPath = pathToUrl(pagesDir, fullPath);
|
||||||
const importPath = fullPath.replace(/\\/g, '/');
|
const importPath = fullPath.replace(/\\/g, '/');
|
||||||
|
|
||||||
routeEntries += ` { path: '${urlPath}', component: () => import('${importPath}') },\n`;
|
routeEntries += ` { path: '${urlPath}', component: () => import('${importPath}') },\n`;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!routeEntries.includes("path: '*'")) {
|
if (!routeEntries.includes("path: '*'")) {
|
||||||
routeEntries += ` { path: '*', component: () => h1('404 - Not Found') },\n`;
|
routeEntries += ` { path: '*', component: () => span('404 - Not Found') },\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `export const routes = [\n${routeEntries}];`;
|
return `export const routes = [\n${routeEntries}];`;
|
||||||
|
|||||||
Reference in New Issue
Block a user