0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

[ci] format

This commit is contained in:
Arsh 2024-01-22 23:35:50 +00:00 committed by astrobot-houston
parent 05adaaa2d2
commit 3a233c9ea2
3 changed files with 42 additions and 37 deletions

View file

@ -246,12 +246,14 @@ export default function vercelServerless({
} }
const routeDefinitions: Array<{ const routeDefinitions: Array<{
src: string src: string;
dest: string dest: string;
middlewarePath?: 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 excludeFiles = _excludeFiles.map((file) => new URL(file, _config.root));
const runtime = getRuntime(process, logger); const runtime = getRuntime(process, logger);
@ -328,8 +330,10 @@ export default function vercelServerless({
? [ ? [
{ {
src: '/.*', src: '/.*',
dest: fourOhFourRoute.prerender ? '/404.html' dest: fourOhFourRoute.prerender
: _middlewareEntryPoint ? MIDDLEWARE_PATH ? '/404.html'
: _middlewareEntryPoint
? MIDDLEWARE_PATH
: NODE_PATH, : NODE_PATH,
status: 404, status: 404,
}, },
@ -362,23 +366,19 @@ export default function vercelServerless({
type Runtime = `nodejs${string}.x`; type Runtime = `nodejs${string}.x`;
interface CreateMiddlewareFolderArgs { interface CreateMiddlewareFolderArgs {
config: AstroConfig config: AstroConfig;
entry: URL entry: URL;
functionName: string functionName: string;
} }
async function createMiddlewareFolder({ async function createMiddlewareFolder({ functionName, entry, config }: CreateMiddlewareFolderArgs) {
functionName,
entry,
config,
}: CreateMiddlewareFolderArgs) {
const functionFolder = new URL(`./functions/${functionName}.func/`, config.outDir); const functionFolder = new URL(`./functions/${functionName}.func/`, config.outDir);
await generateEdgeMiddleware( await generateEdgeMiddleware(
entry, entry,
new URL(VERCEL_EDGE_MIDDLEWARE_FILE, config.srcDir), 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), { await writeJson(new URL(`./.vc-config.json`, functionFolder), {
runtime: 'edge', runtime: 'edge',
@ -434,7 +434,7 @@ async function createFunctionFolder({
// https://vercel.com/docs/build-output-api/v3#vercel-primitives/serverless-functions/configuration // https://vercel.com/docs/build-output-api/v3#vercel-primitives/serverless-functions/configuration
await writeJson(vcConfig, { await writeJson(vcConfig, {
runtime, runtime,
handler: handler.replaceAll("\\","/"), handler: handler.replaceAll('\\', '/'),
launcherType: 'Nodejs', launcherType: 'Nodejs',
maxDuration, maxDuration,
supportsResponseStreaming: true, supportsResponseStreaming: true,

View file

@ -17,9 +17,12 @@ import { ASTRO_LOCALS_HEADER, ASTRO_PATH_HEADER, NODE_PATH } from './adapter.js'
export async function generateEdgeMiddleware( export async function generateEdgeMiddleware(
astroMiddlewareEntryPointPath: URL, astroMiddlewareEntryPointPath: URL,
vercelEdgeMiddlewareHandlerPath: URL, vercelEdgeMiddlewareHandlerPath: URL,
outPath: URL, outPath: URL
): Promise<URL> { ): Promise<URL> {
const code = edgeMiddlewareTemplate(astroMiddlewareEntryPointPath, vercelEdgeMiddlewareHandlerPath); const code = edgeMiddlewareTemplate(
astroMiddlewareEntryPointPath,
vercelEdgeMiddlewareHandlerPath
);
// https://vercel.com/docs/concepts/functions/edge-middleware#create-edge-middleware // https://vercel.com/docs/concepts/functions/edge-middleware#create-edge-middleware
const bundledFilePath = fileURLToPath(outPath); const bundledFilePath = fileURLToPath(outPath);
const esbuild = await import('esbuild'); const esbuild = await import('esbuild');
@ -38,18 +41,23 @@ export async function generateEdgeMiddleware(
bundle: true, bundle: true,
minify: false, minify: false,
// ensure node built-in modules are namespaced with `node:` // ensure node built-in modules are namespaced with `node:`
plugins: [{ plugins: [
{
name: 'esbuild-namespace-node-built-in-modules', name: 'esbuild-namespace-node-built-in-modules',
setup(build) { setup(build) {
const filter = new RegExp(builtinModules.map((mod) => `(^${mod}$)`).join('|')); const filter = new RegExp(builtinModules.map((mod) => `(^${mod}$)`).join('|'));
build.onResolve({ filter }, (args) => ({ path: 'node:' + args.path, external: true })); build.onResolve({ filter }, (args) => ({ path: 'node:' + args.path, external: true }));
}, },
}] },
],
}); });
return pathToFileURL(bundledFilePath); return pathToFileURL(bundledFilePath);
} }
function edgeMiddlewareTemplate(astroMiddlewareEntryPointPath: URL, vercelEdgeMiddlewareHandlerPath: URL) { function edgeMiddlewareTemplate(
astroMiddlewareEntryPointPath: URL,
vercelEdgeMiddlewareHandlerPath: URL
) {
const middlewarePath = JSON.stringify( const middlewarePath = JSON.stringify(
fileURLToPath(astroMiddlewareEntryPointPath).replace(/\\/g, '/') fileURLToPath(astroMiddlewareEntryPointPath).replace(/\\/g, '/')
); );

View file

@ -17,18 +17,15 @@ describe('Vercel edge middleware', () => {
'../.vercel/output/functions/_middleware.func/.vc-config.json' '../.vercel/output/functions/_middleware.func/.vc-config.json'
); );
expect(JSON.parse(contents)).to.deep.include({ expect(JSON.parse(contents)).to.deep.include({
"runtime": "edge", runtime: 'edge',
"entrypoint": "middleware.mjs" entrypoint: 'middleware.mjs',
}); });
}); });
it('deployment config points to the middleware edge function', async () => { it('deployment config points to the middleware edge function', async () => {
const contents = await build.readFile( const contents = await build.readFile('../.vercel/output/config.json');
'../.vercel/output/config.json'
);
const { routes } = JSON.parse(contents); 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? // TODO: The path here seems to be inconsistent?