forked from lihaoze123/xcpc-statement-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
116 lines (104 loc) · 3.4 KB
/
Copy pathvite.config.ts
File metadata and controls
116 lines (104 loc) · 3.4 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
import { resolve } from "node:path";
import { defineConfig, type UserConfig, type PluginOption } from "vite";
import react from "@vitejs/plugin-react";
import { exec } from "node:child_process";
export default defineConfig(async (): Promise<UserConfig> => {
const TypstFontUrlEntriesPlugin = (): PluginOption => {
const name = "typst-font-url-entries-plugin";
const virtualModuleId = "virtual:typst-font-url-entries";
const resolvedVirtualModuleId = "\0" + virtualModuleId;
let pluginLoadResult: string | undefined = undefined;
return {
name,
resolveId(id) {
if (id === virtualModuleId) {
return resolvedVirtualModuleId;
}
},
async load(id) {
if (id !== resolvedVirtualModuleId) return;
if (!pluginLoadResult) {
const fs = await import("node:fs/promises");
const files = await fs.readdir("assets/fonts");
const fontAssetsUrls = await Promise.all(
files
.filter((file) =>
[".woff2", ".woff", ".ttf", ".otf"].some((ext) =>
file.endsWith(ext),
),
)
.map(async (file) => {
const fontkit = await import("fontkit");
const fontBuffer = await fs.readFile("assets/fonts/" + file);
const fontInfo = fontkit.create(fontBuffer);
if (!("postscriptName" in fontInfo))
throw new Error(
`Font file ${file} does not have a PostScript name.`,
);
return [fontInfo.postscriptName, `assets/fonts/${file}`];
}),
);
pluginLoadResult =
fontAssetsUrls
.map(
([, url], index) => `import font${index}Url from "${url}?url";\n`,
)
.join("") +
"\n" +
"const fontUrlEntries = [\n" +
fontAssetsUrls
.map(([name], index) => ` ["${name}", font${index}Url],\n`)
.join("") +
"];\n" +
"\n" +
"export default fontUrlEntries;\n";
}
return pluginLoadResult;
},
};
};
const gitCommitHash = await new Promise<undefined | string>((resolve) =>
exec("git rev-parse --short HEAD", (err, stdout) => {
if (err) resolve(undefined);
else resolve(stdout.trim());
}),
);
const isDirty = await new Promise<boolean>((resolve) =>
exec("git diff-index --quiet HEAD", (err) => resolve(Boolean(err))),
);
return {
base: "./",
plugins: [react(), TypstFontUrlEntriesPlugin()],
resolve: {
alias: [
{ find: "@", replacement: resolve("src") },
{ find: "typst-template", replacement: resolve("typst-template") },
{ find: "assets", replacement: resolve("assets") },
],
},
define: {
GIT_COMMIT_INFO: JSON.stringify(
gitCommitHash === undefined
? "unknown"
: gitCommitHash + (isDirty ? "-dirty" : ""),
),
},
server: {
port: 4482,
headers: {
"Cross-Origin-Embedder-Policy": "require-corp",
"Cross-Origin-Opener-Policy": "same-origin",
},
},
worker: {
format: "es",
},
optimizeDeps: {
exclude: [
"@myriaddreamin/typst.ts",
"@myriaddreamin/typst-ts-web-compiler",
"@myriaddreamin/typst-ts-renderer",
],
},
};
});