1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import fs from "node:fs"; import path from "node:path";
const routes = [ { path: "/", priority: "1.0", changefreq: "daily" }, { path: "/login", priority: "0.8", changefreq: "monthly" }, ];
function generateSitemap(domain = "https://xxx.xxx.com") { const today = new Date().toISOString().split("T")[0];
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n'; xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n';
routes.forEach((route) => { xml += " <url>\n"; xml += ` <loc>${domain}${route.path}</loc>\n`; xml += ` <lastmod>${today}</lastmod>\n`; xml += ` <changefreq>${route.changefreq}</changefreq>\n`; xml += ` <priority>${route.priority}</priority>\n`; xml += " </url>\n"; });
xml += "</urlset>"; return xml; }
function main() { const domain = process.env.VITE_SITE_DOMAIN || "https://xxx.xxx.com"; const outputPath = path.resolve(process.cwd(), "dist/sitemap.xml");
const sitemapContent = generateSitemap(domain);
const distDir = path.dirname(outputPath); if (!fs.existsSync(distDir)) { fs.mkdirSync(distDir, { recursive: true }); }
fs.writeFileSync(outputPath, sitemapContent, "utf8"); console.log(`✅ Sitemap generated: ${outputPath}`); console.log(`🌐 Domain: ${domain}`); console.log(`📄 Total URLs: ${routes.length}`); }
if (import.meta.url === `file://${process.argv[1]}`) { main(); }
export { generateSitemap, main };
|