-
Notifications
You must be signed in to change notification settings - Fork 18
/
vite.config.js
153 lines (146 loc) · 4.36 KB
/
vite.config.js
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
148
149
150
151
152
153
import * as fs from "node:fs";
import path, { resolve } from "node:path";
import { sentryVitePlugin } from "@sentry/vite-plugin";
import react from "@vitejs/plugin-react";
import { defineConfig, loadEnv } from "vite";
import Sitemap from "vite-plugin-sitemap";
import languages from "./src/languages";
const getTranslatedStrings = async (languageCode) => {
const translations = await import(
`./public/locales/${languageCode}/translation.json`
);
return translations.meta;
};
const htmlPlugin = async (env) => {
const translationsEn = await getTranslatedStrings("en");
return {
name: "html-transform",
async transformIndexHtml(html) {
const currentLang = html.match(/<html lang="([a-zA-Z-]+)">/)[1];
const translationsTarget = await getTranslatedStrings(currentLang).catch(
(e) => console.error(e),
);
const { meta_title, meta_description, website_title } = {
...translationsEn,
...translationsTarget,
};
const baseUrl = currentLang === "en" ? "" : `/${currentLang}/`;
const canonicalLinks = `<link href="${env.VITE_BACKEND_API_URL}${baseUrl}" rel="canonical" />`;
const alternateLinks = Object.keys(languages)
.filter((lang) => lang !== currentLang)
.map(
(lang) =>
`\t<link href="${env.VITE_BACKEND_API_URL}/${lang}/" rel="alternate" hreflang="${lang}" />`,
)
.join("\n");
const defaultAlternateLink = `\t<link href="${env.VITE_BACKEND_API_URL}/" rel="alternate" hreflang="x-default" />`;
const seoLinks = `${canonicalLinks}\n${alternateLinks}\n${defaultAlternateLink}`;
return html
.replace(/<title>(.*?)<\/title>/, `<title>${website_title}</title>`)
.replace(
/<meta content="(.*?)" name="title">/,
`<meta content="${meta_title}" name="title">`,
)
.replace(
/<meta content="(.*?)" name="description">/,
`<meta content="${meta_description}" name="description">`,
)
.replace(
/<link href="https:\/\/openaedmap.org" rel="canonical" \/>/,
seoLinks,
)
.replace(
/<meta name="twitter:title" content="(.*)">/,
`<meta name="twitter:title" content="${meta_title}">`,
)
.replace(
/<meta name="og:title" content="(.*)">/,
`<meta name="og:title" content="${meta_title}">`,
)
.replace(
/<meta name="twitter:description" content="(.*)">/,
`<meta name="twitter:description" content="${meta_description}">`,
)
.replace(
/<meta name="og:description" content="(.*)">/,
`<meta name="og:description" content="${meta_description}">`,
);
},
buildStart() {
const content = fs.readFileSync("index.html", "utf-8");
for (const lang of Object.keys(languages)) {
fs.mkdirSync(`langs/${lang}`, { recursive: true });
const contentLang = content.replace(
/<html lang="en">/,
`<html lang="${lang}">`,
);
fs.writeFileSync(`langs/${lang}/index.html`, contentLang);
}
},
writeBundle() {
for (const lang of Object.keys(languages)) {
fs.mkdirSync(`build/${lang}`, { recursive: true });
fs.renameSync(`build/langs/${lang}`, `build/${lang}`);
}
},
};
};
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd());
const plugins = [react(), htmlPlugin(env)];
if (env.VITE_SENTRY_AUTH_TOKEN) {
plugins.push(
sentryVitePlugin({
org: "sentry",
project: "openaedmap-frontend",
authToken: env.VITE_SENTRY_AUTH_TOKEN,
url: "https://sentry.monicz.dev",
telemetry: false,
release: {
dist: env.VITE_ENV,
},
}),
);
}
const isProduction =
env.VITE_BACKEND_API_URL !== undefined &&
!env.VITE_BACKEND_API_URL.includes("dev");
if (isProduction) {
plugins.push(
Sitemap({
outDir: "build",
hostname: env.VITE_BACKEND_API_URL,
exclude: ["/land"],
dynamicRoutes: Object.keys(languages).map((lang) => `/${lang}`),
}),
);
}
const rollupInputs = { main: resolve(__dirname, "index.html") };
for (const lang of Object.keys(languages)) {
rollupInputs[lang] = resolve(__dirname, `langs/${lang}/index.html`);
}
return {
// https://github.com/vitejs/vite/issues/1973#issuecomment-787571499
define: {
"process.env": {},
},
resolve: {
alias: {
"~": path.resolve(__dirname, "./src"),
},
},
build: {
target: "es2015",
outDir: "build",
chunkSizeWarningLimit: 1900,
sourcemap: true,
rollupOptions: {
input: rollupInputs,
},
},
plugins: plugins,
server: {
host: "127.0.0.1",
},
};
});