This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
forked from Neo-Ciber94/vite-ssr-react-context-issue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
66 lines (59 loc) · 1.45 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
59
60
61
62
63
64
65
66
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
import react from "@vitejs/plugin-react";
import { handleRequest } from "./src/entry.server";
import { prepareViteServer } from "./src/vite";
import * as esbuild from "esbuild";
import fs from "fs";
import path from "path";
const virtualModule = "virtual:app";
const resolvedVirtualModule = "\0" + virtualModule;
export default defineConfig({
plugins: [
react(),
tsconfigPaths(),
{
name: "virtual-modules",
resolveId(id) {
if (id === virtualModule) {
return resolvedVirtualModule;
}
},
async load(id) {
if (id === resolvedVirtualModule) {
console.log("Loaded virtual module");
const code = await loadVirtualModule();
const result = await esbuild.transform(code, {
loader: "tsx",
});
return result.code;
}
},
},
{
name: "dev-server",
configureServer(server) {
prepareViteServer(server);
return async () => {
server.middlewares.use(handleRequest);
};
},
},
],
});
async function loadVirtualModule() {
return `
import { Counter } from "/src/components";
import { useTheme } from "/src/context";
export default function App() {
const theme = useTheme();
return (
<div>
<h1>Hello World!</h1>
<h2>Theme: {theme}</h2>
<Counter />
</div>
);
}
`;
}