@wroud/vite-plugin-ssg
Version:
A Vite plugin for static site generation (SSG) with React. Renders React applications to static HTML for faster load times and improved SEO.
47 lines • 2.07 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { pathUrlWithBase } from "./pathUrlWithBase.js";
export function renderViteTags(tags, context, injectTo) {
return _jsx(RenderViteTags, { tags: tags, injectTo: injectTo, context: context });
}
export function RenderViteTags({ tags, injectTo, context, }) {
return (_jsx(_Fragment, { children: tags
.filter((tag) => tag.injectTo === injectTo)
.map((tag, index) => (_jsx(RenderViteTag, { tag: tag, context: context }, `${tag.tag}-${index}`))) }));
}
export function RenderViteTag({ tag, context }) {
const Tag = tag.tag;
const attrs = { ...tag.attrs };
if (context.base) {
if (typeof attrs["href"] === "string") {
attrs["href"] = pathUrlWithBase(context.base, attrs["href"]);
}
if (typeof attrs["src"] === "string") {
attrs["src"] = pathUrlWithBase(context.base, attrs["src"]);
}
if (typeof tag.children === "string") {
tag.children = tag.children.replace(/from\s+(['"])([^'"]+)\1/gi, (_, quote, url) => `from ${quote}${pathUrlWithBase(context.base, url)}${quote}`);
}
if (tag.tag === "meta" && attrs["property"] === "base") {
attrs["content"] = context.base;
}
}
if (context.cspNonce) {
if (attrs["nonce"] === "{{nonce}}") {
attrs["nonce"] = context.cspNonce;
}
if (typeof tag.children === "string") {
tag.children = tag.children.replaceAll("{{nonce}}", context.cspNonce);
}
if (tag.tag === "script" && !attrs["nonce"]) {
attrs["nonce"] = context.cspNonce;
}
}
if (Array.isArray(tag.children)) {
return (_jsx(Tag, { ...attrs, children: _jsx(RenderViteTags, { tags: tag.children, injectTo: tag.injectTo, context: context }) }));
}
if (!tag.children) {
return _jsx(Tag, { ...attrs });
}
return _jsx(Tag, { ...attrs, dangerouslySetInnerHTML: { __html: tag.children } });
}
//# sourceMappingURL=ssg-common.js.map