Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/lean-bundles-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@cloudflare/workers-utils": patch
"miniflare": patch
"wrangler": patch
---

Reduce the installed bundle sizes of Wrangler and Miniflare

Wrangler now resolves bundled workspace dependencies from source during monorepo builds so unused exports can be removed. Miniflare, its shared CLI and container dependencies now use granular `@cloudflare/workers-utils` entry points instead of loading the package barrel, reducing the raw Wrangler and Miniflare artifacts by 6.16 MiB (31.4%) and 1.06 MiB (22.9%) respectively without changing runtime behavior or installed dependencies.
2 changes: 1 addition & 1 deletion packages/cli/check-macos-version.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os from "node:os";
import { UserError } from "@cloudflare/workers-utils";
import { UserError } from "@cloudflare/workers-utils/errors";
import ci from "ci-info";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/containers-shared/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spawn } from "node:child_process";
import { readFileSync } from "node:fs";
import { UserError } from "@cloudflare/workers-utils";
import { UserError } from "@cloudflare/workers-utils/errors";
import { verifyDockerInstalled } from "./utils";
import type {
BuildArgs,
Expand Down
4 changes: 2 additions & 2 deletions packages/containers-shared/src/images.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserError } from "@cloudflare/workers-utils";
import { UserError } from "@cloudflare/workers-utils/errors";
import { buildImage } from "./build";
import { ExternalRegistryKind } from "./client/models/ExternalRegistryKind";
import { getCloudflareContainerRegistry } from "./knobs";
Expand All @@ -16,7 +16,7 @@ import type {
ViteLogger,
WranglerLogger,
} from "./types";
import type { ComplianceConfig } from "@cloudflare/workers-utils";
import type { ComplianceConfig } from "@cloudflare/workers-utils/compliance";

export const DEFAULT_CONTAINER_EGRESS_INTERCEPTOR_IMAGE =
"cloudflare/proxy-everything:3cb1195@sha256:0ef6716c52430096900b150d84a3302057d6cd2319dae7987128c85d0733e3c8";
Expand Down
2 changes: 1 addition & 1 deletion packages/containers-shared/src/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn } from "node:child_process";
import { UserError } from "@cloudflare/workers-utils";
import { UserError } from "@cloudflare/workers-utils/errors";

export async function dockerImageInspect(
dockerPath: string,
Expand Down
4 changes: 2 additions & 2 deletions packages/containers-shared/src/knobs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
COMPLIANCE_REGION_CONFIG_UNKNOWN,
getComplianceRegionSubdomain,
} from "@cloudflare/workers-utils";
} from "@cloudflare/workers-utils/compliance";
import { MF_DEV_CONTAINER_PREFIX } from "./registry";
import type { ComplianceConfig } from "@cloudflare/workers-utils";
import type { ComplianceConfig } from "@cloudflare/workers-utils/compliance";

/**
* Returns the managed container registry for the configured API environment and compliance region.
Expand Down
2 changes: 1 addition & 1 deletion packages/containers-shared/src/login.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn } from "node:child_process";
import { UserError } from "@cloudflare/workers-utils";
import { UserError } from "@cloudflare/workers-utils/errors";
import { ImageRegistriesService, ImageRegistryPermissions } from "./client";
import { OpenAPI } from "./client/core/OpenAPI";

Expand Down
2 changes: 1 addition & 1 deletion packages/containers-shared/src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getCloudflareContainerRegistry } from "./knobs";
import type { ComplianceConfig } from "@cloudflare/workers-utils";
import type { ComplianceConfig } from "@cloudflare/workers-utils/compliance";

/**
* Adds the Cloudflare account namespace to an image tag in the managed registry.
Expand Down
2 changes: 1 addition & 1 deletion packages/containers-shared/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { randomUUID } from "node:crypto";
import { existsSync } from "node:fs";
import { release } from "node:os";
import { UserError } from "@cloudflare/workers-utils";
import { UserError } from "@cloudflare/workers-utils/errors";
import { dockerImageInspect } from "./inspect";
import type { ContainerDevOptions } from "./types";

Expand Down
4 changes: 2 additions & 2 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { setTimeout as wait } from "node:timers/promises";
import util from "node:util";
import zlib from "node:zlib";
import { checkMacOSVersion } from "@cloudflare/cli-shared-helpers";
import { removeDir, removeDirSync } from "@cloudflare/workers-utils";
import { formatZodError } from "@cloudflare/workers-utils";
import { removeDir, removeDirSync } from "@cloudflare/workers-utils/fs-helpers";
import { formatZodError } from "@cloudflare/workers-utils/zod-format";
import { $ as colors$, bold, dim, green, yellow } from "kleur/colors";
import stoppable from "stoppable";
import { getGlobalDispatcher, Pool } from "undici";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "node:fs";
import path from "node:path";
import { brandColor, dim, red } from "@cloudflare/cli-shared-helpers/colors";
import { spinner } from "@cloudflare/cli-shared-helpers/interactive";
import { removeDir } from "@cloudflare/workers-utils";
import { removeDir } from "@cloudflare/workers-utils/fs-helpers";
import { CDP_WEBSOCKET_ENDPOINT_REGEX, launch } from "@puppeteer/browsers";
import BROWSER_RENDERING_WORKER from "worker:browser-rendering/binding";
import { kVoid } from "../../runtime";
Expand Down
6 changes: 2 additions & 4 deletions packages/miniflare/src/plugins/browser-rendering/install.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import {
getGlobalWranglerCachePath,
removeDir,
} from "@cloudflare/workers-utils";
import { removeDir } from "@cloudflare/workers-utils/fs-helpers";
import { getGlobalWranglerCachePath } from "@cloudflare/workers-utils/global-wrangler-config-path";
import {
Browser,
detectBrowserPlatform,
Expand Down
2 changes: 1 addition & 1 deletion packages/miniflare/src/plugins/core/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
containerPrivilegesAllowed,
FUSE_CONTAINER_PRIVILEGES,
} from "@cloudflare/containers-shared";
import { getDockerPath } from "@cloudflare/workers-utils";
import { getDockerPath } from "@cloudflare/workers-utils/docker-path";
import type {
Worker_ContainerEngine,
Worker_DurableObjectNamespace_ContainerOptions_ContainerPrivileges,
Expand Down
4 changes: 2 additions & 2 deletions packages/miniflare/src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { TextEncoder } from "node:util";
import { DEFAULT_CONTAINER_EGRESS_INTERCEPTOR_IMAGE } from "@cloudflare/containers-shared";
import {
getTodaysCompatDate,
removeDirSync,
stripRedundantNodejsCompatFlags,
} from "@cloudflare/workers-utils";
} from "@cloudflare/workers-utils/compatibility-date";
import { removeDirSync } from "@cloudflare/workers-utils/fs-helpers";
import SCRIPT_ACCESS_IDENTITY from "worker:access/access-identity";
import SCRIPT_DEV_CONTROL from "worker:core/dev-control";
import SCRIPT_ENTRY from "worker:core/entry";
Expand Down
2 changes: 1 addition & 1 deletion packages/miniflare/src/plugins/core/node-compat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolveNodejsCompat } from "@cloudflare/workers-utils";
import { resolveNodejsCompat } from "@cloudflare/workers-utils/compatibility-date";

/**
* We can provide Node.js compatibility in a number of different modes:
Expand Down
2 changes: 1 addition & 1 deletion packages/miniflare/src/shared/dev-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
writeFileSync,
} from "node:fs";
import path from "node:path";
import { getGlobalConfigPath } from "@cloudflare/workers-utils";
import { getGlobalConfigPath } from "@cloudflare/workers-utils/global-wrangler-config-path";
import { watch } from "chokidar";
import type { WorkerDefinition, WorkerRegistry } from "./dev-registry-types";
export type { WorkerDefinition, WorkerRegistry };
Expand Down
15 changes: 10 additions & 5 deletions packages/miniflare/test/plugins/browser/install.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ const PLATFORM = "linux64";

let cacheDir: string;

vi.mock("@cloudflare/workers-utils", async (importOriginal) => ({
...(await importOriginal<typeof import("@cloudflare/workers-utils")>()),
// Keep the real `removeDir`; only the cache location is faked.
getGlobalWranglerCachePath: () => cacheDir,
}));
vi.mock(
"@cloudflare/workers-utils/global-wrangler-config-path",
async (importOriginal) => ({
...(await importOriginal<
typeof import("@cloudflare/workers-utils/global-wrangler-config-path")
>()),
// Keep the real filesystem helper; only the cache location is faked.
getGlobalWranglerCachePath: () => cacheDir,
})
);

const install = vi.hoisted(() => vi.fn());

Expand Down
8 changes: 5 additions & 3 deletions packages/miniflare/test/plugins/core/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
containerPrivilegesAllowed,
FUSE_CONTAINER_PRIVILEGES,
} from "@cloudflare/containers-shared";
import { getDockerPath } from "@cloudflare/workers-utils";
import { getDockerPath } from "@cloudflare/workers-utils/docker-path";
import { beforeEach, test, vi } from "vitest";
import {
ContainerPrivilegesCache,
Expand All @@ -13,8 +13,10 @@ vi.mock("@cloudflare/containers-shared", async (importOriginal) => ({
...(await importOriginal<typeof import("@cloudflare/containers-shared")>()),
containerPrivilegesAllowed: vi.fn(),
}));
vi.mock("@cloudflare/workers-utils", async (importOriginal) => ({
...(await importOriginal<typeof import("@cloudflare/workers-utils")>()),
vi.mock("@cloudflare/workers-utils/docker-path", async (importOriginal) => ({
...(await importOriginal<
typeof import("@cloudflare/workers-utils/docker-path")
>()),
getDockerPath: vi.fn(),
}));

Expand Down
20 changes: 20 additions & 0 deletions packages/workers-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"import": "./dist/prometheus-metrics.mjs",
"types": "./dist/prometheus-metrics.d.mts"
},
"./compliance": {
"import": "./dist/compliance.mjs",
"types": "./dist/compliance.d.mts"
},
"./test-helpers": {
"import": "./dist/test-helpers/index.mjs",
"types": "./dist/test-helpers/index.d.mts"
Expand All @@ -35,9 +39,25 @@
"import": "./dist/fs-helpers.mjs",
"types": "./dist/fs-helpers.d.mts"
},
"./compatibility-date": {
"import": "./dist/compatibility-date.mjs",
"types": "./dist/compatibility-date.d.mts"
},
"./docker-path": {
"import": "./dist/docker-path.mjs",
"types": "./dist/docker-path.d.mts"
},
"./errors": {
"import": "./dist/errors.mjs",
"types": "./dist/errors.d.mts"
},
"./global-wrangler-config-path": {
"import": "./dist/global-wrangler-config-path.mjs",
"types": "./dist/global-wrangler-config-path.d.mts"
},
"./zod-format": {
"import": "./dist/zod-format.mjs",
"types": "./dist/zod-format.d.mts"
}
},
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions packages/workers-utils/src/compliance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export {
COMPLIANCE_REGION_CONFIG_PUBLIC,
COMPLIANCE_REGION_CONFIG_UNKNOWN,
getCloudflareApiBaseUrl,
getCloudflareComplianceRegion,
getComplianceRegionSubdomain,
} from "./environment-variables/misc-variables";
export type { ComplianceConfig } from "./environment-variables/misc-variables";
13 changes: 13 additions & 0 deletions packages/workers-utils/src/docker-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getEnvironmentVariableFactory } from "./environment-variables/factory";

/**
* Returns the configured path to the Docker binary.
*
* @returns The value of `WRANGLER_DOCKER_BIN`, or `docker` when unset.
*/
export const getDockerPath = getEnvironmentVariableFactory({
variableName: "WRANGLER_DOCKER_BIN",
defaultValue() {
return "docker";
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,7 @@ export const getD1ExtraLocationChoices: () => string | undefined =
*
* By default it's `docker`.
*/
export const getDockerPath = getEnvironmentVariableFactory({
variableName: "WRANGLER_DOCKER_BIN",
defaultValue() {
return "docker";
},
});
export { getDockerPath } from "../docker-path";

export const getSubdomainMixedStateCheckDisabled =
getBooleanEnvironmentVariableFactory({
Expand Down
10 changes: 7 additions & 3 deletions packages/workers-utils/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ export default defineConfig(() => [
"src/browser.ts",
"src/prometheus-metrics.ts",
"src/test-helpers/index.ts",
// Leaf entry points that only depend on Node.js builtins, so they can
// be imported by packages bundling to ESM (e.g. via Vite) without
// pulling in the barrel's CommonJS dependencies.
// Leaf entry points let consumers bundle individual utilities without
// pulling in the broad package barrel and its transitive dependencies.
"src/compliance.ts",
"src/compatibility-date.ts",
"src/docker-path.ts",
"src/errors.ts",
"src/fs-helpers.ts",
"src/global-wrangler-config-path.ts",
"src/zod-format.ts",
],
platform: "node",
format: "esm",
Expand Down
32 changes: 32 additions & 0 deletions packages/wrangler/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ import { EXTERNAL_DEPENDENCIES } from "./scripts/deps";
import type { Options } from "tsup";

const TEMPLATES_DIR = path.join(__dirname, "templates");
const MONOREPO_PACKAGE_ALIASES = {
"@cloudflare/autoconfig": path.join(__dirname, "../autoconfig/src/index.ts"),
"@cloudflare/deploy-helpers/context": path.join(
__dirname,
"../deploy-helpers/src/shared/context.ts"
),
"@cloudflare/deploy-helpers/create-worker-upload-form": path.join(
__dirname,
"../deploy-helpers/src/deploy/helpers/create-worker-upload-form.ts"
),
"@cloudflare/deploy-helpers": path.join(
__dirname,
"../deploy-helpers/src/index.ts"
),
"@cloudflare/pages-functions": path.join(
__dirname,
"../pages-functions/src/index.ts"
),
"@cloudflare/runtime-types": path.join(
__dirname,
"../runtime-types/src/index.ts"
),
"@cloudflare/workers-utils": path.join(__dirname, "../workers-utils/src"),
};
const workersContexts = new Map<string, esbuild.BuildContext>();
function embedWorkersPlugin({
isWatch,
Expand Down Expand Up @@ -113,6 +137,14 @@ export default defineConfig((options) => [
},
esbuildPlugins: [embedWorkersPlugin({ isWatch: !!options.watch })],
esbuildOptions(esbuildOptions) {
// These workspace packages publish bundles for npm consumers. Resolving
// their sources here gives Wrangler's monorepo build enough module
// structure to tree-shake exports it doesn't use, without changing the
// dependencies installed with the published package.
esbuildOptions.alias = {
...esbuildOptions.alias,
...MONOREPO_PACKAGE_ALIASES,
};
esbuildOptions.logOverride = {
...esbuildOptions.logOverride,
// Suppress the warning for the intentional runtime dynamic
Expand Down
Loading