-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
82 lines (72 loc) · 2.28 KB
/
vite.config.js
File metadata and controls
82 lines (72 loc) · 2.28 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
import { defineConfig } from "vite";
import fs from "node:fs";
import path from "node:path";
import * as process from "node:process";
import { viteStaticCopy } from "vite-plugin-static-copy";
function collectHtmlInputs(rootDir) {
const inputs = {};
const rootIndex = path.resolve(rootDir, "index.html");
if (fs.existsSync(rootIndex)) inputs["index"] = rootIndex;
const pagesDir = path.resolve(rootDir, "to");
if (!fs.existsSync(pagesDir)) return inputs;
const walk = (dir) => {
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.resolve(dir, ent.name);
if (ent.isDirectory()) walk(full);
else if (ent.isFile() && ent.name === "index.html") {
const rel = path.relative(rootDir, full).split(path.sep).join("/");
const key = rel.replace(/\.html$/i, "");
inputs[key] = full;
}
}
};
walk(pagesDir);
return inputs;
}
function directoryIndexPlugin() {
const rewrite = (req, _res, next) => {
const original = req.url || "/";
const [pathname, qs] = original.split("?", 2);
if (pathname.endsWith("/") && !pathname.includes(".")) {
req.url = pathname + "index.html" + (qs ? `?${qs}` : "");
}
next();
};
return {
name: "directory-index",
configureServer(server) {
server.middlewares.use(rewrite);
},
configurePreviewServer(server) {
server.middlewares.use(rewrite);
},
};
}
export default defineConfig(() => {
const viteRoot = path.resolve(process.cwd(), "src");
const inputs = collectHtmlInputs(viteRoot);
if (Object.keys(inputs).length === 0) {
inputs.index = path.resolve(viteRoot, "index.html");
}
return {
base: "/",
root: "./src",
publicDir: "../public",
plugins: [
directoryIndexPlugin(),
viteStaticCopy({
targets: [
{ src: "js/*", dest: "js" },
],
}),
],
build: {
outDir: "../dist",
emptyOutDir: true,
minify: "esbuild",
rollupOptions: {
input: inputs,
},
},
};
});