-
-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathvite.config.ts
More file actions
141 lines (137 loc) · 4.37 KB
/
vite.config.ts
File metadata and controls
141 lines (137 loc) · 4.37 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
import {type Plugin, type ResolvedConfig, defineConfig} from "vite";
import react from "@vitejs/plugin-react";
import basicSsl from "@vitejs/plugin-basic-ssl";
import svgr from "vite-plugin-svgr";
import {cloudflare} from "@cloudflare/vite-plugin";
import path from "node:path";
const headers = {
// Allow SharedArrayBuffer to work locally
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp",
// Allow the service worker to intercept all paths, even when
// initiated from a year subpath.
"Service-Worker-Allowed": "/",
};
export default defineConfig(() => {
return {
environments: {
client: {
build: {
rollupOptions: {
input: {
main: path.resolve(__dirname, "index.html"),
monkey: path.resolve(
__dirname,
"monkey/index.html"
),
},
},
},
},
},
build: {
outDir: "build",
minify: true,
// the library index is 1.5MB (before gzip), which is unavoidable.
chunkSizeWarningLimit: 2048,
},
worker: {
format: "es" as const,
},
assetsInclude: [
"**/*.rom",
"**/*.hda",
"**/*nvram.bin",
"**/*pram.bin",
],
resolve: {
alias: {
"@": path.resolve(__dirname, "src"),
},
},
server: {
port: 3127,
headers,
host: "0.0.0.0",
},
preview: {
port: 4127,
headers,
},
clearScreen: false,
plugins: [
ssrDevCssPlugin(),
basicSslWrapped(),
react(),
svgr(),
cloudflare(),
],
};
});
// The basicSsl() plugin will set up https for both dev/serve and preview, but we only
// want it for the latter. The plugin doesn't support options
// (https://github.com/vitejs/vite-plugin-basic-ssl/issues/9), so we instead
// wrap it and undo the dev/serve https config after it's done.
function basicSslWrapped() {
const originalBasicSsl = basicSsl();
let configResolved:
| ((config: ResolvedConfig) => void | Promise<void>)
| undefined;
const {configResolved: originalConfigResolved} = originalBasicSsl;
if (typeof originalConfigResolved === "function") {
configResolved = function (config) {
const originalResult = originalConfigResolved(config);
if (originalResult instanceof Promise) {
return originalResult.then(() => {
delete config.server.https;
});
}
delete config.server.https;
};
}
return {
...originalBasicSsl,
configResolved,
};
}
// Minimal plugin based on https://github.com/hi-ogawa/vite-plugins/tree/main/packages/ssr-css
// that injects all CSS into the HTML response so that we don't have a flash of
// unstyled content in dev mode (when Vite normally injects the CSS via JS-generated
// <style> tag).
function ssrDevCssPlugin(): Plugin {
const virtualCssPath = "/@virtual:ssr-css.css";
const collectedStyles = new Map<string, string>();
return {
name: "ssr-dev-css",
apply: "serve",
transform(code, id) {
if (id.includes("node_modules")) return null;
if (id.endsWith(".css")) {
collectedStyles.set(id, code);
}
return null;
},
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url === virtualCssPath) {
res.setHeader("Content-Type", "text/css");
res.end(Array.from(collectedStyles.values()).join("\n"));
return;
}
next();
});
},
transformIndexHtml() {
return [
{
tag: "link",
injectTo: "head",
attrs: {
rel: "stylesheet",
href: virtualCssPath,
},
},
];
},
};
}