Skip to content

Commit c7952c7

Browse files
committed
Merge branch 'main' into chore/test-eslint
2 parents c2efa3c + b4cb75b commit c7952c7

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

packages/runtime/src/constants.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export const CATCH_ALL_REGEX = /\/\[\.{3}(.*)](.json)?$/
2727
export const OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)]{2}(.json)?$/
2828
export const DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)]/g
2929
export const MINIMUM_REVALIDATE_SECONDS = 60
30-
// 50MB, which is the documented max, though the hard max seems to be higher
31-
export const LAMBDA_MAX_SIZE = 1024 * 1024 * 50
30+
// 50MB, which is the warning max
31+
export const LAMBDA_WARNING_SIZE = 1024 * 1024 * 50
32+
// 250MB, which is the hard max
33+
export const LAMBDA_MAX_SIZE = 1024 * 1024 * 250
3234

3335
export const DIVIDER = `
3436
────────────────────────────────────────────────────────────────

packages/runtime/src/helpers/verification.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { existsSync, promises } from 'fs'
22
import path, { relative, join } from 'path'
33

44
import type { NetlifyConfig, NetlifyPluginUtils } from '@netlify/build'
5-
import { yellowBright, greenBright, blueBright, redBright, reset } from 'chalk'
5+
import { yellowBright, greenBright, blueBright, reset } from 'chalk'
66
import { async as StreamZip } from 'node-stream-zip'
77
import { outdent } from 'outdent'
88
import prettyBytes from 'pretty-bytes'
99
import { satisfies } from 'semver'
1010

11-
import { LAMBDA_MAX_SIZE } from '../constants'
11+
import { LAMBDA_MAX_SIZE, LAMBDA_WARNING_SIZE } from '../constants'
1212

1313
import { isBundleSizeCheckDisabled } from './utils'
1414

@@ -105,7 +105,11 @@ export const checkForRootPublish = ({
105105
}
106106
}
107107

108-
export const checkZipSize = async (file: string, maxSize: number = LAMBDA_MAX_SIZE): Promise<void> => {
108+
export const checkZipSize = async (
109+
file: string,
110+
maxSize: number = LAMBDA_MAX_SIZE,
111+
warningSize: number = LAMBDA_WARNING_SIZE,
112+
): Promise<void> => {
109113
// Requires contacting the Netlify Support team to fully enable.
110114
// Enabling this without contacting them can result in failed deploys.
111115
if (isBundleSizeCheckDisabled()) {
@@ -120,15 +124,16 @@ export const checkZipSize = async (file: string, maxSize: number = LAMBDA_MAX_SI
120124
return
121125
}
122126
const fileSize = await promises.stat(file).then(({ size }) => size)
123-
if (fileSize < maxSize) {
127+
if (fileSize < warningSize) {
124128
return
125129
}
126130
// We don't fail the build, because the actual hard max size is larger so it might still succeed
127131
console.log(
128-
redBright(outdent`
129-
The function zip ${yellowBright(relative(process.cwd(), file))} size is ${prettyBytes(
132+
yellowBright(outdent`
133+
The function zip ${blueBright(relative(process.cwd(), file))} size is ${prettyBytes(
130134
fileSize,
131-
)}, which is larger than the maximum supported size of ${prettyBytes(maxSize)}.
135+
)}, which is larger than the recommended maximum size of ${prettyBytes(warningSize)}.
136+
This will fail the build if the unzipped size is bigger than the maximum size of ${prettyBytes(maxSize)}.
132137
There are a few reasons this could happen. You may have accidentally bundled a large dependency, or you might have a
133138
large number of pre-rendered pages included.
134139
`),

test/helpers/verification.spec.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ jest.mock('fs', () => ({
2727
existsSync: jest.fn(),
2828
}))
2929

30+
// disable chalk colors to easier validate console text output
31+
jest.mock(`chalk`, () => {
32+
process.env.FORCE_COLOR = '0'
33+
return jest.requireActual('chalk')
34+
})
35+
3036
describe('checkNextSiteHasBuilt', () => {
3137
let failBuildMock
3238

@@ -96,23 +102,61 @@ describe('checkNextSiteHasBuilt', () => {
96102
})
97103

98104
describe('checkZipSize', () => {
99-
let consoleSpy
105+
let consoleWarnSpy, consoleLogSpy
106+
const { existsSync, promises } = require('fs')
100107

101108
beforeEach(() => {
102-
consoleSpy = jest.spyOn(global.console, 'warn')
109+
consoleWarnSpy = jest.spyOn(global.console, 'warn')
110+
consoleWarnSpy.mockClear()
111+
consoleLogSpy = jest.spyOn(global.console, 'log')
112+
consoleLogSpy.mockClear()
113+
process.env.DISABLE_BUNDLE_ZIP_SIZE_CHECK = 'false'
103114
})
104115

105116
afterEach(() => {
106117
delete process.env.DISABLE_BUNDLE_ZIP_SIZE_CHECK
107118
})
108119

120+
afterAll(() => {
121+
consoleWarnSpy.mockReset()
122+
consoleLogSpy.mockReset()
123+
existsSync.mockReset()
124+
})
125+
109126
it('emits a warning that DISABLE_BUNDLE_ZIP_SIZE_CHECK was enabled', async () => {
110127
process.env.DISABLE_BUNDLE_ZIP_SIZE_CHECK = 'true'
111128
await checkZipSize(chance.string())
112-
expect(consoleSpy).toHaveBeenCalledWith(
129+
expect(consoleWarnSpy).toHaveBeenCalledWith(
113130
'Function bundle size check was DISABLED with the DISABLE_BUNDLE_ZIP_SIZE_CHECK environment variable. Your deployment will break if it exceeds the maximum supported size of function zip files in your account.',
114131
)
115132
})
133+
134+
it('does not emit a warning if the file size is below the warning size', async () => {
135+
existsSync.mockReturnValue(true)
136+
jest.spyOn(promises, 'stat').mockResolvedValue({ size: 1024 * 1024 * 20 })
137+
138+
await checkZipSize('some-file.zip')
139+
140+
expect(consoleWarnSpy).not.toHaveBeenCalled()
141+
})
142+
143+
it('emits a warning if the file size is above the warning size', async () => {
144+
existsSync.mockReturnValue(true)
145+
jest.spyOn(promises, 'stat').mockResolvedValue({ size: 1024 * 1024 * 200 })
146+
147+
try {
148+
await checkZipSize('some-file.zip')
149+
} catch {
150+
// StreamZip is not mocked, so ultimately the call will throw an error,
151+
// but we are logging message before that so we can assert it
152+
}
153+
154+
expect(consoleLogSpy).toHaveBeenCalledWith(
155+
expect.stringContaining(
156+
'The function zip some-file.zip size is 210 MB, which is larger than the recommended maximum size of 52.4 MB.',
157+
),
158+
)
159+
})
116160
})
117161

118162
describeCwdTmpDir('getProblematicUserRewrites', () => {

0 commit comments

Comments
 (0)