Combine necessary file for edge route in size calculation (#65053)

Closes NEXT-3228
This commit is contained in:
Sebastian Silbermann 2024-04-26 19:57:22 +02:00 committed by GitHub
parent 4e84f696f0
commit 8eb701c19a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 53 additions and 5 deletions

View file

@ -10,6 +10,10 @@ const { parse: urlParse } = require('url')
const benchmarkUrl = require('./benchmark-url')
const { statsAppDir, diffingDir, benchTitle } = require('../constants')
async function defaultGetRequiredFiles(nextAppDir, fileName) {
return [fileName]
}
module.exports = async function collectStats(
runConfig = {},
statsConfig = {},
@ -136,7 +140,11 @@ module.exports = async function collectStats(
}
for (const fileGroup of runConfig.filesToTrack) {
const { name, globs } = fileGroup
const {
getRequiredFiles = defaultGetRequiredFiles,
name,
globs,
} = fileGroup
const groupStats = {}
const curFiles = new Set()
@ -147,11 +155,17 @@ module.exports = async function collectStats(
for (const file of curFiles) {
const fileKey = path.basename(file)
const absPath = path.join(curDir, file)
try {
let parsedSizeSum = 0
let gzipSizeSum = 0
for (const requiredFile of await getRequiredFiles(curDir, file)) {
const absPath = path.join(curDir, requiredFile)
const fileInfo = await fs.stat(absPath)
groupStats[fileKey] = fileInfo.size
groupStats[`${fileKey} gzip`] = await gzipSize.file(absPath)
parsedSizeSum += fileInfo.size
gzipSizeSum += await gzipSize.file(absPath)
}
groupStats[fileKey] = parsedSizeSum
groupStats[`${fileKey} gzip`] = gzipSizeSum
} catch (err) {
logger.error('Failed to get file stats', err)
}

View file

@ -1,3 +1,6 @@
const fs = require('fs/promises')
const path = require('path')
const clientGlobs = [
{
name: 'Client Bundles (main, webpack)',
@ -31,6 +34,37 @@ const clientGlobs = [
'.next/server/pages/edge-ssr.js',
'.next/server/app/app-edge-ssr/page.js',
],
getRequiredFiles: async (nextAppDir, fileName) => {
if (fileName.startsWith('.next/server/app')) {
const manifestJson = await fs.readFile(
path.join(nextAppDir, '.next/server/middleware-manifest.json')
)
const manifest = JSON.parse(manifestJson)
const manifestFileEntry = path.relative(
path.join(nextAppDir, '.next'),
path.join(nextAppDir, fileName)
)
const functionEntry = Object.values(manifest.functions).find(
(entry) => {
return entry.files.includes(manifestFileEntry)
}
)
if (functionEntry === undefined) {
throw new Error(
`${manifestFileEntry} is not listed in the files files of any functions in the manifest:\n` +
JSON.stringify(manifest, null, 2)
)
}
return functionEntry.files.map((file) => {
return path.join('.next', file)
})
} else {
return [fileName]
}
},
},
{
name: 'Middleware size',