未验证 提交 15d02f65 编写于 作者: T Tim Neutkens 提交者: GitHub

Make experimental stats file approximately 90% smaller (#21858)

Tested against some smaller apps. Will have a high impact for larger apps.
上级 234e1c96
......@@ -1113,7 +1113,8 @@ export default async function getBaseWebpackConfig(
buildId,
rewrites,
}),
!isServer &&
!dev &&
!isServer &&
config.experimental.stats &&
new BuildStatsPlugin({
distDir,
......
......@@ -8,6 +8,64 @@ import { tracer, traceAsyncFn } from '../../tracer'
const STATS_VERSION = 0
function reduceSize(stats: any) {
const modules = new Map()
stats.chunks = stats.chunks.map((chunk: any) => {
const reducedChunk: any = {
id: chunk.id,
files: chunk.files,
size: chunk.size,
}
for (const module of chunk.modules) {
if (!module.identifier) {
continue
}
const reducedModule: any = {
type: module.type,
moduleType: module.moduleType,
size: module.size,
identifier: module.identifier,
}
if (module.reasons) {
for (const reason of module.reasons) {
if (!reason.moduleIdentifier) {
continue
}
if (!reducedModule.reasons) {
reducedModule.reasons = []
}
reducedModule.reasons.push({
moduleIdentifier: reason.moduleIdentifier,
})
}
}
// Identifier is part of the Map
modules.set(module.id, reducedModule)
if (!reducedChunk.modules) {
reducedChunk.modules = []
}
reducedChunk.modules.push(module.id)
}
return reducedChunk
})
stats.modules = [...modules.entries()]
for (const entrypointName in stats.entrypoints) {
delete stats.entrypoints[entrypointName].assets
}
return stats
}
// This plugin creates a stats.json for a build when enabled
export default class BuildStatsPlugin {
private distDir: string
......@@ -25,9 +83,21 @@ export default class BuildStatsPlugin {
const writeStatsSpan = tracer.startSpan('NextJsBuildStats')
await traceAsyncFn(writeStatsSpan, () => {
return new Promise((resolve, reject) => {
const statsJson = stats.toJson({
source: false,
})
const statsJson = reduceSize(
stats.toJson({
all: false,
cached: true,
reasons: true,
entrypoints: true,
chunks: true,
errors: false,
warnings: false,
maxModules: Infinity,
chunkModules: true,
// @ts-ignore this option exists
ids: true,
})
)
const fileStream = fs.createWriteStream(
path.join(this.distDir, 'next-stats.json')
)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册