0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-13 22:11:20 -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 15:31:56 -03:00
import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro';
2022-03-28 10:05:55 -03:00
import type { PathLike } from 'fs';
2022-03-28 17:55:03 -03:00
import fs from 'fs/promises';
2022-03-30 23:08:39 -03:00
import esbuild from 'esbuild';
import { fileURLToPath } from 'url';
2022-03-28 10:05:55 -03:00
2022-04-02 18:56:13 -03:00
const writeJson = (path: PathLike, data: any) =>
fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' });
2022-03-28 10:05:55 -03:00
2022-03-30 23:08:39 -03:00
const ENTRYFILE = '__astro_entry';
2022-03-29 23:01:37 -03:00
export function getAdapter(): AstroAdapter {
return {
name: '@astrojs/vercel',
serverEntrypoint: '@astrojs/vercel/server-entrypoint',
exports: ['_default'],
};
}
2022-03-28 15:13:14 -03:00
2022-03-29 23:01:37 -03:00
export default function vercel(): AstroIntegration {
2022-03-31 15:31:56 -03:00
let _config: AstroConfig;
2022-03-28 10:05:55 -03:00
return {
name: '@astrojs/vercel',
hooks: {
2022-03-29 23:01:37 -03:00
'astro:config:setup': ({ config }) => {
config.outDir = new URL('./.output/', config.root);
2022-04-02 18:59:03 -03:00
config.build.format = 'directory';
2022-03-28 10:05:55 -03:00
},
2022-03-31 15:31:56 -03:00
'astro:config:done': ({ setAdapter, config }) => {
2022-03-29 23:01:37 -03:00
setAdapter(getAdapter());
2022-03-31 15:31:56 -03:00
_config = config;
2022-03-28 10:05:55 -03:00
},
2022-03-31 15:31:56 -03:00
'astro:build:start': async ({ buildConfig }) => {
2022-03-30 23:08:39 -03:00
buildConfig.serverEntry = `${ENTRYFILE}.mjs`;
2022-04-02 18:59:03 -03:00
buildConfig.client = new URL('./static/', _config.outDir);
buildConfig.server = new URL('./server/tmp/', _config.outDir);
2022-03-28 10:05:55 -03: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 23:08:39 -03:00
2022-03-31 15:37:33 -03:00
// Convert server entry to CommonJS
2022-03-30 23:08:39 -03:00
await esbuild.build({
entryPoints: [fileURLToPath(new URL(`./${ENTRYFILE}.mjs`, tmpDir))],
outfile: fileURLToPath(new URL(`./${ENTRYFILE}.js`, bundleDir)),
2022-03-30 23:08:39 -03:00
bundle: true,
format: 'cjs',
platform: 'node',
target: 'node14',
2022-03-29 23:01:37 -03:00
});
await fs.rm(tmpDir, { recursive: true });
2022-03-30 23:08:39 -03:00
2022-03-28 10:05:55 -03:00
// Routes Manifest
// https://vercel.com/docs/file-system-api#configuration/routes
await writeJson(new URL(`./routes-manifest.json`, _config.outDir), {
2022-03-28 10:05:55 -03:00
version: 3,
basePath: '/',
pages404: false,
2022-03-29 23:01:37 -03:00
rewrites: routes.map((route) => ({
source: route.pathname,
2022-03-30 23:08:39 -03:00
destination: `/${ENTRYFILE}`,
2022-03-29 23:01:37 -03:00
})),
2022-03-28 10:05:55 -03:00
});
},
},
};
}