2022-05-11 21:10:38 +00:00
|
|
|
import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro';
|
|
|
|
|
2022-11-14 18:44:37 +00:00
|
|
|
import glob from 'fast-glob';
|
|
|
|
import { pathToFileURL } from 'url';
|
2022-10-10 15:37:03 +00:00
|
|
|
import { getVercelOutput, removeDir, writeJson } from '../lib/fs.js';
|
2022-05-11 21:10:38 +00:00
|
|
|
import { copyDependenciesToFunction } from '../lib/nft.js';
|
|
|
|
import { getRedirects } from '../lib/redirects.js';
|
|
|
|
|
|
|
|
const PACKAGE_NAME = '@astrojs/vercel/serverless';
|
|
|
|
|
|
|
|
function getAdapter(): AstroAdapter {
|
|
|
|
return {
|
|
|
|
name: PACKAGE_NAME,
|
|
|
|
serverEntrypoint: `${PACKAGE_NAME}/entrypoint`,
|
|
|
|
exports: ['default'],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-14 20:19:35 +00:00
|
|
|
export interface VercelServerlessConfig {
|
|
|
|
includeFiles?: string[];
|
|
|
|
excludeFiles?: string[];
|
2023-02-08 16:32:20 +00:00
|
|
|
analytics?: boolean;
|
2022-10-14 20:19:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function vercelServerless({
|
|
|
|
includeFiles,
|
|
|
|
excludeFiles,
|
2023-02-08 16:32:20 +00:00
|
|
|
analytics,
|
2022-10-14 20:19:35 +00:00
|
|
|
}: VercelServerlessConfig = {}): AstroIntegration {
|
2022-05-11 21:10:38 +00:00
|
|
|
let _config: AstroConfig;
|
2022-10-10 15:37:03 +00:00
|
|
|
let buildTempFolder: URL;
|
2022-05-11 21:10:38 +00:00
|
|
|
let functionFolder: URL;
|
|
|
|
let serverEntry: string;
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: PACKAGE_NAME,
|
|
|
|
hooks: {
|
2023-02-08 16:32:20 +00:00
|
|
|
'astro:config:setup': ({ config, updateConfig, injectScript }) => {
|
|
|
|
if (analytics) {
|
|
|
|
injectScript('page', 'import "@astrojs/vercel/analytics"');
|
|
|
|
}
|
2022-10-12 21:25:51 +00:00
|
|
|
const outDir = getVercelOutput(config.root);
|
|
|
|
updateConfig({
|
|
|
|
outDir,
|
|
|
|
build: {
|
|
|
|
serverEntry: 'entry.js',
|
|
|
|
client: new URL('./static/', outDir),
|
|
|
|
server: new URL('./dist/', config.root),
|
2022-10-12 21:27:56 +00:00
|
|
|
},
|
2022-10-12 21:25:51 +00:00
|
|
|
});
|
2022-05-11 21:10:38 +00:00
|
|
|
},
|
|
|
|
'astro:config:done': ({ setAdapter, config }) => {
|
|
|
|
setAdapter(getAdapter());
|
|
|
|
_config = config;
|
2022-10-12 21:25:51 +00:00
|
|
|
buildTempFolder = config.build.server;
|
|
|
|
functionFolder = new URL('./functions/render.func/', config.outDir);
|
|
|
|
serverEntry = config.build.serverEntry;
|
2022-07-27 15:50:48 +00:00
|
|
|
|
2022-07-27 15:52:44 +00:00
|
|
|
if (config.output === 'static') {
|
|
|
|
throw new Error(`
|
2022-07-27 15:50:48 +00:00
|
|
|
[@astrojs/vercel] \`output: "server"\` is required to use the serverless adapter.
|
|
|
|
|
|
|
|
`);
|
|
|
|
}
|
2022-05-11 21:10:38 +00:00
|
|
|
},
|
|
|
|
'astro:build:done': async ({ routes }) => {
|
2022-11-14 18:42:35 +00:00
|
|
|
// Merge any includes from `vite.assetsInclude
|
|
|
|
const inc = includeFiles?.map((file) => new URL(file, _config.root)) || [];
|
2022-11-14 18:44:37 +00:00
|
|
|
if (_config.vite.assetsInclude) {
|
2022-11-14 18:42:35 +00:00
|
|
|
const mergeGlobbedIncludes = (globPattern: unknown) => {
|
2022-11-14 18:44:37 +00:00
|
|
|
if (typeof globPattern === 'string') {
|
|
|
|
const entries = glob.sync(globPattern).map((p) => pathToFileURL(p));
|
2022-11-14 18:42:35 +00:00
|
|
|
inc.push(...entries);
|
2022-11-14 18:44:37 +00:00
|
|
|
} else if (Array.isArray(globPattern)) {
|
|
|
|
for (const pattern of globPattern) {
|
2022-11-14 18:42:35 +00:00
|
|
|
mergeGlobbedIncludes(pattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
mergeGlobbedIncludes(_config.vite.assetsInclude);
|
|
|
|
}
|
|
|
|
|
2022-05-11 21:10:38 +00:00
|
|
|
// Copy necessary files (e.g. node_modules/)
|
2022-10-14 20:19:35 +00:00
|
|
|
const { handler } = await copyDependenciesToFunction({
|
|
|
|
entry: new URL(serverEntry, buildTempFolder),
|
|
|
|
outDir: functionFolder,
|
2022-11-14 18:42:35 +00:00
|
|
|
includeFiles: inc,
|
2022-10-14 20:19:35 +00:00
|
|
|
excludeFiles: excludeFiles?.map((file) => new URL(file, _config.root)) || [],
|
|
|
|
});
|
2022-10-10 15:37:03 +00:00
|
|
|
|
|
|
|
// Remove temporary folder
|
|
|
|
await removeDir(buildTempFolder);
|
2022-05-11 21:10:38 +00:00
|
|
|
|
|
|
|
// Enable ESM
|
|
|
|
// https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/
|
|
|
|
await writeJson(new URL(`./package.json`, functionFolder), {
|
|
|
|
type: 'module',
|
|
|
|
});
|
|
|
|
|
|
|
|
// Serverless function config
|
|
|
|
// https://vercel.com/docs/build-output-api/v3#vercel-primitives/serverless-functions/configuration
|
|
|
|
await writeJson(new URL(`./.vc-config.json`, functionFolder), {
|
2022-05-16 18:34:46 +00:00
|
|
|
runtime: getRuntime(),
|
2022-10-10 15:37:03 +00:00
|
|
|
handler,
|
2022-05-11 21:10:38 +00:00
|
|
|
launcherType: 'Nodejs',
|
|
|
|
});
|
|
|
|
|
|
|
|
// Output configuration
|
|
|
|
// https://vercel.com/docs/build-output-api/v3#build-output-configuration
|
|
|
|
await writeJson(new URL(`./config.json`, _config.outDir), {
|
|
|
|
version: 3,
|
|
|
|
routes: [
|
|
|
|
...getRedirects(routes, _config),
|
|
|
|
{ handle: 'filesystem' },
|
|
|
|
{ src: '/.*', dest: 'render' },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-05-16 18:34:46 +00:00
|
|
|
|
|
|
|
function getRuntime() {
|
|
|
|
const version = process.version.slice(1); // 'v16.5.0' --> '16.5.0'
|
|
|
|
const major = version.split('.')[0]; // '16.5.0' --> '16'
|
|
|
|
return `nodejs${major}.x`;
|
|
|
|
}
|