0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00
astro/packages/integrations/vercel/src/index.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-03-31 13:31:56 -05:00
import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro';
2022-03-28 08:05:55 -05:00
import type { PathLike } from 'fs';
2022-03-28 15:55:03 -05:00
import fs from 'fs/promises';
2022-03-30 21:08:39 -05:00
import esbuild from 'esbuild';
import { fileURLToPath } from 'url';
2022-03-28 08:05:55 -05:00
2022-04-02 16:56:13 -05:00
const writeJson = (path: PathLike, data: any) =>
fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' });
2022-03-28 08:05:55 -05:00
2022-03-30 21:08:39 -05:00
const ENTRYFILE = '__astro_entry';
2022-03-29 21:01:37 -05:00
export function getAdapter(): AstroAdapter {
return {
name: '@astrojs/vercel',
serverEntrypoint: '@astrojs/vercel/server-entrypoint',
exports: ['default'],
2022-03-29 21:01:37 -05:00
};
}
2022-03-28 13:13:14 -05:00
2022-03-29 21:01:37 -05:00
export default function vercel(): AstroIntegration {
2022-03-31 13:31:56 -05:00
let _config: AstroConfig;
2022-03-28 08:05:55 -05:00
return {
name: '@astrojs/vercel',
hooks: {
2022-03-29 21:01:37 -05:00
'astro:config:setup': ({ config }) => {
config.outDir = new URL('./.output/', config.root);
2022-04-02 16:59:03 -05:00
config.build.format = 'directory';
2022-03-28 08:05:55 -05:00
},
2022-03-31 13:31:56 -05:00
'astro:config:done': ({ setAdapter, config }) => {
2022-03-29 21:01:37 -05:00
setAdapter(getAdapter());
2022-03-31 13:31:56 -05:00
_config = config;
2022-03-28 08:05:55 -05:00
},
2022-03-31 13:31:56 -05:00
'astro:build:start': async ({ buildConfig }) => {
2022-03-30 21:08:39 -05:00
buildConfig.serverEntry = `${ENTRYFILE}.mjs`;
2022-04-02 16:59:03 -05:00
buildConfig.client = new URL('./static/', _config.outDir);
buildConfig.server = new URL('./server/tmp/', _config.outDir);
2022-03-28 08:05:55 -05:00
},
'astro:build:done': async ({ routes }) => {
/*
Why do we need two folders? Why don't we just generate all inside `server/pages/`?
When the app builds, it throws some metadata inside a `chunks/` folder.
./server/
pages/
__astro_entry.mjs
chunks/
(lots of js files)
Those chunks will count as serverless functions (which cost money), so we
need to bundle as much as possible in one file. Hence, the following code
*/
const tmpDir = new URL('./server/tmp/', _config.outDir);
const bundleDir = new URL('./server/pages/', _config.outDir);
await fs.mkdir(bundleDir, { recursive: true });
2022-03-30 21:08:39 -05:00
2022-03-31 13:37:33 -05:00
// Convert server entry to CommonJS
2022-03-30 21:08:39 -05:00
await esbuild.build({
entryPoints: [fileURLToPath(new URL(`./${ENTRYFILE}.mjs`, tmpDir))],
outfile: fileURLToPath(new URL(`./${ENTRYFILE}.js`, bundleDir)),
2022-03-30 21:08:39 -05:00
bundle: true,
format: 'cjs',
platform: 'node',
target: 'node14',
2022-03-29 21:01:37 -05:00
});
await fs.rm(tmpDir, { recursive: true });
2022-03-30 21:08:39 -05:00
2022-03-28 08:05:55 -05:00
// Routes Manifest
// https://vercel.com/docs/file-system-api#configuration/routes
await writeJson(new URL(`./routes-manifest.json`, _config.outDir), {
2022-03-28 08:05:55 -05:00
version: 3,
basePath: '/',
pages404: false,
2022-03-29 21:01:37 -05:00
rewrites: routes.map((route) => ({
source: route.pathname,
2022-03-30 21:08:39 -05:00
destination: `/${ENTRYFILE}`,
2022-03-29 21:01:37 -05:00
})),
2022-03-28 08:05:55 -05:00
});
},
},
};
}