Repository Public
1#!/usr/bin/env node2// Completes the standalone output (Next does not copy static assets into it) and prints the bundle report.3import { cpSync, existsSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";4import { dirname, join, resolve } from "node:path";5import { fileURLToPath } from "node:url";6import { gzipSync } from "node:zlib";78const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");9const standalone = join(root, ".next/standalone/apps/web");10if (!existsSync(join(standalone, "server.js"))) {11 console.error("standalone server not found at " + standalone);12 process.exit(1);13}14cpSync(join(root, ".next/static"), join(standalone, ".next/static"), { recursive: true });15if (existsSync(join(root, "public"))) cpSync(join(root, "public"), join(standalone, "public"), { recursive: true });1617// ---- bundle report: JS/CSS chunk sizes (raw and gzip), measured from the files that will be served18const walk = (dir) => readdirSync(dir).flatMap((f) => { const p = join(dir, f); return statSync(p).isDirectory() ? walk(p) : [p]; });19const files = walk(join(root, ".next/static")).filter((f) => /\.(js|css)$/.test(f));20const rows = files.map((f) => { const buf = readFileSync(f); return { file: f.slice(root.length + 1), raw: buf.length, gzip: gzipSync(buf).length }; }).sort((a, b) => b.raw - a.raw);21const sum = (k, re) => rows.filter((r) => re.test(r.file)).reduce((n, r) => n + r[k], 0);22const report = {23 generated_at: new Date().toISOString(),24 totals: { js_raw: sum("raw", /\.js$/), js_gzip: sum("gzip", /\.js$/), css_raw: sum("raw", /\.css$/), css_gzip: sum("gzip", /\.css$/), files: rows.length },25 largest: rows.slice(0, 15),26};27writeFileSync(join(root, ".next/bundle-report.json"), JSON.stringify(report, null, 2));28const kb = (n) => (n / 1024).toFixed(1) + " kB";29console.log(`bundle: JS ${kb(report.totals.js_raw)} raw / ${kb(report.totals.js_gzip)} gzip · CSS ${kb(report.totals.css_raw)} raw / ${kb(report.totals.css_gzip)} gzip · ${rows.length} files`);30