summaryrefslogtreecommitdiff
path: root/packages/integrations/vercel/src/index.ts
blob: 838844a08a516e4748186f3a3e5eb814960eecd1 (plain) (blame)
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
import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'astro';
import type { PathLike } from 'fs';
import fs from 'fs/promises';
import { fileURLToPath } from 'url';
import esbuild from 'esbuild';

const writeJson = (path: PathLike, data: any) =>
	fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' });

const ENTRYFILE = '__astro_entry';

export function getAdapter(): AstroAdapter {
	return {
		name: '@astrojs/vercel',
		serverEntrypoint: '@astrojs/vercel/server-entrypoint',
		exports: ['default'],
	};
}

export default function vercel(): AstroIntegration {
	let _config: AstroConfig;
	let _serverEntry: URL;

	return {
		name: '@astrojs/vercel',
		hooks: {
			'astro:config:setup': ({ config }) => {
				config.outDir = new URL('./.output/', config.root);
				config.build.format = 'directory';
			},
			'astro:config:done': ({ setAdapter, config }) => {
				setAdapter(getAdapter());
				_config = config;
				_serverEntry = new URL(`./server/pages/${ENTRYFILE}.js`, config.outDir);
			},
			'astro:build:setup': ({ vite, target }) => {
				if (target === 'server') {
					vite.build!.rollupOptions = {
						input: [],
						output: {
							format: 'cjs',
							file: fileURLToPath(_serverEntry),
							dir: undefined,
							entryFileNames: undefined,
							chunkFileNames: undefined,
							assetFileNames: undefined,
							inlineDynamicImports: true,
						},
					};
				}
			},
			'astro:build:start': async ({ buildConfig }) => {
				buildConfig.serverEntry = `${ENTRYFILE}.js`;
				buildConfig.client = new URL('./static/', _config.outDir);
				buildConfig.server = new URL('./server/pages/', _config.outDir);

				if (String(process.env.ENABLE_FILE_SYSTEM_API) !== '1') {
					console.warn(
						`The enviroment variable "ENABLE_FILE_SYSTEM_API" 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`
					);
				}
			},
			'astro:build:done': async ({ routes }) => {
				// Bundle dependecies
				await esbuild.build({
					entryPoints: [fileURLToPath(_serverEntry)],
					outfile: fileURLToPath(_serverEntry),
					bundle: true,
					format: 'cjs',
					platform: 'node',
					target: 'node14',
					allowOverwrite: true,
					minifyWhitespace: true,
				});

				let staticRoutes: RouteData[] = [];
				let dynamicRoutes: RouteData[] = [];

				for (const route of routes) {
					if (route.params.length === 0) staticRoutes.push(route);
					else dynamicRoutes.push(route);
				}

				// Routes Manifest
				// https://vercel.com/docs/file-system-api#configuration/routes
				await writeJson(new URL(`./routes-manifest.json`, _config.outDir), {
					version: 3,
					basePath: '/',
					pages404: false,
					redirects:
						_config.trailingSlash !== 'ignore'
							? routes
									.filter((route) => route.type === 'page' && !route.pathname?.endsWith('/'))
									.map((route) => {
										const path =
											'/' +
											route.segments
												.map((segments) =>
													segments
														.map((part) =>
															part.spread
																? `:${part.content}*`
																: part.dynamic
																? `:${part.content}`
																: part.content
														)
														.join('')
												)
												.join('/');

										let source, destination;

										if (_config.trailingSlash === 'always') {
											source = path;
											destination = path + '/';
										} else {
											source = path + '/';
											destination = path;
										}

										return { source, destination, statusCode: 308 };
									})
							: undefined,
					rewrites: staticRoutes.map((route) => {
						let source = route.pathname as string;

						if (
							route.type === 'page' &&
							_config.trailingSlash === 'always' &&
							!source.endsWith('/')
						) {
							source += '/';
						}

						return {
							source,
							regex: route.pattern.toString(),
							destination: `/${ENTRYFILE}`,
						};
					}),
					dynamicRoutes: dynamicRoutes.map((route) => ({
						page: `/${ENTRYFILE}`,
						regex: route.pattern.toString(),
					})),
				});
			},
		},
	};
}