Combine necessary file for edge route in size calculation (#65053)
Closes NEXT-3228
This commit is contained in:
parent
4e84f696f0
commit
8eb701c19a
2 changed files with 53 additions and 5 deletions
|
@ -10,6 +10,10 @@ const { parse: urlParse } = require('url')
|
||||||
const benchmarkUrl = require('./benchmark-url')
|
const benchmarkUrl = require('./benchmark-url')
|
||||||
const { statsAppDir, diffingDir, benchTitle } = require('../constants')
|
const { statsAppDir, diffingDir, benchTitle } = require('../constants')
|
||||||
|
|
||||||
|
async function defaultGetRequiredFiles(nextAppDir, fileName) {
|
||||||
|
return [fileName]
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = async function collectStats(
|
module.exports = async function collectStats(
|
||||||
runConfig = {},
|
runConfig = {},
|
||||||
statsConfig = {},
|
statsConfig = {},
|
||||||
|
@ -136,7 +140,11 @@ module.exports = async function collectStats(
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const fileGroup of runConfig.filesToTrack) {
|
for (const fileGroup of runConfig.filesToTrack) {
|
||||||
const { name, globs } = fileGroup
|
const {
|
||||||
|
getRequiredFiles = defaultGetRequiredFiles,
|
||||||
|
name,
|
||||||
|
globs,
|
||||||
|
} = fileGroup
|
||||||
const groupStats = {}
|
const groupStats = {}
|
||||||
const curFiles = new Set()
|
const curFiles = new Set()
|
||||||
|
|
||||||
|
@ -147,11 +155,17 @@ module.exports = async function collectStats(
|
||||||
|
|
||||||
for (const file of curFiles) {
|
for (const file of curFiles) {
|
||||||
const fileKey = path.basename(file)
|
const fileKey = path.basename(file)
|
||||||
const absPath = path.join(curDir, file)
|
|
||||||
try {
|
try {
|
||||||
const fileInfo = await fs.stat(absPath)
|
let parsedSizeSum = 0
|
||||||
groupStats[fileKey] = fileInfo.size
|
let gzipSizeSum = 0
|
||||||
groupStats[`${fileKey} gzip`] = await gzipSize.file(absPath)
|
for (const requiredFile of await getRequiredFiles(curDir, file)) {
|
||||||
|
const absPath = path.join(curDir, requiredFile)
|
||||||
|
const fileInfo = await fs.stat(absPath)
|
||||||
|
parsedSizeSum += fileInfo.size
|
||||||
|
gzipSizeSum += await gzipSize.file(absPath)
|
||||||
|
}
|
||||||
|
groupStats[fileKey] = parsedSizeSum
|
||||||
|
groupStats[`${fileKey} gzip`] = gzipSizeSum
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error('Failed to get file stats', err)
|
logger.error('Failed to get file stats', err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
const fs = require('fs/promises')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
const clientGlobs = [
|
const clientGlobs = [
|
||||||
{
|
{
|
||||||
name: 'Client Bundles (main, webpack)',
|
name: 'Client Bundles (main, webpack)',
|
||||||
|
@ -31,6 +34,37 @@ const clientGlobs = [
|
||||||
'.next/server/pages/edge-ssr.js',
|
'.next/server/pages/edge-ssr.js',
|
||||||
'.next/server/app/app-edge-ssr/page.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',
|
name: 'Middleware size',
|
||||||
|
|
Loading…
Reference in a new issue