|
| 1 | +const {nodeFileTrace} = require('@vercel/nft'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | + |
| 5 | +function formatBytes(bytes, decimals = 2) { |
| 6 | + if (bytes === 0) return '0 Bytes'; |
| 7 | + const k = 1024; |
| 8 | + const dm = decimals < 0 ? 0 : decimals; |
| 9 | + const sizes = ['Bytes', 'KB', 'MB', 'GB']; |
| 10 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 11 | + return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; |
| 12 | +} |
| 13 | + |
| 14 | +async function analyzeBuild() { |
| 15 | + try { |
| 16 | + const tracePath = path.join( |
| 17 | + process.cwd(), |
| 18 | + 'node_modules/next/dist/server/next-server.js' |
| 19 | + ); |
| 20 | + |
| 21 | + /* |
| 22 | + collectBuildTraces({ |
| 23 | + dir, |
| 24 | + config, |
| 25 | + distDir, |
| 26 | + edgeRuntimeRoutes: collectRoutesUsingEdgeRuntime(pageInfos), |
| 27 | + staticPages: [...staticPages], |
| 28 | + nextBuildSpan, |
| 29 | + hasSsrAmpPages, |
| 30 | + buildTraceContext, |
| 31 | + outputFileTracingRoot, |
| 32 | + isTurbopack: true, |
| 33 | + } |
| 34 | + */ |
| 35 | + |
| 36 | + const fileSizes = new Map(); |
| 37 | + let totalSize = 0; |
| 38 | + |
| 39 | + const results = await nodeFileTrace([tracePath], { |
| 40 | + base: process.cwd(), |
| 41 | + processCwd: process.cwd(), |
| 42 | + ignore: [ |
| 43 | + 'node_modules/next/dist/pages/**/*', |
| 44 | + 'node_modules/next/dist/server/image-optimizer.js', |
| 45 | + 'node_modules/next/dist/compiled/@ampproject/toolbox-optimizer/**/*', |
| 46 | + 'node_modules/next/dist/compiled/webpack/(bundle4|bundle5).js', |
| 47 | + 'node_modules/react/**/*.development.js', |
| 48 | + 'node_modules/react-dom/**/*.development.js', |
| 49 | + 'node_modules/use-subscription/**/*.development.js', |
| 50 | + 'node_modules/sharp/**/*', |
| 51 | + ], |
| 52 | + }); |
| 53 | + |
| 54 | + for (const file of results.fileList) { |
| 55 | + const stats = fs.statSync(file); |
| 56 | + const fileSize = stats.size; |
| 57 | + fileSizes.set(file, fileSize); |
| 58 | + totalSize += fileSize; |
| 59 | + } |
| 60 | + |
| 61 | + const reportPath = path.join(process.cwd(), 'build-analysis.json'); |
| 62 | + fs.writeFileSync( |
| 63 | + reportPath, |
| 64 | + JSON.stringify( |
| 65 | + { |
| 66 | + files: Array.from(results.fileList), |
| 67 | + reasons: Object.fromEntries( |
| 68 | + Array.from(results.reasons.entries()).map(([file, reason]) => [file, reason]) |
| 69 | + ), |
| 70 | + sizes: Object.fromEntries(fileSizes), |
| 71 | + summary: { |
| 72 | + totalFiles: results.fileList.size, |
| 73 | + totalSize: { |
| 74 | + bytes: totalSize, |
| 75 | + formatted: formatBytes(totalSize), |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + null, |
| 80 | + 2 |
| 81 | + ) |
| 82 | + ); |
| 83 | + console.log(`Report written to ${reportPath}`); |
| 84 | + console.log('Total size:', formatBytes(totalSize)); |
| 85 | + } catch (error) { |
| 86 | + console.error('Error analyzing build:', error); |
| 87 | + process.exit(1); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +analyzeBuild(); |
0 commit comments