-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvite.config.ts
58 lines (56 loc) · 1.62 KB
/
vite.config.ts
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
import fs from "node:fs";
import { tinyReactVitePlugin } from "@hiogawa/tiny-react/dist/plugins/vite";
import { vitePluginWorkerd } from "@hiogawa/vite-node-miniflare";
import { vitePluginSimpleHmr } from "@hiogawa/vite-plugin-simple-hmr";
import { Log } from "miniflare";
import { type Plugin, type ViteDevServer, defineConfig } from "vite";
export default defineConfig({
clearScreen: false,
appType: "custom",
plugins: [
vitePluginSimpleHmr({
include: new URL("./src/**/*.tsx", import.meta.url).pathname,
}),
vitePluginWorkerd({
entry: "/src/worker-entry",
miniflare: {
log: new Log(),
},
}),
tinyReactVitePlugin(),
vitePluginVirtualIndexHtml(),
],
environments: {
workerd: {
resolve: {
noExternal: true,
},
},
},
});
export function vitePluginVirtualIndexHtml(): Plugin {
let server: ViteDevServer | undefined;
return {
name: vitePluginVirtualIndexHtml.name,
configureServer(server_) {
server = server_;
},
resolveId(source, _importer, _options) {
return source === "virtual:index-html" ? "\0" + source : undefined;
},
async load(id, _options) {
if (id === "\0" + "virtual:index-html") {
let html: string;
if (server) {
this.addWatchFile("index.html");
html = await fs.promises.readFile("index.html", "utf-8");
html = await server.transformIndexHtml("/", html);
} else {
html = await fs.promises.readFile("dist/client/index.html", "utf-8");
}
return `export default ${JSON.stringify(html)}`;
}
return;
},
};
}