-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvite.config.ts
More file actions
129 lines (119 loc) · 3.33 KB
/
Copy pathvite.config.ts
File metadata and controls
129 lines (119 loc) · 3.33 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
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";
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",
}];
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 siteConfig(): UserConfig {
return {
root: resolve(repoRoot, "site"),
publicDir: false,
plugins: [distributionPlugin()],
server: {
host: "127.0.0.1",
},
preview: {
host: "127.0.0.1",
},
build: {
outDir: resolve(repoRoot, "dist/site"),
emptyOutDir: true,
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();
});