如何减少vercel无服务器函数的包大小?
我一直在nx monorepo中摆弄next-auth和nextjs app dir,但在使用电子邮件提供商auth部署应用程序时,我遇到了瓶颈。由于某些原因,依赖项的大小会膨胀并超过限制。
// logs from vercel dashboard
Serverless Function's page: api/auth/[...nextauth].js
Large Dependencies Uncompressed size Compressed size
node_modules/@swc/core-linux-x64-gnu 49.75 MB 16.52 MB
node_modules/@swc/core-linux-x64-musl 49.36 MB 16.39 MB
node_modules/.prisma/client 16.25 MB 7.23 MB
node_modules/next/dist 19.67 MB 4.87 MB
node_modules/@esbuild/linux-x64 8.68 MB 3.64 MB
node_modules/webpack/lib 3.31 MB 871.18 KB
node_modules/sass/sass.dart.js 4.27 MB 669.82 KB
node_modules/react-dom/cjs 1.64 MB 404.02 KB
node_modules/caniuse-lite/data 882.5 KB 316.5 KB
dist/apps/manager-dashboard 1.48 MB 253.23 KB
node_modules/terser/dist 957.25 KB 183.23 KB
node_modules/uglify-js/lib 972.25 KB 166.19 KB
node_modules/webpack/schemas 606.81 KB 93.78 KB
All dependencies 161.46 MB 52.47 MB
我尝试使用vercel.json
来排除最大的违规者,但它不起作用。
// apps/my-app/vercel.json
{
"functions": {
"app/api/auth/[...nextauth].js": {
"excludeFiles": "**/node_modules/{@swc,@esbuild}/**"
}
}
}
nextauth中的代码基本上遵循文档:
// apps/my-app/app/api/auth/[...nextauth]/route.ts
import NextAuth from 'next-auth';
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import { PrismaClient } from '@prisma/client';
import EmailProvider from 'next-auth/providers/email';
const emailProvider = EmailProvider({
from: 'no-reply@exanubes.com',
type: 'email',
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
maxAge: 24 * 60 * 60, // 24 hours
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
});
const prisma = new PrismaClient();
const handler = NextAuth({
secret: process.env.NEXTAUTH_SECRET,
session: {
strategy: 'jwt',
},
adapter: PrismaAdapter(prisma),
providers: [emailProvider],
});
export { handler as GET, handler as POST };
1条答案
按热度按时间x0fgdtte1#
我能够通过删除锁定文件并将所有依赖项移动到devDependencies来修复此问题。有趣的是,在vercel部署日志中,它注意到swc linux dep丢失了,但没有它们也编译得很好。