-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
build-stats-plugin.ts
161 lines (140 loc) · 4.07 KB
/
build-stats-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import fs from 'fs'
import path from 'path'
import { Transform, TransformCallback } from 'stream'
// @ts-ignore no types package
import bfj from 'next/dist/compiled/bfj'
import { spans } from './profiling-plugin'
import { webpack } from 'next/dist/compiled/webpack/webpack'
const STATS_VERSION = 0
function reduceSize(stats: any) {
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.id) {
continue
}
if (!reducedChunk.modules) {
reducedChunk.modules = []
}
reducedChunk.modules.push(module.id)
}
return reducedChunk
})
stats.modules = stats.modules.map((module: any) => {
const reducedModule: any = {
type: module.type,
moduleType: module.moduleType,
size: module.size,
name: module.name,
}
if (module.reasons) {
for (const reason of module.reasons) {
if (!reason.moduleName || reason.moduleId === module.id) {
continue
}
if (!reducedModule.reasons) {
reducedModule.reasons = []
}
reducedModule.reasons.push(reason.moduleId)
}
}
return [module.id, reducedModule]
})
for (const entrypointName in stats.entrypoints) {
delete stats.entrypoints[entrypointName].assets
}
return stats
}
const THRESHOLD = 16 * 1024
class BufferingStream extends Transform {
private items: Buffer[] = []
private itemsSize = 0
_transform(
chunk: Buffer | string,
encoding: BufferEncoding,
callback: TransformCallback
): void {
const buffer = Buffer.isBuffer(chunk)
? chunk
: Buffer.from(chunk as string, encoding)
const size = buffer.length
if (this.itemsSize > 0 && this.itemsSize + size > THRESHOLD) {
this.push(Buffer.concat(this.items))
this.itemsSize = 0
this.items.length = 0
}
if (size > THRESHOLD) {
this.push(buffer)
} else {
this.items.push(buffer)
this.itemsSize += size
}
callback()
}
_flush(callback: TransformCallback): void {
if (this.itemsSize > 0) {
this.push(Buffer.concat(this.items))
this.itemsSize = 0
this.items.length = 0
}
callback()
}
}
// This plugin creates a stats.json for a build when enabled
export default class BuildStatsPlugin {
private distDir: string
constructor(options: { distDir: string }) {
this.distDir = options.distDir
}
apply(compiler: webpack.Compiler) {
compiler.hooks.done.tapAsync(
'NextJsBuildStats',
async (stats, callback) => {
const compilerSpan = spans.get(compiler)
try {
const writeStatsSpan = compilerSpan!.traceChild('NextJsBuildStats')
await writeStatsSpan.traceAsyncFn(() => {
return new Promise((resolve, reject) => {
const statsJson = reduceSize(
stats.toJson({
all: false,
cached: true,
reasons: true,
entrypoints: true,
chunks: true,
errors: false,
warnings: false,
maxModules: Infinity,
chunkModules: true,
modules: true,
// @ts-ignore this option exists
ids: true,
})
)
const fileStream = fs.createWriteStream(
path.join(this.distDir, 'next-stats.json'),
{ highWaterMark: THRESHOLD }
)
const jsonStream = bfj.streamify({
version: STATS_VERSION,
stats: statsJson,
})
jsonStream.pipe(new BufferingStream()).pipe(fileStream)
jsonStream.on('error', reject)
fileStream.on('error', reject)
jsonStream.on('dataError', reject)
fileStream.on('close', resolve)
})
})
callback()
} catch (err) {
callback(err)
}
}
)
}
}