summaryrefslogtreecommitdiff
path: root/packages/integrations/vercel/src/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/vercel/src/index.ts')
-rw-r--r--packages/integrations/vercel/src/index.ts89
1 files changed, 37 insertions, 52 deletions
diff --git a/packages/integrations/vercel/src/index.ts b/packages/integrations/vercel/src/index.ts
index 5d6794206..648f624b0 100644
--- a/packages/integrations/vercel/src/index.ts
+++ b/packages/integrations/vercel/src/index.ts
@@ -1,79 +1,64 @@
-import type { AstroIntegration, AstroConfig } from 'astro';
-import type { IncomingMessage, ServerResponse } from 'http';
+import type { AstroAdapter, AstroIntegration } from 'astro';
import type { PathLike } from 'fs';
-
import fs from 'fs/promises';
-import { fileURLToPath } from 'url';
-import { globby } from 'globby';
-import esbuild from 'esbuild';
-
-export type VercelRequest = IncomingMessage;
-export type VercelResponse = ServerResponse;
-export type VercelHandler = (request: VercelRequest, response: VercelResponse) => void | Promise<void>;
const writeJson = (path: PathLike, data: any) => fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' });
-const ENDPOINT_GLOB = 'api/**/*.{js,ts,tsx}';
+export function getAdapter(): AstroAdapter {
+ return {
+ name: '@astrojs/vercel',
+ serverEntrypoint: '@astrojs/vercel/server-entrypoint',
+ exports: ['_default'],
+ };
+}
-function vercelFunctions(): AstroIntegration {
- let _config: AstroConfig;
- let output: URL;
+export default function vercel(): AstroIntegration {
+ let entryFile: string;
return {
name: '@astrojs/vercel',
hooks: {
- 'astro:config:setup': ({ config, ignorePages }) => {
- output = new URL('./.output/', config.projectRoot);
- config.dist = new URL('./static/', output);
+ 'astro:config:setup': ({ config }) => {
+ config.dist = new URL('./.output/', config.projectRoot);
config.buildOptions.pageUrlFormat = 'directory';
- ignorePages(ENDPOINT_GLOB);
},
- 'astro:config:done': async ({ config }) => {
- _config = config;
+ 'astro:config:done': ({ setAdapter }) => {
+ setAdapter(getAdapter());
},
- 'astro:build:start': async () => {
- await fs.rm(output, { recursive: true, force: true });
+ 'astro:build:start': async ({ buildConfig, config }) => {
+ entryFile = buildConfig.serverEntry;
+ buildConfig.client = new URL('./static/', config.dist);
+ buildConfig.server = new URL('./functions/', config.dist);
},
- 'astro:build:done': async ({ pages }) => {
- // Split pages from the rest of files
- await Promise.all(
- pages.map(async ({ pathname }) => {
- const origin = new URL(`./static/${pathname}index.html`, output);
- const finalDir = new URL(`./server/pages/${pathname}`, output);
-
- await fs.mkdir(finalDir, { recursive: true });
- await fs.copyFile(origin, new URL(`./index.html`, finalDir));
- await fs.rm(origin);
- })
- );
+ 'astro:build:done': async ({ dir, routes }) => {
+ await writeJson(new URL(`./functions/package.json`, dir), {
+ type: 'commonjs',
+ });
// Routes Manifest
// https://vercel.com/docs/file-system-api#configuration/routes
- await writeJson(new URL(`./routes-manifest.json`, output), {
+ await writeJson(new URL(`./routes-manifest.json`, dir), {
version: 3,
basePath: '/',
pages404: false,
+ rewrites: routes.map((route) => ({
+ source: route.pathname,
+ destination: '/__astro_entry',
+ })),
});
- const endpoints = await globby([ENDPOINT_GLOB, '!_*'], { onlyFiles: true, cwd: _config.pages });
-
- if (endpoints.length === 0) return;
-
- await esbuild.build({
- entryPoints: endpoints.map((endpoint) => new URL(endpoint, _config.pages)).map(fileURLToPath),
- outdir: fileURLToPath(new URL('./server/pages/api/', output)),
- outbase: fileURLToPath(new URL('./api/', _config.pages)),
- inject: [fileURLToPath(new URL('./shims.js', import.meta.url))],
- bundle: true,
- target: 'node14',
- platform: 'node',
- format: 'cjs',
+ // 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}`,
+ },
+ },
});
-
- await writeJson(new URL(`./package.json`, output), { type: 'commonjs' });
},
},
};
}
-
-export default vercelFunctions;