-
Notifications
You must be signed in to change notification settings - Fork 0
/
astro.config.ts
98 lines (82 loc) · 2.54 KB
/
astro.config.ts
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
import tailwind from "@astrojs/tailwind";
import { defineConfig } from "astro/config";
import solidJs from "@astrojs/solid-js";
import sitemap from "@astrojs/sitemap";
import arraybuffer from "vite-plugin-arraybuffer";
// https://astro.build/config
export default defineConfig({
site: "https://specta.dev",
integrations: [tailwind(), solidJs(), sitemap()],
build: {
// This is to ensure `/404.html` over `/404/index.html` for Cloudflare Pages.
// This will help Cloudflare Pages find the closest match for a 404 page.
format: "file",
},
vite: {
plugins: [
generateContentManifest(),
solidMarkdownEntrypoint(),
arraybuffer(),
],
},
});
// TODO: It would be good if this could all be either in Astro or an Astro plugin?
import type { Plugin } from "vite";
import path from "node:path";
import fs from "node:fs/promises";
function generateContentManifest(): Plugin {
const srcPath = path.resolve("src");
const basePath = path.resolve(srcPath, "content", "docs");
const run = async () => {
const files = await fs.readdir(basePath, {
withFileTypes: true,
recursive: true,
});
await fs.writeFile(
"./src/routes.gen.tsx",
`// @ts-nocheck\n// DO NOT MODIFY THIS FILE. It's generated by a Vite plugin!\n\nexport const manifest = {
${files
.filter((file) => file.isFile())
.map(
(file) =>
// `file.parentPath` is not defined on Linux so don't trust it.
`\t"${path.sep + path.relative(basePath, file.path) + path.sep + path.parse(file.name).name}": () => import(".${path.sep + path.relative(srcPath, file.path) + path.sep + file.name}?markdown"),`,
)
.join("\n")}\n} satisfies Record<string, () => unknown>;`,
);
};
return {
name: "generate-content-manifest",
buildStart() {
run();
this.addWatchFile("content");
},
watchChange(id) {
if (id.endsWith(".md")) run();
},
};
}
function solidMarkdownEntrypoint(): Plugin {
return {
name: "solid-markdown-entrypoint",
resolveId(id) {
const [idRaw, queryRaw] = id.split("?");
const query = new URLSearchParams(queryRaw);
if (query.has("markdown")) return `\0${id}`;
},
load(id) {
const [idRaw, queryRaw] = id.split("?");
const query = new URLSearchParams(queryRaw);
if (query.has("markdown")) {
return `import { compiledContent } from "${idRaw}";
import { ssr, ssrHydrationKey } from "solid-js/web";
export default function Component() {
if (import.meta.env.SSR) return ssr([compiledContent()]); // TODO: , ssrHydrationKey()
const child = document.createElement("div");
child.innerHTML = compiledContent();
return child;
}`;
}
},
};
}