-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvite.config.ts
More file actions
158 lines (147 loc) · 4.51 KB
/
Copy pathvite.config.ts
File metadata and controls
158 lines (147 loc) · 4.51 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { defineConfig, type Plugin, type UserConfig } from "vite";
import {
DISTRIBUTION_CATALOG,
type DistributionMediaType,
} from "./site/catalog";
import { PROJECTS } from "./site/projects";
import {
renderLandingProjectCards,
renderLandingStructuredData,
} from "./site/render-projects";
const repoRoot = import.meta.dirname;
const publicRoot = resolve(repoRoot, "site/public");
interface DistributionFile {
readonly path: string;
readonly mediaType: DistributionMediaType;
}
function distributionFiles(): readonly DistributionFile[] {
const files: DistributionFile[] = [
{ path: "catalog.json", mediaType: "application/json" },
{ path: "favicon.ico", mediaType: "image/x-icon" },
{ path: "projects.json", mediaType: "application/json" },
...PROJECTS.map(({ preview }) => ({
path: preview.replace(/^\//u, ""),
mediaType: "image/webp" as const,
})),
{ path: "robots.txt", mediaType: "text/plain" },
{ path: "sitemap.xml", mediaType: "application/xml" },
];
for (const asset of DISTRIBUTION_CATALOG.assets) {
files.push(asset.preview);
files.push(...asset.resources.map((resource) => ({
path: `${asset.root}/${resource.path}`,
mediaType: resource.mediaType,
})));
}
const paths = files.map(({ path }) => path);
if (new Set(paths).size !== paths.length) {
throw new Error("The css.graphics distribution contains duplicate paths.");
}
return files;
}
function distributionPlugin(): Plugin {
const files = distributionFiles();
const byPath = new Map(files.map((file) => [file.path, file]));
return {
name: "cssgraphics-distribution",
configureServer(server) {
server.middlewares.use(async (request, response, next) => {
if (!request.url || !["GET", "HEAD"].includes(request.method ?? "GET")) {
next();
return;
}
let path: string;
try {
path = decodeURIComponent(
new URL(request.url, "http://css.graphics").pathname,
).replace(/^\//u, "");
} catch {
next();
return;
}
const file = byPath.get(path);
if (!file) {
next();
return;
}
try {
const bytes = await readFile(resolve(publicRoot, path));
response.statusCode = 200;
response.setHeader("Content-Type", file.mediaType);
response.setHeader("Content-Length", bytes.byteLength);
response.end(request.method === "HEAD" ? undefined : bytes);
} catch (error) {
next(error);
}
});
},
async buildStart() {
await Promise.all(files.map(async (file) => {
this.emitFile({
type: "asset",
fileName: file.path,
source: await readFile(resolve(publicRoot, file.path)),
});
}));
},
};
}
function landingHtmlPlugin(): Plugin {
return {
name: "cssgraphics-landing-html",
transformIndexHtml(html) {
const projectsMarker = "<!-- cssgraphics-projects -->";
const structuredDataMarker = "<!-- cssgraphics-structured-data -->";
if (!html.includes(projectsMarker) || !html.includes(structuredDataMarker)) {
throw new Error("The css.graphics landing HTML markers are missing.");
}
return html
.replace(projectsMarker, renderLandingProjectCards(PROJECTS))
.replace(structuredDataMarker, renderLandingStructuredData(PROJECTS));
},
};
}
function siteConfig(): UserConfig {
const deployBuild = process.env.CSSGRAPHICS_DEPLOY_BUILD === "1";
return {
root: resolve(repoRoot, "site"),
publicDir: false,
plugins: [landingHtmlPlugin(), distributionPlugin()],
server: {
host: "127.0.0.1",
},
preview: {
host: "127.0.0.1",
},
build: {
outDir: resolve(repoRoot, "dist/site"),
emptyOutDir: !deployBuild,
rollupOptions: {
input: resolve(repoRoot, "site/index.html"),
},
},
};
}
function libraryConfig(): UserConfig {
return {
build: {
outDir: resolve(repoRoot, "dist/runtime"),
emptyOutDir: true,
lib: {
entry: resolve(repoRoot, "src/bundle.ts"),
formats: ["es"],
fileName: "cssgraphics",
cssFileName: "cssgraphics",
},
rollupOptions: {
external: ["@layoutit/polycss", "@layoutit/polycss-morph"],
},
},
};
}
export default defineConfig(({ mode }) => {
if (mode === "library") return libraryConfig();
return siteConfig();
});