forked from nitrojs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreset.ts
More file actions
115 lines (107 loc) · 3.26 KB
/
Copy pathpreset.ts
File metadata and controls
115 lines (107 loc) · 3.26 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
import { defineNitroPreset } from "../_utils/preset.ts";
import type { Nitro } from "nitro/types";
import { presetsDir } from "nitro/meta";
import { join } from "pathe";
import {
deprecateSWR,
generateFunctionFiles,
generateStaticFiles,
resolveVercelRuntime,
} from "./utils.ts";
export type { VercelOptions as PresetOptions } from "./types.ts";
// https://vercel.com/docs/build-output-api/v3
const vercel = defineNitroPreset(
{
entry: "./vercel/runtime/vercel.{format}",
manifest: {
deploymentId: process.env.VERCEL_DEPLOYMENT_ID,
},
vercel: {
skewProtection: !!process.env.VERCEL_SKEW_PROTECTION_ENABLED,
cronHandlerRoute: "/_vercel/cron",
},
output: {
dir: "{{ rootDir }}/.vercel/output",
serverDir: "{{ output.dir }}/functions/__server.func",
publicDir: "{{ output.dir }}/static/{{ baseURL }}",
},
commands: {
preview: "npx srvx --static ../../static ./functions/__server.func/index.mjs",
deploy: "npx vercel deploy --prebuilt",
},
hooks: {
"build:before": async (nitro: Nitro) => {
const logger = nitro.logger.withTag("vercel");
// Runtime
const runtime = await resolveVercelRuntime(nitro);
if (runtime.startsWith("bun") && !nitro.options.exportConditions!.includes("bun")) {
nitro.options.exportConditions!.push("bun");
}
logger.info(`Using \`${runtime}\` runtime.`);
// Entry handler format
let serverFormat = nitro.options.vercel?.entryFormat;
if (!serverFormat) {
const hasNodeHandler = nitro.routing.routes.routes
.flatMap((r) => r.data)
.some((h) => h.format === "node");
serverFormat = hasNodeHandler ? "node" : "web";
}
logger.info(`Using \`${serverFormat}\` entry format.`);
nitro.options.entry = nitro.options.entry.replace("{format}", serverFormat);
// Cron tasks handler
if (
nitro.options.experimental.tasks &&
Object.keys(nitro.options.scheduledTasks || {}).length > 0
) {
nitro.options.handlers.push({
route: nitro.options.vercel!.cronHandlerRoute || "/_vercel/cron",
lazy: true,
handler: join(presetsDir, "vercel/runtime/cron-handler"),
});
}
},
"rollup:before": (nitro: Nitro) => {
deprecateSWR(nitro);
},
async compiled(nitro: Nitro) {
await generateFunctionFiles(nitro);
},
},
},
{
name: "vercel" as const,
stdName: "vercel",
}
);
const vercelStatic = defineNitroPreset(
{
extends: "static",
manifest: {
deploymentId: process.env.VERCEL_DEPLOYMENT_ID,
},
vercel: {
skewProtection: !!process.env.VERCEL_SKEW_PROTECTION_ENABLED,
},
output: {
dir: "{{ rootDir }}/.vercel/output",
publicDir: "{{ output.dir }}/static/{{ baseURL }}",
},
commands: {
preview: "npx serve ./static",
},
hooks: {
"rollup:before": (nitro: Nitro) => {
deprecateSWR(nitro);
},
async compiled(nitro: Nitro) {
await generateStaticFiles(nitro);
},
},
},
{
name: "vercel-static" as const,
stdName: "vercel",
static: true,
}
);
export default [vercel, vercelStatic] as const;