Skip to content

Commit 9a1d993

Browse files
committed
feat: warn if zip is too large, and log the largest files
1 parent c7fb6fa commit 9a1d993

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@
5858
"chalk": "^4.1.2",
5959
"fs-extra": "^10.0.0",
6060
"moize": "^6.1.0",
61+
"node-stream-zip": "^1.15.0",
6162
"outdent": "^0.8.0",
6263
"pathe": "^0.2.0",
64+
"pretty-bytes": "^5.6.0",
6365
"semver": "^7.3.5",
6466
"slash": "^3.0.0",
6567
"tiny-glob": "^0.2.9"

src/helpers/verification.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
const { existsSync, promises } = require('fs')
12
const path = require('path')
3+
const { relative } = require('path')
24

3-
const { yellowBright, greenBright, blueBright } = require('chalk')
4-
const { existsSync } = require('fs-extra')
5+
const { yellowBright, greenBright, blueBright, redBright } = require('chalk')
6+
const { async: StreamZip } = require('node-stream-zip')
57
const outdent = require('outdent')
8+
const prettyBytes = require('pretty-bytes')
69
const { satisfies } = require('semver')
710

811
exports.verifyBuildTarget = (target) => {
@@ -53,6 +56,42 @@ exports.checkForRootPublish = ({ publish, failBuild }) => {
5356
}
5457
}
5558

59+
// 50MB, which is the documented max, though the hard max seems to be higher
60+
const LAMBDA_MAX_SIZE = 1024 * 1024 * 50
61+
62+
exports.checkZipSize = async (file) => {
63+
const size = await promises.stat(file).then(({ size }) => size)
64+
if (size < LAMBDA_MAX_SIZE) {
65+
return
66+
}
67+
// We don't fail the build, because the actual hard max size is larger so it might still succeed
68+
console.log(
69+
redBright(outdent`
70+
71+
The function zip ${yellowBright(relative(process.cwd(), file))} size is ${prettyBytes(
72+
size,
73+
)}, which is larger than the maximum supported size of ${prettyBytes(LAMBDA_MAX_SIZE)}.
74+
There are a few reasons this could happen. You may have accidentally bundled a large dependency, or you might have a
75+
large number of pre-rendered pages included.
76+
77+
`),
78+
)
79+
const zip = new StreamZip({ file })
80+
console.log(`Contains ${await zip.entriesCount} files`)
81+
const sortedFiles = Object.values(await zip.entries()).sort((a, b) => b.size - a.size)
82+
83+
const largest = {}
84+
for (let i = 0; i < 10; i++) {
85+
largest[`${i + 1}`] = {
86+
File: sortedFiles[i].name,
87+
'Compressed Size': prettyBytes(sortedFiles[i].compressedSize),
88+
'Uncompressed Size': prettyBytes(sortedFiles[i].size),
89+
}
90+
}
91+
console.log(yellowBright`\n\nThese are the largest files in the zip:`)
92+
console.table(largest)
93+
}
94+
5695
exports.logBetaMessage = () =>
5796
console.log(
5897
greenBright(

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { join, relative } = require('path')
44

55
const { copy, existsSync } = require('fs-extra')
66

7+
const { ODB_FUNCTION_NAME } = require('./constants')
78
const { restoreCache, saveCache } = require('./helpers/cache')
89
const { getNextConfig, configureHandlerFunctions, generateRedirects } = require('./helpers/config')
910
const { generateFunctions, setupImageFunction, generatePagesResolver } = require('./helpers/functions')
@@ -13,6 +14,7 @@ const {
1314
verifyBuildTarget,
1415
checkForRootPublish,
1516
logBetaMessage,
17+
checkZipSize,
1618
} = require('./helpers/verification')
1719

1820
module.exports = {
@@ -65,8 +67,9 @@ module.exports = {
6567
})
6668
},
6769

68-
async onPostBuild({ netlifyConfig, utils: { cache } }) {
69-
return saveCache({ cache, publish: netlifyConfig.build.publish })
70+
async onPostBuild({ netlifyConfig, utils: { cache }, constants }) {
71+
await saveCache({ cache, publish: netlifyConfig.build.publish })
72+
await checkZipSize(join(process.cwd(), constants.FUNCTIONS_DIST, `${ODB_FUNCTION_NAME}.zip`))
7073
},
7174
onEnd() {
7275
logBetaMessage()

0 commit comments

Comments
 (0)