2022-03-30 02:01:37 +00:00
|
|
|
import type { AstroAdapter, AstroIntegration } from 'astro';
|
2022-03-28 13:05:55 +00:00
|
|
|
import type { PathLike } from 'fs';
|
2022-03-28 20:55:03 +00:00
|
|
|
import fs from 'fs/promises';
|
2022-03-28 13:05:55 +00:00
|
|
|
|
|
|
|
const writeJson = (path: PathLike, data: any) => fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' });
|
|
|
|
|
2022-03-30 02:01:37 +00:00
|
|
|
export function getAdapter(): AstroAdapter {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/vercel',
|
|
|
|
serverEntrypoint: '@astrojs/vercel/server-entrypoint',
|
|
|
|
exports: ['_default'],
|
|
|
|
};
|
|
|
|
}
|
2022-03-28 18:13:14 +00:00
|
|
|
|
2022-03-30 02:01:37 +00:00
|
|
|
export default function vercel(): AstroIntegration {
|
|
|
|
let entryFile: string;
|
2022-03-28 18:13:14 +00:00
|
|
|
|
2022-03-28 13:05:55 +00:00
|
|
|
return {
|
|
|
|
name: '@astrojs/vercel',
|
|
|
|
hooks: {
|
2022-03-30 02:01:37 +00:00
|
|
|
'astro:config:setup': ({ config }) => {
|
|
|
|
config.dist = new URL('./.output/', config.projectRoot);
|
2022-03-28 13:05:55 +00:00
|
|
|
config.buildOptions.pageUrlFormat = 'directory';
|
|
|
|
},
|
2022-03-30 02:01:37 +00:00
|
|
|
'astro:config:done': ({ setAdapter }) => {
|
|
|
|
setAdapter(getAdapter());
|
2022-03-28 13:05:55 +00:00
|
|
|
},
|
2022-03-30 02:01:37 +00:00
|
|
|
'astro:build:start': async ({ buildConfig, config }) => {
|
|
|
|
entryFile = buildConfig.serverEntry;
|
|
|
|
buildConfig.client = new URL('./static/', config.dist);
|
|
|
|
buildConfig.server = new URL('./functions/', config.dist);
|
2022-03-28 13:05:55 +00:00
|
|
|
},
|
2022-03-30 02:01:37 +00:00
|
|
|
'astro:build:done': async ({ dir, routes }) => {
|
|
|
|
await writeJson(new URL(`./functions/package.json`, dir), {
|
|
|
|
type: 'commonjs',
|
|
|
|
});
|
2022-03-28 13:05:55 +00:00
|
|
|
|
|
|
|
// Routes Manifest
|
|
|
|
// https://vercel.com/docs/file-system-api#configuration/routes
|
2022-03-30 02:01:37 +00:00
|
|
|
await writeJson(new URL(`./routes-manifest.json`, dir), {
|
2022-03-28 13:05:55 +00:00
|
|
|
version: 3,
|
|
|
|
basePath: '/',
|
|
|
|
pages404: false,
|
2022-03-30 02:04:53 +00:00
|
|
|
// redirects: [
|
|
|
|
// {
|
|
|
|
// source: '/nice/',
|
|
|
|
// destination: '/stuff',
|
|
|
|
// statusCode: 308,
|
|
|
|
// regex: '^/nice.*$',
|
|
|
|
// },
|
|
|
|
// ],
|
2022-03-30 02:01:37 +00:00
|
|
|
rewrites: routes.map((route) => ({
|
|
|
|
source: route.pathname,
|
|
|
|
destination: '/__astro_entry',
|
|
|
|
})),
|
2022-03-28 13:05:55 +00:00
|
|
|
});
|
2022-03-28 18:13:14 +00:00
|
|
|
|
2022-03-30 02:01:37 +00:00
|
|
|
// Functions Manifest
|
|
|
|
// https://vercel.com/docs/file-system-api#configuration/functions
|
|
|
|
await writeJson(new URL(`./functions-manifest.json`, dir), {
|
|
|
|
version: 1,
|
|
|
|
pages: {
|
|
|
|
__astro_entry: {
|
|
|
|
runtime: 'nodejs14',
|
|
|
|
handler: `functions/${entryFile}`,
|
|
|
|
},
|
|
|
|
},
|
2022-03-28 18:13:14 +00:00
|
|
|
});
|
2022-03-28 13:05:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|