-
Notifications
You must be signed in to change notification settings - Fork 7
/
vite.config.js
55 lines (45 loc) · 1.38 KB
/
vite.config.js
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
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";
import { promises as fs } from "fs";
function ssrPlugin() {
/**
* @type {import('vite').Plugin}
*/
return {
name: "ssrPlugin",
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url !== "/") {
return next();
}
const { renderInNode } = await server.ssrLoadModule(
path.resolve(__dirname, "./src/entry-server")
);
const indexHtml = await fs.readFile(
path.resolve(__dirname, "./index.html"),
"utf-8"
);
const url = new URL("http://localhost:3000/" + req.url);
const template = await server.transformIndexHtml(
url.toString(),
indexHtml
);
/**
* Scrape out `head` contents injected by Vite. This is used for React runtime and fast refresh.
* It will be injected into the `<Html>` React component shell in the server entrypoint.
*/
const head = template.match(/<head>(.+?)<\/head>/s)[1];
return renderInNode({ res, head });
});
},
};
}
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), ssrPlugin()],
ssr: {
noExternal: Boolean(process.env.WORKER),
target: process.env.WORKER ? "webworker" : "node",
},
});