forked from vercel/next.js
/
trace-next-server.js
137 lines (120 loc) · 3.75 KB
/
trace-next-server.js
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
const os = require('os')
const path = require('path')
const execa = require('execa')
const fs = require('fs-extra')
const prettyBytes = require('pretty-bytes')
const gzipSize = require('next/dist/compiled/gzip-size')
const { nodeFileTrace } = require('next/dist/compiled/@vercel/nft')
const { linkPackages } =
require('../.github/actions/next-stats-action/src/prepare/repo-setup')()
const MAX_COMPRESSED_SIZE = 250 * 1000
const MAX_UNCOMPRESSED_SIZE = 2.5 * 1000 * 1000
// install next outside the monorepo for clean `node_modules`
// to trace against which helps ensure minimal trace is
// produced.
// react and react-dom need to be traced specific to installed
// version so isn't pre-traced
async function main() {
const tmpdir = os.tmpdir()
const repoDir = path.join(__dirname, '..')
const workDir = path.join(tmpdir, `trace-next-${Date.now()}`)
console.log('using workdir', workDir)
await fs.ensureDir(workDir)
const pkgPaths = await linkPackages(repoDir)
await fs.writeFile(
path.join(workDir, 'package.json'),
JSON.stringify(
{
dependencies: {
next: pkgPaths.get('next'),
},
private: true,
},
null,
2
)
)
await execa('yarn', ['install'], {
cwd: workDir,
stdio: ['ignore', 'inherit', 'inherit'],
env: {
...process.env,
YARN_CACHE_FOLDER: path.join(workDir, '.yarn-cache'),
},
})
// remove temporary package packs
pkgPaths.forEach((packagePath) => {
fs.unlinkSync(packagePath)
})
// remove changes to package.json files from packing
await execa('git', ['checkout', '.'], {
cwd: repoDir,
stdio: ['ignore', 'inherit', 'inherit'],
})
const nextServerPath = path.join(
workDir,
'node_modules/next/dist/server/next-server.js'
)
const traceLabel = `traced ${nextServerPath}`
console.time(traceLabel)
const result = await nodeFileTrace([nextServerPath], {
base: workDir,
processCwd: workDir,
ignore: [
'node_modules/next/dist/pages/**/*',
'node_modules/next/dist/server/image-optimizer.js',
'node_modules/next/dist/compiled/@ampproject/toolbox-optimizer/**/*',
'node_modules/next/dist/server/lib/squoosh/**/*.wasm',
'node_modules/next/dist/compiled/webpack/(bundle4|bundle5).js',
'node_modules/react/**/*.development.js',
'node_modules/react-dom/**/*.development.js',
'node_modules/use-subscription/**/*.development.js',
'node_modules/sharp/**/*',
],
})
const tracedDeps = new Set()
let totalCompressedSize = 0
let totalUncompressedSize = 0
for (const file of result.fileList) {
if (result.reasons[file].type === 'initial') {
continue
}
tracedDeps.add(file)
const stat = await fs.stat(path.join(workDir, file))
if (stat.isFile()) {
const compressedSize = await gzipSize(path.join(workDir, file))
totalUncompressedSize += stat.size || 0
totalCompressedSize += compressedSize
} else {
console.log('not a file', file, stat.isDirectory())
}
}
console.log({
numberFiles: tracedDeps.size,
totalGzipSize: prettyBytes(totalCompressedSize),
totalUncompressedSize: prettyBytes(totalUncompressedSize),
})
await fs.writeFile(
path.join(
__dirname,
'../packages/next/dist/server/next-server.js.nft.json'
),
JSON.stringify({
files: Array.from(tracedDeps),
version: 1,
})
)
await fs.remove(workDir)
console.timeEnd(traceLabel)
if (
totalCompressedSize > MAX_COMPRESSED_SIZE ||
totalUncompressedSize > MAX_UNCOMPRESSED_SIZE
) {
throw new Error(
`Max traced size of next-server exceeded limits of ${MAX_COMPRESSED_SIZE} compressed or ${MAX_UNCOMPRESSED_SIZE} uncompressed`
)
}
}
main()
.then(() => console.log('done'))
.catch(console.error)