Skip to content

Commit ec33d6f

Browse files
committed
fix: wip - report bundle size on error
1 parent 4aaeb79 commit ec33d6f

File tree

4 files changed

+112
-6
lines changed

4 files changed

+112
-6
lines changed

package-lock.json

Lines changed: 59 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,20 @@
5757
"@netlify/build": "^35.1.7",
5858
"@netlify/config": "^24.0.4",
5959
"@netlify/edge-bundler": "^14.5.5",
60-
"@netlify/edge-functions-bootstrap": "^2.14.0",
6160
"@netlify/edge-functions": "^2.17.1",
61+
"@netlify/edge-functions-bootstrap": "^2.14.0",
6262
"@netlify/eslint-config-node": "^7.0.1",
6363
"@netlify/functions": "^4.2.7",
6464
"@netlify/serverless-functions-api": "^2.5.0",
6565
"@netlify/zip-it-and-ship-it": "^14.1.8",
6666
"@opentelemetry/api": "^1.8.0",
6767
"@playwright/test": "^1.43.1",
68+
"@types/adm-zip": "^0.5.7",
6869
"@types/node": "^20.12.7",
6970
"@types/picomatch": "^3.0.0",
7071
"@types/uuid": "^10.0.0",
7172
"@vercel/nft": "^0.30.0",
73+
"adm-zip": "^0.5.16",
7274
"cheerio": "^1.0.0-rc.12",
7375
"clean-package": "^2.2.0",
7476
"esbuild": "^0.25.0",
@@ -90,6 +92,7 @@
9092
"path-to-regexp": "^6.2.1",
9193
"picomatch": "^4.0.2",
9294
"prettier": "^3.2.5",
95+
"pretty-bytes": "^7.1.0",
9396
"semver": "^7.6.0",
9497
"typescript": "^5.4.5",
9598
"unionfs": "^4.5.4",

src/build/functions/utils.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { stat } from 'node:fs/promises'
2+
import { join as posixJoin, sep as posixSep } from 'node:path/posix'
3+
4+
import { trace } from '@opentelemetry/api'
5+
import { wrapTracer } from '@opentelemetry/api/experimental'
6+
// eslint-disable-next-line import/no-extraneous-dependencies
7+
import AdmZip from 'adm-zip'
8+
// eslint-disable-next-line import/no-extraneous-dependencies
9+
import prettyBytes from 'pretty-bytes'
10+
11+
import { PluginContext, SERVER_HANDLER_NAME } from '../plugin-context.js'
12+
13+
const tracer = wrapTracer(trace.getTracer('Next runtime'))
14+
15+
/** Copies the runtime dist folder to the lambda */
16+
export const checkBundleSize = async (ctx: PluginContext) => {
17+
const LAMBDA_MAX_SIZE = 1024 * 1024 * 250 // 250MB
18+
const TOP_N_ENTRIES = 5
19+
const LEVELS = 3
20+
21+
await tracer.withActiveSpan('checkBundleSize', async () => {
22+
const bundleFileName: string = posixJoin(
23+
ctx.constants.FUNCTIONS_DIST,
24+
`${SERVER_HANDLER_NAME}.zip`,
25+
)
26+
const bundleSize = await stat(bundleFileName).then(({ size }) => size)
27+
if (bundleSize < LAMBDA_MAX_SIZE) {
28+
return
29+
}
30+
31+
const zip = new AdmZip(bundleFileName)
32+
const bundleContentSizes: Record<string, number> = {}
33+
for (const entry of zip.getEntries()) {
34+
const entryName = entry.entryName.split(posixSep).slice(0, LEVELS).join(posixSep)
35+
bundleContentSizes[entryName] = (bundleContentSizes[entryName] || 0) + entry.header.size
36+
}
37+
38+
// eslint-disable-next-line id-length
39+
const sortedBundleContentSizes = Object.entries(bundleContentSizes).sort((a, b) => b[1] - a[1])
40+
for (const [dir, size] of sortedBundleContentSizes.slice(0, TOP_N_ENTRIES)) {
41+
console.log(`${prettyBytes(size)} \t${dir}`)
42+
}
43+
})
44+
}

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from './build/content/static.js'
1717
import { clearStaleEdgeHandlers, createEdgeHandlers } from './build/functions/edge.js'
1818
import { clearStaleServerHandlers, createServerHandler } from './build/functions/server.js'
19+
import { checkBundleSize } from './build/functions/utils.js'
1920
import { setImageConfig } from './build/image-cdn.js'
2021
import { PluginContext } from './build/plugin-context.js'
2122
import {
@@ -110,7 +111,10 @@ export const onPostBuild = async (options: NetlifyPluginOptions) => {
110111
}
111112

112113
await tracer.withActiveSpan('onPostBuild', async () => {
113-
await publishStaticDir(new PluginContext(options))
114+
const ctx = new PluginContext(options)
115+
116+
await publishStaticDir(ctx)
117+
await checkBundleSize(ctx)
114118
})
115119
}
116120

0 commit comments

Comments
 (0)