-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (83 loc) · 2.13 KB
/
server.js
File metadata and controls
92 lines (83 loc) · 2.13 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
import { directives, wrapExportsPlugin } from "@vinxi/plugin-directives";
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { chunkify } from "vinxi/lib/chunks";
import { handlerModule, join, normalize } from "vinxi/lib/path";
import { CLIENT_REFERENCES_MANIFEST } from "./constants.js";
export function serverTransform({ runtime }) {
return directives({
hash: chunkify,
runtime: runtime,
transforms: [
wrapExportsPlugin({
runtime: {
module: runtime,
function: "createServerReference",
},
hash: chunkify,
apply: (code, id, options) => {
return options.ssr;
},
pragma: "use socket",
}),
],
onReference(type, reference) {},
});
}
/**
*
* @returns {import('vinxi').Plugin}
*/
export const serverBuild = ({ client, manifest }) => {
let input;
return {
name: "server-functions:build",
enforce: "post",
apply: "build",
config(config, env) {
// @ts-ignore
const router = config.router;
// @ts-ignore
const app = config.app;
const rscRouter = app.getRouter(client);
const serverFunctionsManifest = JSON.parse(
readFileSync(join(rscRouter.outDir, rscRouter.base, manifest), "utf-8")
);
input = {
entry: handlerModule(router),
...Object.fromEntries(
serverFunctionsManifest.server.map((key) => {
return [chunkify(key), key];
})
),
};
return {
build: {
rollupOptions: {
output: {
chunkFileNames: "[name].mjs",
entryFileNames: "[name].mjs",
},
treeshake: true,
},
},
};
},
configResolved(config) {
config.build.rollupOptions.input = input;
},
};
};
/**
*
* @returns {import('vinxi').Plugin[]}
*/
export function server({
client = "client",
manifest = CLIENT_REFERENCES_MANIFEST,
runtime = normalize(
fileURLToPath(new URL("./server-runtime.js", import.meta.url))
),
} = {}) {
return [serverTransform({ runtime }), serverBuild({ client, manifest })];
}