summaryrefslogtreecommitdiff
path: root/packages/integrations/netlify/src/integration-functions.ts
blob: 2720eb5911677b91ba23ea88bff229225abdcdcb (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
import type { AstroAdapter, AstroIntegration, AstroConfig } from 'astro';
import fs from 'fs';

export function getAdapter(): AstroAdapter {
	return {
		name: '@astrojs/netlify/functions',
		serverEntrypoint: '@astrojs/netlify/netlify-functions.js',
		exports: ['handler'],
		args: {},
	};
}

interface NetlifyFunctionsOptions {
	dist?: URL;
}

function netlifyFunctions({ dist }: NetlifyFunctionsOptions = {}): AstroIntegration {
	let _config: AstroConfig;
	let entryFile: string;
	return {
		name: '@astrojs/netlify',
		hooks: {
			'astro:config:setup': ({ config }) => {
				if (dist) {
					config.outDir = dist;
				} else {
					config.outDir = new URL('./netlify/', config.root);
				}
			},
			'astro:config:done': ({ config, setAdapter }) => {
				setAdapter(getAdapter());
				_config = config;
			},
			'astro:build:start': async ({ buildConfig }) => {
				entryFile = buildConfig.serverEntry.replace(/\.m?js/, '');
				buildConfig.client = _config.outDir;
				buildConfig.server = new URL('./functions/', _config.outDir);
			},
			'astro:build:done': async ({ routes, dir }) => {
				const _redirectsURL = new URL('./_redirects', dir);

				// Create the redirects file that is used for routing.
				let _redirects = '';
				for (const route of routes) {
					if (route.pathname) {
						_redirects += `
${route.pathname}    /.netlify/functions/${entryFile}    200`;
					} else {
						const pattern =
							'/' + route.segments.map(([part]) => (part.dynamic ? '*' : part.content)).join('/');
						_redirects += `
${pattern}    /.netlify/functions/${entryFile}    200`;
					}
				}

				// Always use appendFile() because the redirects file could already exist,
				// e.g. due to a `/public/_redirects` file that got copied to the output dir.
				// If the file does not exist yet, appendFile() automatically creates it.
				await fs.promises.appendFile(_redirectsURL, _redirects, 'utf-8');
			},
		},
	};
}

export { netlifyFunctions, netlifyFunctions as default };