-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvite.config.js
More file actions
133 lines (117 loc) · 4.07 KB
/
vite.config.js
File metadata and controls
133 lines (117 loc) · 4.07 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
import { exec } from "child_process";
import micromatch from "micromatch";
import path from "path";
import { promisify } from "util";
import { defineConfig } from "vite";
import { createRequire } from "module";
import serveStatic from "serve-static";
const require = createRequire(import.meta.url); // eslint-disable-line @typescript-eslint/no-unused-vars
const pathToDist = path.resolve("dist");
const execAsync = promisify(exec);
const rootDir = process.cwd(); // eslint-disable-line @typescript-eslint/no-unused-vars
import { parse } from "url";
function getReloadPath() {
const pathname = parse(
new URL("http://localhost:3000" + (globalThis.__CURRENT_PATH__ || "/"))
.pathname,
).pathname;
return pathname.endsWith("/") ? pathname + "index.html" : pathname;
}
const watchGlobs = [
"src/ts/**/*",
"src/scss/**/*",
"src/jinja/**/*",
"kist.yml",
];
let lastBuild = 0;
async function runKist(server) {
const now = Date.now();
if (now - lastBuild < 500) return;
lastBuild = now;
// eslint-disable-next-line no-console
console.log("[Kist] Running build...");
try {
const { stdout, stderr } = await execAsync(
"npx kist --config ./kist.dev.yml",
);
// eslint-disable-next-line no-console
if (stdout) console.log("[Kist] stdout:", stdout);
// eslint-disable-next-line no-console
if (stderr) console.error("[Kist] stderr:", stderr);
// eslint-disable-next-line no-console
console.log("[Kist] Build complete");
// setTimeout(() => {
// server?.ws.send({
// type: 'full-reload',
// path: '*'
// });
// }, 200);
setTimeout(() => {
const pathToReload = getReloadPath();
server?.ws.send({
type: "full-reload",
path: pathToReload,
});
}, 200);
} catch (err) {
// eslint-disable-next-line no-console
console.error("[Kist] Build failed:", err.stderr || err.message);
}
}
export default defineConfig({
root: ".",
publicDir: false,
server: {
port: 3000,
open: true,
fs: { strict: false },
},
resolve: {
alias: {
"~": path.resolve(__dirname, "./node_modules"),
"/js": path.resolve(__dirname, "./dist/js"),
"/css": path.resolve(__dirname, "./dist/css"),
},
},
plugins: [
{
name: "serve-kist-html",
configureServer(server) {
runKist(server);
server.middlewares.use(
"/css",
serveStatic(path.join(pathToDist, "css")),
);
server.middlewares.use(
"/js",
serveStatic(path.join(pathToDist, "js")),
);
server.middlewares.use(
"/font",
serveStatic(path.join(pathToDist, "font")),
);
// Serve / as index.html
server.middlewares.use((req, res, next) => {
if (req.url === "/") req.url = "/index.html";
next();
});
// Serve all static HTML/CSS/JS from dist/html
server.middlewares.use(serveStatic(path.resolve("dist/html")));
// server.middlewares.use((req, res, next) => {
// if (req.url === '/' || req.url === '/index.html') {
// req.url = '/dist/html/index.html';
// }
// next();
// });
server.watcher.on("change", (file) => {
const relativePath = path.relative(process.cwd(), file);
if (micromatch.isMatch(relativePath, watchGlobs)) {
// eslint-disable-next-line no-console
console.log(`[Kist] File changed: ${relativePath}`);
runKist(server);
}
});
},
},
],
});