Skip to content

Commit 707cb6f

Browse files
authored
[wrangler] Add bundle sizes to structured output (#14915)
1 parent bda1ccc commit 707cb6f

9 files changed

Lines changed: 83 additions & 43 deletions

File tree

‎.changeset/tidy-ravens-report.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Include exact raw and gzip-compressed Worker bundle sizes in structured `deploy` and `version-upload` output.

‎packages/deploy-helpers/src/deploy/deploy.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import {
2323
syncAssets,
2424
} from "./helpers/assets";
2525
import { getBindings } from "./helpers/binding-utils";
26-
import { printBundleSize } from "./helpers/bundle-reporter";
26+
import {
27+
getSize,
28+
printBundleSize,
29+
type BundleSize,
30+
} from "./helpers/bundle-reporter";
2731
import { confirmLatestDeploymentOverwrite } from "./helpers/confirm-latest-deployment-overwrite";
2832
import { createWorkerUploadForm } from "./helpers/create-worker-upload-form";
2933
import { deployWfpUserWorker } from "./helpers/deploy-wfp";
@@ -140,6 +144,7 @@ type DeployResult = {
140144
workerTag: string | null;
141145
assetUploadStats?: AssetUploadStats;
142146
targets?: string[];
147+
bundleSize?: BundleSize;
143148
};
144149

145150
export default async function deploy(
@@ -161,6 +166,12 @@ export default async function deploy(
161166
targets: result.targets,
162167
wrangler_environment: props.env,
163168
worker_name_overridden: props.workerNameOverridden ?? false,
169+
bundle_size: result.bundleSize
170+
? {
171+
raw_bytes: result.bundleSize.size,
172+
gzip_bytes: result.bundleSize.gzipSize,
173+
}
174+
: undefined,
164175
});
165176

166177
return result;
@@ -367,10 +378,8 @@ async function deployWorker(
367378
0
368379
);
369380

370-
await printBundleSize(
371-
{ name: path.basename(resolvedEntryPointPath), content: content },
372-
modules
373-
);
381+
const bundleSize = await getSize([...modules, { content }]);
382+
printBundleSize(bundleSize);
374383

375384
// We can use the new versions/deployments APIs if we:
376385
// * are uploading a worker that already exists
@@ -746,7 +755,7 @@ async function deployWorker(
746755

747756
if (isDryRun) {
748757
logger.log(`--dry-run: exiting now.`);
749-
return { versionId, workerTag };
758+
return { versionId, workerTag, bundleSize };
750759
}
751760

752761
const uploadMs = Date.now() - start;
@@ -769,7 +778,7 @@ async function deployWorker(
769778
// Early exit for WfP since it doesn't need the below code
770779
if (props.dispatchNamespace !== undefined) {
771780
deployWfpUserWorker(props.dispatchNamespace, versionId);
772-
return { versionId, workerTag, assetUploadStats };
781+
return { versionId, workerTag, assetUploadStats, bundleSize };
773782
}
774783
assert(accountId);
775784
// deploy triggers
@@ -793,5 +802,6 @@ async function deployWorker(
793802
workerTag,
794803
assetUploadStats,
795804
targets: targets ?? [],
805+
bundleSize,
796806
};
797807
}

‎packages/deploy-helpers/src/deploy/helpers/bundle-reporter.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ const ONE_KIB_BYTES = 1024;
99
// See https://developers.cloudflare.com/workers/platform/limits/#worker-size
1010
const MAX_GZIP_SIZE_BYTES = 3 * ONE_KIB_BYTES * ONE_KIB_BYTES;
1111

12-
async function getSize(modules: Pick<CfModule, "content">[]) {
12+
export interface BundleSize {
13+
size: number;
14+
gzipSize: number;
15+
}
16+
17+
export async function getSize(
18+
modules: Pick<CfModule, "content">[]
19+
): Promise<BundleSize> {
1320
const gzipSize = gzipSync(
1421
await new Blob(modules.map((file) => file.content)).arrayBuffer()
1522
).byteLength;
@@ -18,15 +25,7 @@ async function getSize(modules: Pick<CfModule, "content">[]) {
1825
return { size: aggregateSize, gzipSize };
1926
}
2027

21-
export async function printBundleSize(
22-
main: {
23-
name: string;
24-
content: string;
25-
},
26-
modules: CfModule[]
27-
) {
28-
const { size, gzipSize } = await getSize([...modules, main]);
29-
28+
export function printBundleSize({ size, gzipSize }: BundleSize) {
3029
const bundleReport = `${(size / ONE_KIB_BYTES).toFixed(2)} KiB / gzip: ${(
3130
gzipSize / ONE_KIB_BYTES
3231
).toFixed(2)} KiB`;

‎packages/deploy-helpers/src/deploy/versions-upload.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import { getWorkersDevSubdomain } from "../triggers/subdomain";
1616
import { resolveAssetOptions, syncAssets } from "./helpers/assets";
1717
import { renderBindingDependsOnExportError } from "./helpers/binding-depends-on-export";
1818
import { getBindings } from "./helpers/binding-utils";
19-
import { printBundleSize } from "./helpers/bundle-reporter";
19+
import {
20+
getSize,
21+
printBundleSize,
22+
type BundleSize,
23+
} from "./helpers/bundle-reporter";
2024
import { createWorkerUploadForm } from "./helpers/create-worker-upload-form";
2125
import {
2226
applyServiceAndEnvironmentTags,
@@ -59,6 +63,7 @@ type VersionsUploadResult = {
5963
assetUploadStats?: AssetUploadStats;
6064
versionPreviewUrl?: string | undefined;
6165
versionPreviewAliasUrl?: string | undefined;
66+
bundleSize?: BundleSize;
6267
};
6368

6469
export default async function versionsUpload(
@@ -86,6 +91,12 @@ export default async function versionsUpload(
8691
preview_alias_url: result.versionPreviewAliasUrl,
8792
wrangler_environment: props.env,
8893
worker_name_overridden: props.workerNameOverridden ?? false,
94+
bundle_size: result.bundleSize
95+
? {
96+
raw_bytes: result.bundleSize.size,
97+
gzip_bytes: result.bundleSize.gzipSize,
98+
}
99+
: undefined,
89100
});
90101

91102
return result;
@@ -232,10 +243,8 @@ async function uploadWorkerVersion(
232243
: undefined,
233244
};
234245

235-
await printBundleSize(
236-
{ name: path.basename(resolvedEntryPointPath), content: content },
237-
modules
238-
);
246+
const bundleSize = await getSize([...modules, { content }]);
247+
printBundleSize(bundleSize);
239248

240249
let workerBundle: FormData;
241250

@@ -415,7 +424,7 @@ async function uploadWorkerVersion(
415424

416425
if (props.dryRun) {
417426
logger.log(`--dry-run: exiting now.`);
418-
return { versionId, workerTag };
427+
return { versionId, workerTag, bundleSize };
419428
}
420429
assert(accountId);
421430

@@ -466,5 +475,6 @@ Changes to triggers (routes, custom domains, cron schedules, etc) must be applie
466475
assetUploadStats: assetsUploadResult?.assetUploadStats,
467476
versionPreviewUrl,
468477
versionPreviewAliasUrl,
478+
bundleSize,
469479
};
470480
}

‎packages/workers-utils/src/output.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ interface OutputEntryBase<T extends string> {
7474
type: T;
7575
}
7676

77+
interface OutputEntryBundleSize {
78+
/** The uncompressed size of the Worker bundle. */
79+
raw_bytes: number;
80+
/** The gzip-compressed size of the Worker bundle. */
81+
gzip_bytes: number;
82+
}
83+
7784
/**
7885
* All the different types of entry that can be written to the output file.
7986
*/
@@ -112,6 +119,8 @@ interface OutputEntryDeployment extends OutputEntryBase<"deploy"> {
112119
worker_name_overridden: boolean;
113120
/** wrangler environment used */
114121
wrangler_environment: string | undefined;
122+
/** Exact Worker bundle sizes in bytes. */
123+
bundle_size?: OutputEntryBundleSize;
115124
}
116125

117126
interface OutputEntryPreview extends OutputEntryBase<"preview"> {
@@ -194,6 +203,8 @@ interface OutputEntryVersionUpload extends OutputEntryBase<"version-upload"> {
194203
worker_name_overridden: boolean;
195204
/** wrangler environment used */
196205
wrangler_environment: string | undefined;
206+
/** Exact Worker bundle sizes in bytes. */
207+
bundle_size?: OutputEntryBundleSize;
197208
}
198209

199210
interface OutputEntryVersionDeployment extends OutputEntryBase<"version-deploy"> {

‎packages/wrangler/src/__tests__/deploy/build.test.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
import * as esbuild from "esbuild";
1212
import { http, HttpResponse } from "msw";
1313
import { afterEach, beforeEach, describe, it, test, vi } from "vitest";
14-
import { printBundleSize } from "../../deployment-bundle/bundle-reporter";
14+
import {
15+
getSize,
16+
printBundleSize,
17+
} from "../../deployment-bundle/bundle-reporter";
1518
import { clearOutputFilePath } from "../../output";
1619
import { diagnoseScriptSizeError } from "../../utils/friendly-validator-errors";
1720
import { mockAccountId, mockApiToken } from "../helpers/mock-account-id";
@@ -1117,17 +1120,19 @@ export default { fetch() { return new Response(foo); } }`
11171120
// keeping these as unit tests to try and keep them snappy, as they often deal with
11181121
// big files that would take a while to deal with in a full wrangler test
11191122

1120-
test("should print the bundle size", async ({ expect }) => {
1123+
test("should calculate the bundle size", async ({ expect }) => {
11211124
const bigModule = Buffer.alloc(10_000_000);
11221125
randomFillSync(bigModule);
1123-
await printBundleSize({ name: "index.js", content: "" }, [
1124-
{
1125-
name: "index.js",
1126-
filePath: undefined,
1127-
content: bigModule,
1128-
type: "buffer",
1129-
},
1130-
]);
1126+
const bundleSize = await getSize([{ content: bigModule }]);
1127+
1128+
expect(bundleSize).toEqual({
1129+
size: 10_000_000,
1130+
gzipSize: expect.any(Number),
1131+
});
1132+
});
1133+
1134+
test("should print the bundle size", ({ expect }) => {
1135+
printBundleSize({ size: 10_000_000, gzipSize: 10_000_000 });
11311136

11321137
expect(std).toMatchInlineSnapshot(`
11331138
{

‎packages/wrangler/src/__tests__/deploy/core.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,13 @@ describe("deploy", () => {
19561956
.map((line) => JSON.parse(line)) as OutputEntry[];
19571957

19581958
expect(outputEntries).toContainEqual(
1959-
expect.objectContaining({ type: "deploy" })
1959+
expect.objectContaining({
1960+
type: "deploy",
1961+
bundle_size: {
1962+
raw_bytes: expect.any(Number),
1963+
gzip_bytes: expect.any(Number),
1964+
},
1965+
})
19601966
);
19611967

19621968
const autoconfigOutputEntry = outputEntries.find(
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { printBundleSize } from "@cloudflare/deploy-helpers";
1+
export { getSize, printBundleSize } from "@cloudflare/deploy-helpers";

‎packages/wrangler/src/dev/remote.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from "node:path";
33
import { syncAssets } from "@cloudflare/deploy-helpers";
44
import { APIError, UserError } from "@cloudflare/workers-utils";
55
import { isAuthenticationError } from "../core/handle-errors";
6-
import { printBundleSize } from "../deployment-bundle/bundle-reporter";
6+
import { getSize, printBundleSize } from "../deployment-bundle/bundle-reporter";
77
import { getBundleType } from "../deployment-bundle/bundle-type";
88
import { withSourceURLs } from "../deployment-bundle/source-url";
99
import { getInferredHost } from "../dev";
@@ -164,13 +164,7 @@ export async function createRemoteWorkerInit(props: {
164164
);
165165

166166
// TODO: For Dev we could show the reporter message in the interactive box.
167-
void printBundleSize(
168-
{
169-
name: path.basename(props.bundle.path),
170-
content,
171-
},
172-
props.modules
173-
);
167+
void getSize([...props.modules, { content }]).then(printBundleSize);
174168

175169
const workersSitesAssets = await syncWorkersSite(
176170
props.complianceConfig,

0 commit comments

Comments
 (0)