-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
111 lines (104 loc) · 2.85 KB
/
Copy pathvite.config.ts
File metadata and controls
111 lines (104 loc) · 2.85 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
import { defineConfig } from "vite";
import importMetaUrlPlugin from "@codingame/esbuild-import-meta-url-plugin";
import { existsSync } from "node:fs";
import path from "node:path";
const crossOriginIsolationHeaders = {
"Cross-Origin-Embedder-Policy": "require-corp",
"Cross-Origin-Opener-Policy": "same-origin",
};
const contentTypes = new Map([
[".js", "text/javascript"],
[".wasm", "application/wasm"],
]);
function precompressedPreview() {
return {
name: "precompressed-preview",
configurePreviewServer(server) {
server.middlewares.use((request, response, next) => {
if (
!request.url ||
!/\bbr\b/.test(request.headers["accept-encoding"] ?? "")
) {
return next();
}
const pathname = decodeURIComponent(
new URL(request.url, "http://localhost").pathname,
);
const extension = path.extname(pathname);
const contentType = contentTypes.get(extension);
if (!contentType) return next();
const relativePath = pathname.replace(/^\/+/, "");
const candidate = path.resolve(
server.config.root,
server.config.build.outDir,
`${relativePath}.br`,
);
const outputRoot = path.resolve(
server.config.root,
server.config.build.outDir,
);
if (
!candidate.startsWith(`${outputRoot}${path.sep}`) ||
!existsSync(candidate)
) {
return next();
}
request.url = `${pathname}.br`;
response.setHeader("Content-Encoding", "br");
response.setHeader("Content-Type", contentType);
response.setHeader("Vary", "Accept-Encoding");
next();
});
},
};
}
export default defineConfig({
plugins: [precompressedPreview()],
base: "./",
define: {
__CLANGD_WASM_BASE__: JSON.stringify(process.env.CLANGD_WASM_BASE ?? ""),
},
publicDir: false,
server: {
headers: crossOriginIsolationHeaders,
},
preview: {
headers: crossOriginIsolationHeaders,
},
build: {
target: "es2022",
cssCodeSplit: false,
lib: {
entry: {
editor: "src/runtime.ts",
simple: "src/simple.ts",
ansi: "src/ansi.ts",
"codeblocks-module": "src/codeblocks-entry.ts",
},
formats: ["es"],
fileName: (_format, entryName) => `${entryName}.js`,
cssFileName: "codeblocks",
},
rollupOptions: {
output: {
assetFileNames: (asset) =>
asset.name?.endsWith(".css")
? "codeblocks.css"
: "assets/[name]-[hash][extname]",
chunkFileNames: "assets/[name]-[hash].js",
},
},
},
optimizeDeps: {
esbuildOptions: {
tsconfig: "./tsconfig.json",
plugins: [importMetaUrlPlugin],
},
},
worker: {
format: "es",
},
resolve: {
dedupe: ["monaco-editor", "vscode"],
},
});