From 3a233c9ea29db89d2a1ef8c6f609eb20283f5c21 Mon Sep 17 00:00:00 2001 From: Arsh Date: Mon, 22 Jan 2024 23:35:50 +0000 Subject: [PATCH] [ci] format --- .../vercel/src/serverless/adapter.ts | 42 +++++++++---------- .../vercel/src/serverless/middleware.ts | 26 ++++++++---- .../vercel/test/edge-middleware.test.js | 11 ++--- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index 4f2a5092ec54..f1c75e4b4d9e 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -179,7 +179,7 @@ export default function vercelServerless({ if (command === 'build' && speedInsights?.enabled) { injectScript('page', 'import "@astrojs/vercel/speed-insights"'); } - + updateConfig({ outDir: new URL('./.vercel/output/', config.root), build: { @@ -211,9 +211,9 @@ export default function vercelServerless({ `\tYou can set functionPerRoute: false to prevent surpassing the limit.\n` ); } - + setAdapter(getAdapter({ functionPerRoute, edgeMiddleware })); - + _config = config; _buildTempFolder = config.build.server; _serverEntry = config.build.serverEntry; @@ -246,12 +246,14 @@ export default function vercelServerless({ } const routeDefinitions: Array<{ - src: string - dest: string - middlewarePath?: string + src: string; + dest: string; + middlewarePath?: string; }> = []; - const includeFiles = _includeFiles.map((file) => new URL(file, _config.root)).concat(extraFilesToInclude); + const includeFiles = _includeFiles + .map((file) => new URL(file, _config.root)) + .concat(extraFilesToInclude); const excludeFiles = _excludeFiles.map((file) => new URL(file, _config.root)); const runtime = getRuntime(process, logger); @@ -328,9 +330,11 @@ export default function vercelServerless({ ? [ { src: '/.*', - dest: fourOhFourRoute.prerender ? '/404.html' - : _middlewareEntryPoint ? MIDDLEWARE_PATH - : NODE_PATH, + dest: fourOhFourRoute.prerender + ? '/404.html' + : _middlewareEntryPoint + ? MIDDLEWARE_PATH + : NODE_PATH, status: 404, }, ] @@ -362,23 +366,19 @@ export default function vercelServerless({ type Runtime = `nodejs${string}.x`; interface CreateMiddlewareFolderArgs { - config: AstroConfig - entry: URL - functionName: string + config: AstroConfig; + entry: URL; + functionName: string; } -async function createMiddlewareFolder({ - functionName, - entry, - config, -}: CreateMiddlewareFolderArgs) { +async function createMiddlewareFolder({ functionName, entry, config }: CreateMiddlewareFolderArgs) { const functionFolder = new URL(`./functions/${functionName}.func/`, config.outDir); await generateEdgeMiddleware( entry, new URL(VERCEL_EDGE_MIDDLEWARE_FILE, config.srcDir), - new URL('./middleware.mjs', functionFolder), - ) + new URL('./middleware.mjs', functionFolder) + ); await writeJson(new URL(`./.vc-config.json`, functionFolder), { runtime: 'edge', @@ -434,7 +434,7 @@ async function createFunctionFolder({ // https://vercel.com/docs/build-output-api/v3#vercel-primitives/serverless-functions/configuration await writeJson(vcConfig, { runtime, - handler: handler.replaceAll("\\","/"), + handler: handler.replaceAll('\\', '/'), launcherType: 'Nodejs', maxDuration, supportsResponseStreaming: true, diff --git a/packages/integrations/vercel/src/serverless/middleware.ts b/packages/integrations/vercel/src/serverless/middleware.ts index 81abaee434ee..648bc0d68f7b 100644 --- a/packages/integrations/vercel/src/serverless/middleware.ts +++ b/packages/integrations/vercel/src/serverless/middleware.ts @@ -17,9 +17,12 @@ import { ASTRO_LOCALS_HEADER, ASTRO_PATH_HEADER, NODE_PATH } from './adapter.js' export async function generateEdgeMiddleware( astroMiddlewareEntryPointPath: URL, vercelEdgeMiddlewareHandlerPath: URL, - outPath: URL, + outPath: URL ): Promise { - const code = edgeMiddlewareTemplate(astroMiddlewareEntryPointPath, vercelEdgeMiddlewareHandlerPath); + const code = edgeMiddlewareTemplate( + astroMiddlewareEntryPointPath, + vercelEdgeMiddlewareHandlerPath + ); // https://vercel.com/docs/concepts/functions/edge-middleware#create-edge-middleware const bundledFilePath = fileURLToPath(outPath); const esbuild = await import('esbuild'); @@ -38,18 +41,23 @@ export async function generateEdgeMiddleware( bundle: true, minify: false, // ensure node built-in modules are namespaced with `node:` - plugins: [{ - name: 'esbuild-namespace-node-built-in-modules', - setup(build) { - const filter = new RegExp(builtinModules.map((mod) => `(^${mod}$)`).join('|')); - build.onResolve({ filter }, (args) => ({ path: 'node:' + args.path, external: true })); + plugins: [ + { + name: 'esbuild-namespace-node-built-in-modules', + setup(build) { + const filter = new RegExp(builtinModules.map((mod) => `(^${mod}$)`).join('|')); + build.onResolve({ filter }, (args) => ({ path: 'node:' + args.path, external: true })); + }, }, - }] + ], }); return pathToFileURL(bundledFilePath); } -function edgeMiddlewareTemplate(astroMiddlewareEntryPointPath: URL, vercelEdgeMiddlewareHandlerPath: URL) { +function edgeMiddlewareTemplate( + astroMiddlewareEntryPointPath: URL, + vercelEdgeMiddlewareHandlerPath: URL +) { const middlewarePath = JSON.stringify( fileURLToPath(astroMiddlewareEntryPointPath).replace(/\\/g, '/') ); diff --git a/packages/integrations/vercel/test/edge-middleware.test.js b/packages/integrations/vercel/test/edge-middleware.test.js index 91146a41c0f8..35b83bb93110 100644 --- a/packages/integrations/vercel/test/edge-middleware.test.js +++ b/packages/integrations/vercel/test/edge-middleware.test.js @@ -17,18 +17,15 @@ describe('Vercel edge middleware', () => { '../.vercel/output/functions/_middleware.func/.vc-config.json' ); expect(JSON.parse(contents)).to.deep.include({ - "runtime": "edge", - "entrypoint": "middleware.mjs" + runtime: 'edge', + entrypoint: 'middleware.mjs', }); }); - it('deployment config points to the middleware edge function', async () => { - const contents = await build.readFile( - '../.vercel/output/config.json' - ); + const contents = await build.readFile('../.vercel/output/config.json'); const { routes } = JSON.parse(contents); - expect(routes.some(route => route.dest === '_middleware')).to.be.true; + expect(routes.some((route) => route.dest === '_middleware')).to.be.true; }); // TODO: The path here seems to be inconsistent?