-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.ts
More file actions
147 lines (131 loc) · 4.55 KB
/
Copy pathvite.config.ts
File metadata and controls
147 lines (131 loc) · 4.55 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { defineConfig, loadEnv, type Plugin } from "vite";
import preact from "@preact/preset-vite";
import { ASN_GUIDE_PATH, createAsnGuidePage } from "./src/seo/asnGuidePage";
const GOOGLE_SITE_VERIFICATION_FILE = "googleb55a2c9f70d84e9f.html";
function escapeHtmlAttribute(value: string): string {
return value
.replaceAll("&", "&")
.replaceAll('"', """)
.replaceAll("<", "<")
.replaceAll(">", ">");
}
function normalizeSiteUrl(value: string | undefined): string | null {
const trimmed = value?.trim();
if (!trimmed) {
return null;
}
return trimmed.replace(/\/+$/, "");
}
function createSeoSchemaJson(siteUrl: string | null): string {
const schema: Record<string, unknown> = {
"@context": "https://schema.org",
"@type": "WebApplication",
name: "Plasn",
description:
"Paperless-ngx ASN label generator for printable ASN QR label sheets, barcode separator pages, and calibration PDFs.",
applicationCategory: "UtilitiesApplication",
operatingSystem: "Any",
inLanguage: ["en", "de"],
offers: {
"@type": "Offer",
price: "0",
priceCurrency: "USD",
},
featureList: [
"ASN QR label sheet generator for Paperless-ngx",
"Barcode separator page generator",
"Print calibration and overlay test sheets",
"Browser-based workflow with local settings",
],
};
if (siteUrl) {
schema.url = `${siteUrl}/`;
}
return JSON.stringify(schema, null, 8);
}
function createSiteAssetsPlugin(env: Record<string, string>): Plugin {
const siteUrl = normalizeSiteUrl(env.PLASN_SITE_URL);
const siteRootUrl = siteUrl ? `${siteUrl}/` : null;
const ogImageUrl = siteRootUrl ? `${siteRootUrl}plasn-mark-256.png` : null;
const schemaJson = createSeoSchemaJson(siteUrl);
return {
name: "plasn-site-assets",
transformIndexHtml(html) {
const canonicalTag = siteRootUrl
? ` <link rel="canonical" href="${escapeHtmlAttribute(siteRootUrl)}" />`
: "";
const socialUrlTags = siteRootUrl
? [
` <meta property="og:url" content="${escapeHtmlAttribute(siteRootUrl)}" />`,
` <meta property="og:image" content="${escapeHtmlAttribute(ogImageUrl!)}" />`,
].join("\n")
: "";
const twitterImageTag = ogImageUrl
? ` <meta name="twitter:image" content="${escapeHtmlAttribute(ogImageUrl)}" />`
: "";
return html
.replace(" <!-- PLASN_CANONICAL -->", canonicalTag)
.replace(" <!-- PLASN_SOCIAL_URLS -->", socialUrlTags)
.replace(" <!-- PLASN_TWITTER_IMAGE -->", twitterImageTag)
.replace("__PLASN_SCHEMA_JSON__", schemaJson);
},
generateBundle() {
const robotsTxt = siteRootUrl
? `User-agent: *\nAllow: /\n\nSitemap: ${siteRootUrl}sitemap.xml\n`
: "User-agent: *\nAllow: /\n";
this.emitFile({
type: "asset",
fileName: "robots.txt",
source: robotsTxt,
});
if (siteRootUrl) {
this.emitFile({
type: "asset",
fileName: "sitemap.xml",
source:
`<?xml version="1.0" encoding="UTF-8"?>\n` +
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n` +
` <url>\n` +
` <loc>${siteRootUrl}</loc>\n` +
` <changefreq>weekly</changefreq>\n` +
` <priority>1.0</priority>\n` +
` </url>\n` +
` <url>\n` +
` <loc>${siteRootUrl}${ASN_GUIDE_PATH.slice(1)}</loc>\n` +
` <changefreq>monthly</changefreq>\n` +
` <priority>0.9</priority>\n` +
` </url>\n` +
`</urlset>\n`,
});
this.emitFile({
type: "asset",
fileName: `${ASN_GUIDE_PATH.slice(1)}index.html`,
source: createAsnGuidePage(siteRootUrl),
});
}
if (siteRootUrl) {
this.emitFile({
type: "asset",
fileName: GOOGLE_SITE_VERIFICATION_FILE,
source: `google-site-verification: ${GOOGLE_SITE_VERIFICATION_FILE}\n`,
});
}
},
};
}
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), "");
const siteUrl = normalizeSiteUrl(env.PLASN_SITE_URL);
const siteRootUrl = siteUrl ? `${siteUrl}/` : null;
return {
plugins: [preact(), createSiteAssetsPlugin(env)],
define: {
__PLASN_SEO_GUIDE_PATH__: JSON.stringify(siteRootUrl ? ASN_GUIDE_PATH : null),
},
test: {
environment: "jsdom",
globals: true,
setupFiles: ["./src/test/setup.ts"],
},
};
});