Skip to content

Commit

Permalink
Colorize Deployed Bundle Size (#3928)
Browse files Browse the repository at this point in the history
* Colorize Deployed Bundle Size
Most bundlers, and other tooling that give you size outputs will colorize their the text to indicate if the value is within certain ranges.
  • Loading branch information
JacobMGEvans authored Sep 15, 2023
1 parent 92e7b7d commit 95b24b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 12 additions & 0 deletions .changeset/gentle-chefs-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"wrangler": patch
---

Colorize Deployed Bundle Size
Most bundlers, and other tooling that give you size outputs will colorize their the text to indicate if the value is within certain ranges.
The current range values are:
red 100% - 90%
yellow 89% - 70%
green <70%

resolves #1312
16 changes: 13 additions & 3 deletions packages/wrangler/src/deployment-bundle/bundle-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Blob } from "node:buffer";
import { gzipSync } from "node:zlib";
import chalk from "chalk";
import { logger } from "../logger";
import type { CfModule } from "./worker";
import type { Metafile } from "esbuild";

const ONE_KIB_BYTES = 1024;
const ONE_MIB_BYTES = ONE_KIB_BYTES * 1024;
const ALLOWED_INITIAL_MAX = ONE_KIB_BYTES * 1024; // Current max is 1 MiB

async function getSize(modules: CfModule[]) {
const gzipSize = gzipSync(
Expand All @@ -29,9 +30,18 @@ export async function printBundleSize(
gzipSize / ONE_KIB_BYTES
).toFixed(2)} KiB`;

logger.log(`Total Upload: ${bundleReport}`);
const percentage = (gzipSize / ALLOWED_INITIAL_MAX) * 100;

if (gzipSize > ONE_MIB_BYTES && !process.env.NO_SCRIPT_SIZE_WARNING) {
const colorizedReport =
percentage > 90
? chalk.red(bundleReport)
: percentage > 70
? chalk.yellow(bundleReport)
: chalk.green(bundleReport);

logger.log(`Total Upload: ${colorizedReport}`);

if (gzipSize > ALLOWED_INITIAL_MAX && !process.env.NO_SCRIPT_SIZE_WARNING) {
logger.warn(
"We recommend keeping your script less than 1MiB (1024 KiB) after gzip. Exceeding past this can affect cold start time"
);
Expand Down

0 comments on commit 95b24b1

Please sign in to comment.