Commit deeaa923 authored by Janicklas Ralph's avatar Janicklas Ralph Committed by JJ Kasper

Update page sizes displayed on build (#8777)

* Fixing page sizes displayed during build

* Adding comments

* Update variable name

* Fixed support for modern mode

* Remove braces

* Memoize fs.stat result

* Memoize fs.stat result
parent b3d1b776
...@@ -3,6 +3,7 @@ import fs from 'fs' ...@@ -3,6 +3,7 @@ import fs from 'fs'
import mkdirpOrig from 'mkdirp' import mkdirpOrig from 'mkdirp'
import { import {
PAGES_MANIFEST, PAGES_MANIFEST,
BUILD_MANIFEST,
PHASE_PRODUCTION_BUILD, PHASE_PRODUCTION_BUILD,
PRERENDER_MANIFEST, PRERENDER_MANIFEST,
SERVER_DIRECTORY, SERVER_DIRECTORY,
...@@ -262,6 +263,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -262,6 +263,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY, isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
PAGES_MANIFEST PAGES_MANIFEST
) )
const buildManifestPath = path.join(distDir, BUILD_MANIFEST)
const sprPages = new Set<string>() const sprPages = new Set<string>()
const staticPages = new Set<string>() const staticPages = new Set<string>()
...@@ -269,6 +271,8 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -269,6 +271,8 @@ export default async function build(dir: string, conf = null): Promise<void> {
const hybridAmpPages = new Set<string>() const hybridAmpPages = new Set<string>()
const pageInfos = new Map<string, PageInfo>() const pageInfos = new Map<string, PageInfo>()
const pagesManifest = JSON.parse(await fsReadFile(manifestPath, 'utf8')) const pagesManifest = JSON.parse(await fsReadFile(manifestPath, 'utf8'))
const buildManifest = JSON.parse(await fsReadFile(buildManifestPath, 'utf8'))
let customAppGetInitialProps: boolean | undefined let customAppGetInitialProps: boolean | undefined
process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD
...@@ -284,7 +288,13 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -284,7 +288,13 @@ export default async function build(dir: string, conf = null): Promise<void> {
const chunks = getPageChunks(page) const chunks = getPageChunks(page)
const actualPage = page === '/' ? '/index' : page const actualPage = page === '/' ? '/index' : page
const size = await getPageSizeInKb(actualPage, distDir, buildId) const size = await getPageSizeInKb(
actualPage,
distDir,
buildId,
buildManifest,
config.experimental.modern
)
const bundleRelative = path.join( const bundleRelative = path.join(
isLikeServerless ? 'pages' : `static/${buildId}/pages`, isLikeServerless ? 'pages' : `static/${buildId}/pages`,
actualPage + '.js' actualPage + '.js'
......
...@@ -10,7 +10,15 @@ import prettyBytes from '../lib/pretty-bytes' ...@@ -10,7 +10,15 @@ import prettyBytes from '../lib/pretty-bytes'
import { recursiveReadDir } from '../lib/recursive-readdir' import { recursiveReadDir } from '../lib/recursive-readdir'
import { getPageChunks } from './webpack/plugins/chunk-graph-plugin' import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
const fsStat = promisify(fs.stat) const fsStatPromise = promisify(fs.stat)
const fileStats: { [k: string]: Promise<fs.Stats> } = {}
const fsStat = (file: string) => {
if (fileStats[file]) return fileStats[file]
fileStats[file] = fsStatPromise(file)
return fileStats[file]
}
export function collectPages( export function collectPages(
directory: string, directory: string,
...@@ -132,15 +140,34 @@ export function printTreeView( ...@@ -132,15 +140,34 @@ export function printTreeView(
export async function getPageSizeInKb( export async function getPageSizeInKb(
page: string, page: string,
distPath: string, distPath: string,
buildId: string buildId: string,
buildManifest: { pages: { [k: string]: string[] } },
isModern: boolean
): Promise<number> { ): Promise<number> {
const clientBundle = path.join( const clientBundle = path.join(
distPath, distPath,
`static/${buildId}/pages/`, `static/${buildId}/pages/`,
`${page}.js` `${page}${isModern ? '.module' : ''}.js`
) )
// With granularChunks flag enabled, each page may have additional chunks that it depends on
const baseDeps = page === '/_app' ? [] : buildManifest.pages['/_app']
// Get the list of chunks specific to this page
// With granularChunks: false, this will be []
const deps = (buildManifest.pages[page] || [])
.filter(
dep => !baseDeps.includes(dep) && /\.module\.js$/.test(dep) === isModern
)
.map(dep => `${distPath}/${dep}`)
// Add the main bundle for the page
deps.push(clientBundle)
try { try {
return (await fsStat(clientBundle)).size let depStats = await Promise.all(deps.map(fsStat))
return depStats.reduce((size, stat) => size + stat.size, 0)
} catch (_) {} } catch (_) {}
return -1 return -1
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment