-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
adapter.ts
74 lines (67 loc) · 2.3 KB
/
adapter.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
67
68
69
70
71
72
73
74
import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro';
import { writeJson, getVercelOutput } from '../lib/fs.js';
import { getRedirects } from '../lib/redirects.js';
const PACKAGE_NAME = '@astrojs/vercel/edge';
function getAdapter(): AstroAdapter {
return {
name: PACKAGE_NAME,
serverEntrypoint: `${PACKAGE_NAME}/entrypoint`,
exports: ['default'],
};
}
export default function vercelEdge(): AstroIntegration {
let _config: AstroConfig;
let functionFolder: URL;
let serverEntry: string;
return {
name: PACKAGE_NAME,
hooks: {
'astro:config:setup': ({ config }) => {
config.outDir = getVercelOutput(config.root);
},
'astro:config:done': ({ setAdapter, config }) => {
setAdapter(getAdapter());
_config = config;
},
'astro:build:setup': ({ vite, target }) => {
if (target === 'server') {
vite.resolve ||= {};
vite.resolve.alias ||= {};
const alias = vite.resolve.alias as Record<string, string>;
alias['react-dom/server'] = 'react-dom/server.browser';
vite.ssr = {
noExternal: true,
};
}
},
'astro:build:start': async ({ buildConfig }) => {
if (String(process.env.ENABLE_VC_BUILD) !== '1') {
throw new Error(
`The enviroment variable "ENABLE_VC_BUILD" was not found. Make sure you have it set to "1" in your Vercel project.\nLearn how to set enviroment variables here: https://vercel.com/docs/concepts/projects/environment-variables`
);
}
buildConfig.serverEntry = serverEntry = 'entry.mjs';
buildConfig.client = new URL('./static/', _config.outDir);
buildConfig.server = functionFolder = new URL('./functions/render.func/', _config.outDir);
},
'astro:build:done': async ({ routes }) => {
// Edge function config
// https://vercel.com/docs/build-output-api/v3#vercel-primitives/edge-functions/configuration
await writeJson(new URL(`./.vc-config.json`, functionFolder), {
runtime: 'edge',
entrypoint: serverEntry,
});
// Output configuration
// https://vercel.com/docs/build-output-api/v3#build-output-configuration
await writeJson(new URL(`./config.json`, _config.outDir), {
version: 3,
routes: [
...getRedirects(routes, _config),
{ handle: 'filesystem' },
{ src: '/.*', middlewarePath: 'render' },
],
});
},
},
};
}