-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.js
More file actions
158 lines (132 loc) · 4.91 KB
/
Copy pathindex.js
File metadata and controls
158 lines (132 loc) · 4.91 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import fs from "node:fs/promises";
import path from "node:path";
import { checkResourceExists } from "@greenwood/cli/src/lib/resource-utils.js";
const DEFAULT_RUNTIME = "nodejs24.x";
// https://vercel.com/docs/functions/serverless-functions/runtimes/node-js#node.js-helpers
function generateOutputFormat(id, type) {
const handlerAlias = "$handler";
const path = type === "page" ? `${id}.route` : id;
return `
import { handler as ${handlerAlias} } from './${path}.js';
export default async function handler (request, response) {
const { body, url, headers = {}, method } = request;
const contentType = headers['content-type'] || '';
let format = body;
if (['GET', 'HEAD'].includes(method.toUpperCase())) {
format = null
} else if (contentType.includes('application/x-www-form-urlencoded')) {
const formData = new FormData();
for (const key of Object.keys(body)) {
formData.append(key, body[key]);
}
// when using FormData, let Request set the correct headers
// or else it will come out as multipart/form-data
// https://stackoverflow.com/a/43521052/417806
format = formData;
delete headers['content-type'];
} else if(contentType.includes('application/json')) {
format = JSON.stringify(body);
}
const req = new Request(new URL(url, \`http://\${headers.host}\`), {
body: format,
headers: new Headers(headers),
method
});
const res = await ${handlerAlias}(req);
res.headers.forEach((value, key) => {
response.setHeader(key, value);
});
response.status(res.status);
response.send(await res.text());
}
`;
}
async function setupFunctionBuildFolder(id, outputType, outputRoot, runtime) {
const outputFormat = generateOutputFormat(id, outputType);
await fs.mkdir(outputRoot, { recursive: true });
await fs.writeFile(new URL("./index.js", outputRoot), outputFormat);
await fs.writeFile(
new URL("./package.json", outputRoot),
JSON.stringify({
type: "module",
}),
);
await fs.writeFile(
new URL("./.vc-config.json", outputRoot),
JSON.stringify({
runtime,
handler: "index.js",
launcherType: "Nodejs",
shouldAddHelpers: true,
}),
);
}
async function vercelAdapter(compilation, options) {
const { runtime = DEFAULT_RUNTIME } = options;
const { outputDir, projectDirectory } = compilation.context;
const adapterOutputUrl = new URL("./.vercel/output/functions/", projectDirectory);
const ssrPages = compilation.graph.filter((page) => page.isSSR);
const apiRoutes = compilation.manifest.apis;
if (await checkResourceExists(adapterOutputUrl)) {
await fs.rm(adapterOutputUrl, { recursive: true });
}
await fs.mkdir(adapterOutputUrl, { recursive: true });
await fs.writeFile(
new URL("./.vercel/output/config.json", projectDirectory),
JSON.stringify({
version: 3,
}),
);
for (const page of ssrPages) {
const outputType = "page";
const { id, outputHref, route } = page;
// chop off the last / in route, and just use the id if the index route
const name = id === "index" ? id : `.${route.slice(0, -1)}`;
const outputRoot = new URL(`${name}.func/`, adapterOutputUrl);
const chunks = (await fs.readdir(outputDir)).filter(
(file) => file.startsWith(`${id}.route.chunk`) && file.endsWith(".js"),
);
await setupFunctionBuildFolder(id, outputType, outputRoot, runtime);
// handle user's actual route entry file
await fs.cp(
new URL(outputHref),
new URL(`./${outputHref.replace(outputDir.href, "")}`, outputRoot),
{ recursive: true },
);
// and any (URL) chunks for the page
for (const chunk of chunks) {
await fs.cp(new URL(`./${chunk}`, outputDir), new URL(`./${chunk}`, outputRoot), {
recursive: true,
});
}
}
for (const [key, value] of apiRoutes.entries()) {
const outputType = "api";
const { id, outputHref, route } = apiRoutes.get(key);
const outputRoot = new URL(`.${route}.func/`, adapterOutputUrl);
const { assets = [] } = value;
await setupFunctionBuildFolder(id, outputType, outputRoot, runtime);
await fs.cp(new URL(outputHref), new URL(`./${id}.js`, outputRoot), { recursive: true });
for (const asset of assets) {
const name = path.basename(asset);
await fs.cp(new URL(asset), new URL(`./${name}`, outputRoot), { recursive: true });
}
}
// static assets / build
await fs.cp(outputDir, new URL("./.vercel/output/static/", projectDirectory), {
recursive: true,
});
}
/** @type {import('./types/index.d.ts').VercelAdapter} */
const greenwoodPluginAdapterVercel = (options = {}) => [
{
type: "adapter",
name: "plugin-adapter-vercel",
provider: (compilation) => {
return async () => {
await vercelAdapter(compilation, options);
};
},
},
];
export { greenwoodPluginAdapterVercel };