Skip to content

feat: warn if zip is too large, and log the largest files #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 20 additions & 3 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@
"chalk": "^4.1.2",
"fs-extra": "^10.0.0",
"moize": "^6.1.0",
"node-stream-zip": "^1.15.0",
"outdent": "^0.8.0",
"pathe": "^0.2.0",
"pretty-bytes": "^5.6.0",
"semver": "^7.3.5",
"slash": "^3.0.0",
"tiny-glob": "^0.2.9"
Expand Down
43 changes: 41 additions & 2 deletions src/helpers/verification.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const { existsSync, promises } = require('fs')
const path = require('path')
const { relative } = require('path')

const { yellowBright, greenBright, blueBright } = require('chalk')
const { existsSync } = require('fs-extra')
const { yellowBright, greenBright, blueBright, redBright } = require('chalk')
const { async: StreamZip } = require('node-stream-zip')
const outdent = require('outdent')
const prettyBytes = require('pretty-bytes')
const { satisfies } = require('semver')

exports.verifyBuildTarget = (target) => {
Expand Down Expand Up @@ -53,6 +56,42 @@ exports.checkForRootPublish = ({ publish, failBuild }) => {
}
}

// 50MB, which is the documented max, though the hard max seems to be higher
const LAMBDA_MAX_SIZE = 1024 * 1024 * 50

exports.checkZipSize = async (file) => {
const size = await promises.stat(file).then(({ size }) => size)
if (size < LAMBDA_MAX_SIZE) {
return
}
// We don't fail the build, because the actual hard max size is larger so it might still succeed
console.log(
redBright(outdent`

The function zip ${yellowBright(relative(process.cwd(), file))} size is ${prettyBytes(
size,
)}, which is larger than the maximum supported size of ${prettyBytes(LAMBDA_MAX_SIZE)}.
There are a few reasons this could happen. You may have accidentally bundled a large dependency, or you might have a
large number of pre-rendered pages included.

`),
)
const zip = new StreamZip({ file })
console.log(`Contains ${await zip.entriesCount} files`)
const sortedFiles = Object.values(await zip.entries()).sort((a, b) => b.size - a.size)

const largest = {}
for (let i = 0; i < 10; i++) {
largest[`${i + 1}`] = {
File: sortedFiles[i].name,
'Compressed Size': prettyBytes(sortedFiles[i].compressedSize),
'Uncompressed Size': prettyBytes(sortedFiles[i].size),
}
}
console.log(yellowBright`\n\nThese are the largest files in the zip:`)
console.table(largest)
}

exports.logBetaMessage = () =>
console.log(
greenBright(
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { join, relative } = require('path')

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

const { ODB_FUNCTION_NAME } = require('./constants')
const { restoreCache, saveCache } = require('./helpers/cache')
const { getNextConfig, configureHandlerFunctions, generateRedirects } = require('./helpers/config')
const { generateFunctions, setupImageFunction, generatePagesResolver } = require('./helpers/functions')
Expand All @@ -13,6 +14,7 @@ const {
verifyBuildTarget,
checkForRootPublish,
logBetaMessage,
checkZipSize,
} = require('./helpers/verification')

module.exports = {
Expand Down Expand Up @@ -65,8 +67,9 @@ module.exports = {
})
},

async onPostBuild({ netlifyConfig, utils: { cache } }) {
return saveCache({ cache, publish: netlifyConfig.build.publish })
async onPostBuild({ netlifyConfig, utils: { cache }, constants }) {
await saveCache({ cache, publish: netlifyConfig.build.publish })
await checkZipSize(join(process.cwd(), constants.FUNCTIONS_DIST, `${ODB_FUNCTION_NAME}.zip`))
},
onEnd() {
logBetaMessage()
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const SAMPLE_PROJECT_DIR = `${__dirname}/../demo`
const constants = {
INTERNAL_FUNCTIONS_SRC: '.netlify/internal-functions',
PUBLISH_DIR: '.next',
FUNCTIONS_DIST: '.netlify/functions',
}
const utils = {
build: {
Expand Down