Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@
"@netlify/build": "^35.1.7",
"@netlify/config": "^24.0.4",
"@netlify/edge-bundler": "^14.5.5",
"@netlify/edge-functions-bootstrap": "^2.14.0",
"@netlify/edge-functions": "^2.17.1",
"@netlify/edge-functions-bootstrap": "^2.14.0",
"@netlify/eslint-config-node": "^7.0.1",
"@netlify/functions": "^4.2.7",
"@netlify/serverless-functions-api": "^2.5.0",
"@netlify/zip-it-and-ship-it": "^14.1.8",
"@opentelemetry/api": "^1.8.0",
"@playwright/test": "^1.43.1",
"@types/adm-zip": "^0.5.7",
"@types/node": "^20.12.7",
"@types/picomatch": "^3.0.0",
"@types/uuid": "^10.0.0",
"@vercel/nft": "^0.30.0",
"adm-zip": "^0.5.16",
"cheerio": "^1.0.0-rc.12",
"clean-package": "^2.2.0",
"esbuild": "^0.25.0",
Expand All @@ -90,6 +92,7 @@
"path-to-regexp": "^6.2.1",
"picomatch": "^4.0.2",
"prettier": "^3.2.5",
"pretty-bytes": "^7.1.0",
"semver": "^7.6.0",
"typescript": "^5.4.5",
"unionfs": "^4.5.4",
Expand Down
44 changes: 44 additions & 0 deletions src/build/functions/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { stat } from 'node:fs/promises'
import { join as posixJoin, sep as posixSep } from 'node:path/posix'

import { trace } from '@opentelemetry/api'
import { wrapTracer } from '@opentelemetry/api/experimental'
// eslint-disable-next-line import/no-extraneous-dependencies
import AdmZip from 'adm-zip'
// eslint-disable-next-line import/no-extraneous-dependencies
import prettyBytes from 'pretty-bytes'

import { PluginContext, SERVER_HANDLER_NAME } from '../plugin-context.js'

const tracer = wrapTracer(trace.getTracer('Next runtime'))

/** Copies the runtime dist folder to the lambda */
export const checkBundleSize = async (ctx: PluginContext) => {
const LAMBDA_MAX_SIZE = 1024 * 1024 * 250 // 250MB
const TOP_N_ENTRIES = 5
const LEVELS = 3

await tracer.withActiveSpan('checkBundleSize', async () => {
const bundleFileName: string = posixJoin(
ctx.constants.FUNCTIONS_DIST,
`${SERVER_HANDLER_NAME}.zip`,
)
const bundleSize = await stat(bundleFileName).then(({ size }) => size)
if (bundleSize < LAMBDA_MAX_SIZE) {
return
}

const zip = new AdmZip(bundleFileName)
const bundleContentSizes: Record<string, number> = {}
for (const entry of zip.getEntries()) {
const entryName = entry.entryName.split(posixSep).slice(0, LEVELS).join(posixSep)
bundleContentSizes[entryName] = (bundleContentSizes[entryName] || 0) + entry.header.size
}

// eslint-disable-next-line id-length
const sortedBundleContentSizes = Object.entries(bundleContentSizes).sort((a, b) => b[1] - a[1])
for (const [dir, size] of sortedBundleContentSizes.slice(0, TOP_N_ENTRIES)) {
console.log(`${prettyBytes(size)} \t${dir}`)
}
})
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from './build/content/static.js'
import { clearStaleEdgeHandlers, createEdgeHandlers } from './build/functions/edge.js'
import { clearStaleServerHandlers, createServerHandler } from './build/functions/server.js'
import { checkBundleSize } from './build/functions/utils.js'
import { setImageConfig } from './build/image-cdn.js'
import { PluginContext } from './build/plugin-context.js'
import {
Expand Down Expand Up @@ -110,7 +111,10 @@ export const onPostBuild = async (options: NetlifyPluginOptions) => {
}

await tracer.withActiveSpan('onPostBuild', async () => {
await publishStaticDir(new PluginContext(options))
const ctx = new PluginContext(options)

await publishStaticDir(ctx)
await checkBundleSize(ctx)
})
}

Expand Down
Loading