Skip to content

Commit

Permalink
chore: move vercel adapter to code
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Feb 6, 2025
1 parent 3f3c487 commit 64b118a
Show file tree
Hide file tree
Showing 35 changed files with 302 additions and 69 deletions.
7 changes: 3 additions & 4 deletions packages/integrations/vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
},
"files": ["dist", "types.d.ts"],
"scripts": {
"build": "tsc",
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"test": "astro-scripts test --timeout 50000 \"test/**/!(hosted).test.js\"",
"test:hosted": "astro-scripts test --timeout 30000 \"test/hosted/*.test.js\""
},
"dependencies": {
"@astrojs/internal-helpers": "^0.4.2",
"@astrojs/internal-helpers": "^0.5.1",
"@vercel/analytics": "^1.4.1",
"@vercel/edge": "^1.2.1",
"@vercel/nft": "^0.29.0",
Expand All @@ -48,8 +48,7 @@
"astro": "^5.0.0"
},
"devDependencies": {
"@astrojs/test-utils": "workspace:*",
"astro": "^5.1.6",
"astro": "workspace:*",
"astro-scripts": "workspace:*",
"cheerio": "1.0.0"
},
Expand Down
1 change: 0 additions & 1 deletion packages/integrations/vercel/src/image/build-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const service: ExternalImageService = {
options.width && searchParams.append('w', options.width.toString());
options.quality && searchParams.append('q', options.quality.toString());

// biome-ignore lint/style/useTemplate: <explanation>
return '/_vercel/image?' + searchParams;
},
};
Expand Down
3 changes: 0 additions & 3 deletions packages/integrations/vercel/src/image/shared-dev-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const baseDevService: Omit<LocalImageService, 'transform'> = {
options.width && searchParams.append('w', options.width.toString());
options.quality && searchParams.append('q', options.quality.toString());

// biome-ignore lint/style/useTemplate: <explanation>
return '/_image?' + searchParams;
},
parseURL(url) {
Expand All @@ -26,9 +25,7 @@ export const baseDevService: Omit<LocalImageService, 'transform'> = {
}

const transform = {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
src: params.get('href')!,
// biome-ignore lint/style/noNonNullAssertion: <explanation>
width: params.has('w') ? Number.parseInt(params.get('w')!) : undefined,
quality: params.get('q'),
};
Expand Down
1 change: 0 additions & 1 deletion packages/integrations/vercel/src/image/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export function sharedValidateOptions(
} else {
if (!configuredWidths.includes(options.width)) {
const nearestWidth = configuredWidths.reduce((prev, curr) => {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
return Math.abs(curr - options.width!) < Math.abs(prev - options.width!) ? curr : prev;
});

Expand Down
17 changes: 3 additions & 14 deletions packages/integrations/vercel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,9 @@ export default function vercelAdapter({
}: VercelServerlessConfig = {}): AstroIntegration {
if (maxDuration) {
if (typeof maxDuration !== 'number') {
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
throw new TypeError(`maxDuration must be a number`, { cause: maxDuration });
}
if (maxDuration <= 0) {
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
throw new TypeError(`maxDuration must be a positive number`, { cause: maxDuration });
}
}
Expand Down Expand Up @@ -223,9 +221,9 @@ export default function vercelAdapter({
hooks: {
'astro:build:done': async () => {
logger.info('Copying static files to .vercel/output/static');
const staticDir =
const _staticDir =
_buildOutput === 'static' ? _config.outDir : _config.build.client;
cpSync(staticDir, new URL('./.vercel/output/static/', _config.root), {
cpSync(_staticDir, new URL('./.vercel/output/static/', _config.root), {
recursive: true,
});
},
Expand Down Expand Up @@ -258,7 +256,6 @@ export default function vercelAdapter({
`maxDuration is set to ${maxDuration} seconds, which is longer than the maximum allowed duration of 900 seconds.`
);
logger.warn(
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`Please make sure that your plan allows for this duration. See https://vercel.com/docs/functions/serverless-functions/runtimes#maxduration for more information.`
);
}
Expand Down Expand Up @@ -574,7 +571,7 @@ class VercelBuilder {
) {}

async buildServerlessFolder(entry: URL, functionName: string, root: URL) {
const { config, includeFiles, excludeFiles, logger, NTF_CACHE, runtime, maxDuration } = this;
const { includeFiles, excludeFiles, logger, NTF_CACHE, runtime, maxDuration } = this;
// .vercel/output/functions/<name>.func/
const functionFolder = new URL(`./functions/${functionName}.func/`, this.outDir);
const packageJson = new URL(`./functions/${functionName}.func/package.json`, this.outDir);
Expand Down Expand Up @@ -635,7 +632,6 @@ class VercelBuilder {
this.logger
);

// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
await writeJson(new URL(`./.vc-config.json`, functionFolder), {
runtime: 'edge',
entrypoint: 'middleware.mjs',
Expand All @@ -649,13 +645,9 @@ function getRuntime(process: NodeJS.Process, logger: AstroIntegrationLogger): Ru
const support = SUPPORTED_NODE_VERSIONS[major];
if (support === undefined) {
logger.warn(
// biome-ignore lint/style/useTemplate: <explanation>
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\n` +
`\tThe local Node.js version (${major}) is not supported by Vercel Serverless Functions.\n` +
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\tYour project will use Node.js 18 as the runtime instead.\n` +
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\tConsider switching your local version to 18.\n`
);
return 'nodejs18.x';
Expand All @@ -682,12 +674,9 @@ function getRuntime(process: NodeJS.Process, logger: AstroIntegrationLogger): Ru
support.removal
);
logger.warn(
// biome-ignore lint/style/useTemplate: <explanation>
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\n` +
`\tYour project is being built for Node.js ${major} as the runtime.\n` +
`\tThis version is deprecated by Vercel Serverless Functions, and scheduled to be disabled on ${removeDate}.\n` +
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\tConsider upgrading your local version to 18.\n`
);
return `nodejs${major}.x`;
Expand Down
2 changes: 0 additions & 2 deletions packages/integrations/vercel/src/lib/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export async function copyDependenciesToFunction(

for (const error of result.warnings) {
if (error.message.startsWith('Failed to resolve dependency')) {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
const [, module, file] = /Cannot find module '(.+?)' loaded from (.+)/.exec(error.message)!;

// The import(astroRemark) sometimes fails to resolve, but it's not a problem
Expand All @@ -65,7 +64,6 @@ export async function copyDependenciesToFunction(
// such as this html file in "main" meant for nw instead of node:
// https://github.com/vercel/nft/issues/311
else if (error.message.startsWith('Failed to parse')) {
// biome-ignore lint/correctness/noUnnecessaryContinue: <explanation>
continue;
} else {
throw error;
Expand Down
10 changes: 0 additions & 10 deletions packages/integrations/vercel/src/lib/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ import type { Redirect } from '@vercel/routing-utils';

const pathJoin = nodePath.posix.join;

// https://vercel.com/docs/project-configuration#legacy/routes
interface VercelRoute {
src: string;
methods?: string[];
dest?: string;
headers?: Record<string, string>;
status?: number;
continue?: boolean;
}

// Copied from astro/packages/astro/src/core/routing/manifest/create.ts
// Disable eslint as we're not sure how to improve this regex yet
// eslint-disable-next-line regexp/no-super-linear-backtracking
Expand Down
1 change: 0 additions & 1 deletion packages/integrations/vercel/src/lib/web-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export async function getInjectableWebAnalyticsContent({
}: {
mode: 'development' | 'production';
}) {
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
const base = `window.va = window.va || function () { (window.vaq = window.vaq || []).push(arguments); };`;

if (mode === 'development') {
Expand Down
9 changes: 5 additions & 4 deletions packages/integrations/vercel/src/serverless/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import {
*
* Then this file gets bundled with esbuild. The bundle phase will inline the Astro middleware code.
*
* @param astroMiddlewareEntryPoint
* @param astroMiddlewareEntryPointPath
* @param root
* @param vercelEdgeMiddlewareHandlerPath
* @param outPath
* @param middlewareSecret
* @param logger
* @returns {Promise<URL>} The path to the bundled file
*/
export async function generateEdgeMiddleware(
Expand Down Expand Up @@ -61,7 +65,6 @@ export async function generateEdgeMiddleware(
name: 'esbuild-namespace-node-built-in-modules',
setup(build) {
const filter = new RegExp(builtinModules.map((mod) => `(^${mod}$)`).join('|'));
// biome-ignore lint/style/useTemplate: <explanation>
build.onResolve({ filter }, (args) => ({ path: 'node:' + args.path, external: true }));
},
},
Expand All @@ -82,14 +85,12 @@ function edgeMiddlewareTemplate(
const filePathEdgeMiddleware = fileURLToPath(vercelEdgeMiddlewareHandlerPath);
let handlerTemplateImport = '';
let handlerTemplateCall = '{}';
// biome-ignore lint/style/useTemplate: <explanation>
if (existsSync(filePathEdgeMiddleware + '.js') || existsSync(filePathEdgeMiddleware + '.ts')) {
logger.warn(
'Usage of `vercel-edge-middleware.js` is deprecated. You can now use the `waitUntil(promise)` function directly as `ctx.locals.waitUntil(promise)`.'
);
const stringified = JSON.stringify(filePathEdgeMiddleware.replace(/\\/g, '/'));
handlerTemplateImport = `import handler from ${stringified}`;
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
handlerTemplateCall = `await handler({ request, context })`;
} else {
}
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/vercel/test/edge-middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';

describe('Vercel edge middleware', () => {
/** @type {import('../../../astro/test/test-utils.js').Fixture} */
/** @type {import('./test-utils.js').Fixture} */
let build;
before(async () => {
build = await loadFixture({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
},
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"dependencies": {
"@astrojs/sitemap": "^3.2.1",
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "^5.1.6"
"astro": "workspace:*"
}
}
2 changes: 1 addition & 1 deletion packages/integrations/vercel/test/image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Image', () => {
/** @type {import('../../../astro/test/test-utils.js').Fixture} */
/** @type {import('./test-utils.js').Fixture} */
let fixture;

before(async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';

describe('Assets generated by integrations', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/vercel/test/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';

describe('Redirects', () => {
/** @type {import('../../../astro/test/test-utils.js').Fixture} */
/** @type {import('./test-utils.js').Fixture} */
let fixture;

before(async () => {
Expand Down
Loading

0 comments on commit 64b118a

Please sign in to comment.