UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

76 lines (75 loc) 2.97 kB
import MicroMatch from "micromatch"; function _instanceof(left, right) { return right != null && typeof Symbol < "u" && right[Symbol.hasInstance] ? !!right[Symbol.hasInstance](left) : left instanceof right; } function generateSitemap(routes, options) { var _options_baseUrl, _options_priority, envUrl = process.env.ONE_SERVER_URL, baseUrl = (_options_baseUrl = options.baseUrl) !== null && _options_baseUrl !== void 0 ? _options_baseUrl : envUrl && envUrl !== "undefined" ? envUrl : "", defaultPriority = (_options_priority = options.priority) !== null && _options_priority !== void 0 ? _options_priority : 0.5, defaultChangefreq = options.changefreq, excludePatterns = options.exclude || [], entries = [], _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0; try { for (var _iterator = routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) { var route = _step.value, _ref, _ref1, { path, routeExport } = route; if (!routeExport?.exclude && !(excludePatterns.length > 0 && MicroMatch.isMatch(path, excludePatterns))) { var priority = (_ref = routeExport?.priority) !== null && _ref !== void 0 ? _ref : defaultPriority, changefreq = (_ref1 = routeExport?.changefreq) !== null && _ref1 !== void 0 ? _ref1 : defaultChangefreq, lastmod = routeExport?.lastmod; entries.push({ path, priority, changefreq, lastmod }); } } } catch (err) { _didIteratorError = !0, _iteratorError = err; } finally { try { !_iteratorNormalCompletion && _iterator.return != null && _iterator.return(); } finally { if (_didIteratorError) throw _iteratorError; } } return buildSitemapXml(entries, baseUrl); } function buildSitemapXml(entries, baseUrl) { var urlEntries = entries.map(function (entry) { var loc = baseUrl ? `${baseUrl.replace(/\/$/, "")}${entry.path}` : entry.path, xml = ` <url> <loc>${escapeXml(loc)}</loc>`; if (entry.lastmod) { var date = _instanceof(entry.lastmod, Date) ? entry.lastmod.toISOString().split("T")[0] : entry.lastmod; xml += ` <lastmod>${escapeXml(date)}</lastmod>`; } return entry.changefreq && (xml += ` <changefreq>${entry.changefreq}</changefreq>`), entry.priority !== void 0 && (xml += ` <priority>${entry.priority.toFixed(1)}</priority>`), xml += ` </url>`, xml; }).join(` `); return `<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> ${urlEntries} </urlset> `; } function escapeXml(str) { return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;"); } export { generateSitemap }; //# sourceMappingURL=generateSitemap.native.js.map