one
Version:
One is a new React Framework that makes Vite serve both native and web.
99 lines • 3.53 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var generateSitemap_exports = {};
__export(generateSitemap_exports, {
generateSitemap: () => generateSitemap
});
module.exports = __toCommonJS(generateSitemap_exports);
var import_micromatch = __toESM(require("micromatch"), 1);
function generateSitemap(routes, options) {
const envUrl = process.env.ONE_SERVER_URL;
const baseUrl = options.baseUrl ?? (envUrl && envUrl !== "undefined" ? envUrl : "");
const defaultPriority = options.priority ?? 0.5;
const defaultChangefreq = options.changefreq;
const excludePatterns = options.exclude || [];
const entries = [];
for (const route of routes) {
const {
path,
routeExport
} = route;
if (routeExport?.exclude) {
continue;
}
if (excludePatterns.length > 0 && import_micromatch.default.isMatch(path, excludePatterns)) {
continue;
}
const priority = routeExport?.priority ?? defaultPriority;
const changefreq = routeExport?.changefreq ?? defaultChangefreq;
const lastmod = routeExport?.lastmod;
entries.push({
path,
priority,
changefreq,
lastmod
});
}
return buildSitemapXml(entries, baseUrl);
}
function buildSitemapXml(entries, baseUrl) {
const urlEntries = entries.map(entry => {
const loc = baseUrl ? `${baseUrl.replace(/\/$/, "")}${entry.path}` : entry.path;
let xml = ` <url>
<loc>${escapeXml(loc)}</loc>`;
if (entry.lastmod) {
const date = entry.lastmod instanceof Date ? entry.lastmod.toISOString().split("T")[0] : entry.lastmod;
xml += `
<lastmod>${escapeXml(date)}</lastmod>`;
}
if (entry.changefreq) {
xml += `
<changefreq>${entry.changefreq}</changefreq>`;
}
if (entry.priority !== void 0) {
xml += `
<priority>${entry.priority.toFixed(1)}</priority>`;
}
xml += "\n </url>";
return xml;
}).join("\n");
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, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
}