mirror of
https://github.com/withastro/astro.git
synced 2025-03-10 23:01:26 -05:00
Build API Routes
This commit is contained in:
parent
9b780eb456
commit
3bb190b53d
2 changed files with 28 additions and 6 deletions
|
@ -23,8 +23,9 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@astrojs/webapi": "^0.11.0",
|
||||
"@vercel/nft": "^0.18.0",
|
||||
"@vercel/node": "^1.14.0"
|
||||
"@vercel/node": "^1.14.0",
|
||||
"esbuild": "0.14.25",
|
||||
"globby": "^12.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"astro": "workspace:*",
|
||||
|
|
|
@ -1,30 +1,37 @@
|
|||
import type { AstroIntegration, AstroConfig } from 'astro';
|
||||
import fs from 'fs/promises';
|
||||
import type { PathLike } from 'fs';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { globby } from 'globby';
|
||||
import esbuild from 'esbuild';
|
||||
|
||||
export type { VercelApiHandler, VercelRequest, VercelRequestBody, VercelRequestCookies, VercelRequestQuery, VercelResponse } from '@vercel/node';
|
||||
|
||||
const writeJson = (path: PathLike, data: any) => fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' });
|
||||
|
||||
const ENDPOINT_GLOB = 'api/**/*.{js,ts,tsx}';
|
||||
|
||||
export function vercelFunctions(): AstroIntegration {
|
||||
let _config: AstroConfig;
|
||||
let output: URL;
|
||||
|
||||
return {
|
||||
name: '@astrojs/vercel',
|
||||
hooks: {
|
||||
'astro:config:setup': ({ config }) => {
|
||||
'astro:config:setup': ({ config, ignorePages }) => {
|
||||
output = new URL('./.output/', config.projectRoot);
|
||||
config.dist = new URL('./static/', output);
|
||||
config.buildOptions.pageUrlFormat = 'directory';
|
||||
ignorePages(ENDPOINT_GLOB);
|
||||
},
|
||||
'astro:config:done': async ({ config, setAdapter }) => {
|
||||
// setAdapter(getAdapter(config.buildOptions.site));
|
||||
'astro:config:done': async ({ config }) => {
|
||||
_config = config;
|
||||
},
|
||||
'astro:build:start': async () => {
|
||||
await fs.rm(output, { recursive: true });
|
||||
await fs.rm(output, { recursive: true, force: true });
|
||||
},
|
||||
'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);
|
||||
|
@ -43,6 +50,20 @@ export function vercelFunctions(): AstroIntegration {
|
|||
basePath: '/',
|
||||
pages404: false,
|
||||
});
|
||||
|
||||
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)),
|
||||
bundle: true,
|
||||
target: 'node14',
|
||||
platform: 'node',
|
||||
format: 'cjs',
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue