-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
137 lines (112 loc) · 4.09 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
import { defineConfig } from "vite";
import fs from "fs/promises";
import path from "path";
import pkg from "./package.json";
const APP_TMPDIR = ".app";
const srcdir = path.resolve(__dirname, "src");
async function getRoutes() {
const routes = (await fs.readdir(path.resolve(srcdir, "routes"), { withFileTypes: true }))
.filter(x => x.isFile() && path.extname(x.name).toLowerCase() === ".js")
.map(x => path.basename(x.name, ".js"));
return routes;
}
function generateApp() {
let config;
return {
name: "generate-app",
configResolved(resolvedconfig) {
config = resolvedconfig;
},
async buildStart() {
try { await fs.access(APP_TMPDIR); }
catch { await fs.mkdir(APP_TMPDIR, { recursive: true }); }
const routes = await getRoutes();
const routesimports = routes.reduce((acc, x) => {
config.logger.info(`Found route /${x}`);
acc.push(`import ${x}_route from "$src/routes/${x}.js";`);
return acc;
}, []);
const routesmap = routes.map(x => {
return ` "/${x === "index" ? "" : x}": ${x}_route,`
});
const APP_TEMPLATE =
`import App from "$src/app.js"\n` +
`${routesimports.join("\n")}\n\n` +
`export default function initApp(options) {\n` +
` new App({\n` +
`${routesmap.join("\n")}\n` +
` }, options);\n` +
`}\n`;
await fs.writeFile(path.join(APP_TMPDIR, "index.js"), APP_TEMPLATE);
},
}
}
function generateSiteMap() {
let config;
return {
name: "generate-sitemap",
apply: "build",
configResolved(resolvedconfig) {
config = resolvedconfig;
},
async closeBundle() {
if (!pkg.homepage) {
config.logger.warn("Homepage not set, skipping sitemap generation");
return;
}
const homepage = pkg.homepage.replace(/\/+$/, "") + "/";
const routes = await getRoutes();
if (!routes.length) {
config.logger.warn("No routes found, skipping sitemap generation");
return;
}
const urlset = routes.map(x => {
return "" +
` <url>\n` +
` <loc>${homepage}${x}</loc>\n` +
` </url>`;
});
const SITEMAP_TEMPLATE =
`<?xml version="1.0" encoding="UTF-8"?>\n` +
`<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">\n` +
` <url>\n` +
` <loc>${homepage}</loc>\n` +
` </url>\n` +
urlset.join("\n") + "\n" +
`</urlset>`;
config.logger.info("Generating sitemap.xml");
try { await fs.access(config.build.outDir); }
catch { await fs.mkdir(config.build.outDir, { recursive: true }); }
await fs.writeFile(path.join(config.build.outDir, "sitemap.xml"), SITEMAP_TEMPLATE);
}
}
}
export default defineConfig({
plugins: [generateApp(), generateSiteMap()],
root: "src",
publicDir: path.resolve(__dirname, "public"),
resolve: {
alias: {
"$src": srcdir,
"$lib": path.resolve(srcdir, "lib"),
"$app": path.resolve(__dirname, APP_TMPDIR),
}
},
build: {
minify: "esbuild", // Use esbuild for faster builds
outDir: path.resolve(__dirname, "dist"),
emptyOutDir: true,
rollupOptions: {
output: {
manualChunks(id) { // Split libs, routes and vendor code
if (id.includes("lib/"))
return "lib";
else if (id.includes("routes/"))
return "routes";
else if (id.includes("node_modules/"))
return "vendor";
},
},
},
},
});