-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvite.config.js
More file actions
46 lines (42 loc) · 1.31 KB
/
Copy pathvite.config.js
File metadata and controls
46 lines (42 loc) · 1.31 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
import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { getPosts } from "./scripts/posts.mjs";
const rootDir = fileURLToPath(new URL(".", import.meta.url));
const POSTS_MANIFEST_ID = "virtual:posts-manifest";
const RESOLVED_POSTS_MANIFEST_ID = "\0" + POSTS_MANIFEST_ID;
const postsManifestPlugin = () => ({
name: "posts-manifest",
resolveId(id) {
if (id === POSTS_MANIFEST_ID) return RESOLVED_POSTS_MANIFEST_ID;
},
async load(id) {
if (id === RESOLVED_POSTS_MANIFEST_ID) {
const posts = await getPosts(rootDir);
return `export const posts = ${JSON.stringify(posts)};`;
}
},
handleHotUpdate({ file, server, modules }) {
if (file.replace(/\\/g, "/").includes("/posts/") && file.endsWith(".md")) {
const mod = server.moduleGraph.getModuleById(RESOLVED_POSTS_MANIFEST_ID);
if (mod) return [mod, ...modules];
}
},
async generateBundle() {
const posts = await getPosts(rootDir);
this.emitFile({
type: "asset",
fileName: "posts-manifest.json",
source: JSON.stringify(posts),
});
},
});
export default defineConfig({
base: "/",
plugins: [vue(), postsManifestPlugin()],
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
});