Unverified Commit 5ea721ea authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Redesigned page output (#7324)

* Redesigned page output

* Remove old commented out code

* right align files
parent 0770470b
...@@ -19,9 +19,10 @@ import { ...@@ -19,9 +19,10 @@ import {
collectPages, collectPages,
getCacheIdentifier, getCacheIdentifier,
getFileForPage, getFileForPage,
getPageInfo, getPageSizeInKb,
getSpecifiedPages, getSpecifiedPages,
printTreeView, printTreeView,
PageInfo,
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { import {
...@@ -238,43 +239,28 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -238,43 +239,28 @@ export default async function build(dir: string, conf = null): Promise<void> {
console.log(chalk.green('Compiled successfully.\n')) console.log(chalk.green('Compiled successfully.\n'))
} }
const pageInfos = new Map() const pageInfos = new Map<string, PageInfo>()
const distPath = path.join(dir, config.distDir) const distPath = path.join(dir, config.distDir)
let pageKeys = Object.keys(mappedPages) const pageKeys = Object.keys(mappedPages)
for (const page of pageKeys) { for (const page of pageKeys) {
const chunks = getPageChunks(page) const chunks = getPageChunks(page)
const actualPage = page === '/' ? 'index' : page
const info = await getPageInfo(
actualPage,
distPath,
buildId,
false,
config.target === 'serverless'
)
pageInfos.set(page, {
...(info || {}),
chunks,
})
if (!(typeof info.serverSize === 'number')) { const actualPage = page === '/' ? '/index' : page
pageKeys = pageKeys.filter(pg => pg !== page) const size = await getPageSizeInKb(actualPage, distPath, buildId)
} pageInfos.set(page, { size, chunks })
} }
if (Array.isArray(configs[0].plugins)) { if (Array.isArray(configs[0].plugins)) {
configs[0].plugins.some((plugin: any) => { configs[0].plugins.some((plugin: any) => {
if (plugin.ampPages) { if (!plugin.ampPages) {
plugin.ampPages.forEach((pg: any) => { return false
const info = pageInfos.get(pg)
if (info) {
info.ampOnly = true
pageInfos.set(pg, info)
}
})
} }
return Boolean(plugin.ampPages)
plugin.ampPages.forEach((pg: any) => {
pageInfos.get(pg)!.isAmp = true
})
return true
}) })
} }
......
import chalk from 'chalk'
import crypto from 'crypto' import crypto from 'crypto'
import findUp from 'find-up' import findUp from 'find-up'
import fs from 'fs' import fs from 'fs'
import textTable from 'next/dist/compiled/text-table'
import path from 'path' import path from 'path'
import stripAnsi from 'strip-ansi'
import { promisify } from 'util' import { promisify } from 'util'
import prettyBytes from '../lib/pretty-bytes' 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'
const fsStat = promisify(fs.stat) const fsStat = promisify(fs.stat)
const fsExists = promisify(fs.exists) const fsExists = promisify(fs.exists)
...@@ -20,78 +25,70 @@ export function collectPages( ...@@ -20,78 +25,70 @@ export function collectPages(
) )
} }
interface ChunksItem { export interface PageInfo {
external: Set<string>, isAmp?: boolean
internal: Set<string> size: number
} chunks?: ReturnType<typeof getPageChunks>
interface PageInfo {
ampOnly: boolean,
serverSize: number,
clientSize: number,
chunks: ChunksItem
} }
export function printTreeView(list: string[], pageInfos: Map<string, PageInfo>) { export function printTreeView(
/* list: string[],
red for >250kb pageInfos: Map<string, PageInfo>
yellow for >100kb ) {
green for <100kb const getPrettySize = (_size: number): string => {
*/ const size = prettyBytes(_size)
const getSizeColor = (size: number): string => { // green for 0-100kb
if (size < 100 * 1000) return '\x1b[32m' if (_size < 100 * 1000) return chalk.green(size)
if (size < 250 * 1000) return '\x1b[33m' // yellow for 100-250kb
return '\x1b[31m' if (_size < 250 * 1000) return chalk.yellow(size)
// red for >= 250kb
return chalk.red.bold(size)
} }
const messages: string[][] = [
['Page', 'Size', 'Files', 'Packages'].map(entry => chalk.underline(entry)),
]
list list
.sort((a, b) => (a > b ? 1 : -1)) .sort((a, b) => a.localeCompare(b))
.forEach((item, i) => { .forEach((item, i) => {
const info = pageInfos.get(item) const symbol =
let numExternal
let numInternal
let serverSize
let clientSize
let isAmp
if (info) {
isAmp = info.ampOnly
serverSize = info.serverSize
clientSize = info.clientSize
if (info.chunks) {
const { chunks } = info
numExternal = chunks.external.size
numInternal = chunks.internal.size
}
}
const corner =
i === 0 i === 0
? list.length === 1 ? list.length === 1
? '─' ? '─'
: '┌' : '┌'
: i === list.length - 1
? '└'
: '├' : '├'
console.log(` \x1b[90m${corner}\x1b[39m ${item}${isAmp ? ' (AMP)' : ''}`) const pageInfo = pageInfos.get(item)
if (typeof numExternal === 'number') { messages.push([
console.log(` \x1b[90m| \x1b[39mPackages: ${numExternal} Local modules: ${numInternal}`); `${symbol} ${item}`,
} ...(pageInfo
let sizes = ' \x1b[90m|' ? [
pageInfo.isAmp
if (typeof serverSize === 'number') { ? chalk.cyan('AMP')
sizes += getSizeColor(serverSize) + : pageInfo.size >= 0
` Server size: ${prettyBytes(serverSize)}` ? getPrettySize(pageInfo.size)
} : 'N/A',
if (typeof clientSize === 'number') { pageInfo.chunks
sizes += getSizeColor(clientSize) + ? pageInfo.chunks.internal.size.toString()
` Client size: ${prettyBytes(clientSize)}` : 'N/A',
} pageInfo.chunks
? pageInfo.chunks.external.size.toString()
: 'N/A',
]
: ['', '', '']),
])
})
if (sizes) console.log(sizes) console.log(
console.log(` \x1b[90m${i === list.length - 1 ? '└' : '|'}`); textTable(messages, {
align: ['l', 'l', 'r', 'r'],
stringLength: str => stripAnsi(str).length,
}) })
)
console.log() console.log()
} }
...@@ -209,35 +206,18 @@ export async function getCacheIdentifier({ ...@@ -209,35 +206,18 @@ export async function getCacheIdentifier({
.digest('hex') .digest('hex')
} }
export async function getPageInfo( export async function getPageSizeInKb(
page: string, page: string,
distPath: string, distPath: string,
buildId: string, buildId: string
dev: boolean, ): Promise<number> {
serverless?: boolean,
) {
const info: any = {}
const staticPath = dev ? 'development' : buildId
const clientBundle = path.join( const clientBundle = path.join(
distPath, `static/${staticPath}/pages/`, `${page}.js` distPath,
`static/${buildId}/pages/`,
`${page}.js`
) )
const serverPath = serverless try {
? path.join(distPath, 'serverless/pages') return (await fsStat(clientBundle)).size
: path.join(distPath, 'server/static', staticPath, 'pages') } catch (_) {}
return -1
const serverBundle = path.join(serverPath, `${page}.js`)
info.clientBundle = clientBundle
if (!dev) {
try {
info.serverSize = (await fsStat(serverBundle)).size
} catch (_) {}
try {
info.clientSize = (await fsStat(clientBundle)).size
} catch (_) {}
}
if (page.match(/(_app|_error|_document)/)) return info
return info
} }
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