summaryrefslogtreecommitdiff
path: root/packages/integrations/vercel/src/index.ts
blob: 439eeeb04ce2d4d54a3c1577b222b8241df408a0 (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
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,
					rewrites: staticRoutes.map((route) => ({
						source: route.pathname,
						regex: route.pattern.toString(),
						destination: `/${ENTRYFILE}`,
					})),
					dynamicRoutes: dynamicRoutes.map((route) => ({
						page: `/${ENTRYFILE}`,
						regex: route.pattern.toString(),
					})),
				});
			},
		},
	};
}