Skip to content

Commit 80bbee3

Browse files
workers-devprodpetebacondarwin
andauthoredMar 17, 2025··
Support no_bundle config in Pages for both dev and deploy. (#8500)
This was already supported via a command line arg (`--no-bundle`). Co-authored-by: Peter Bacon Darwin <pbacondarwin@cloudflare.com>

File tree

6 files changed

+66
-12
lines changed

6 files changed

+66
-12
lines changed
 

‎.changeset/crazy-sheep-give.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Support `no_bundle` config in Pages for both `dev` and `deploy`.
6+
7+
This was already supported via a command line arg (`--no-bundle`).

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

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mkdirSync, writeFileSync } from "node:fs";
22
import { chdir } from "node:process";
3+
import TOML from "@iarna/toml";
34
import { execa } from "execa";
45
import { http, HttpResponse } from "msw";
56
import dedent from "ts-dedent";
@@ -74,7 +75,7 @@ describe("pages deploy", () => {
7475
--commit-message The commit message to attach to this deployment [string]
7576
--commit-dirty Whether or not the workspace should be considered dirty for this deployment [boolean]
7677
--skip-caching Skip asset caching which speeds up builds [boolean]
77-
--no-bundle Whether to run bundling on \`_worker.js\` before deploying [boolean] [default: false]
78+
--no-bundle Whether to run bundling on \`_worker.js\` before deploying [boolean]
7879
--upload-source-maps Whether to upload any server-side sourcemaps with this deployment [boolean] [default: false]"
7980
`);
8081
});
@@ -5425,6 +5426,51 @@ Failed to publish your Function. Got error: Uncaught TypeError: a is not a funct
54255426
});
54265427
});
54275428

5429+
describe("_worker.js directory bundling", () => {
5430+
const workerIsBundled = async (contents: FormDataEntryValue | null) =>
5431+
(await toString(contents)).includes("worker_default as default");
5432+
5433+
["wrangler.json", "wrangler.toml"].forEach((configPath) => {
5434+
it(
5435+
"should not bundle the _worker.js when `no_bundle = true` in Wrangler config: " +
5436+
configPath,
5437+
async () => {
5438+
mkdirSync("public/_worker.js", { recursive: true });
5439+
writeFileSync(
5440+
"public/_worker.js/index.js",
5441+
`
5442+
export default {
5443+
async fetch(request, env) {
5444+
return new Response('Ok');
5445+
}
5446+
};
5447+
`
5448+
);
5449+
5450+
const config = {
5451+
name: "foo",
5452+
no_bundle: true,
5453+
pages_build_output_dir: "public",
5454+
};
5455+
writeFileSync(
5456+
`${configPath}`,
5457+
configPath === "wrangler.json"
5458+
? JSON.stringify(config)
5459+
: TOML.stringify(config)
5460+
);
5461+
5462+
simulateServer((generatedWorkerJS) =>
5463+
expect(workerIsBundled(generatedWorkerJS)).resolves.toBeFalsy()
5464+
);
5465+
5466+
await runWrangler("pages deploy");
5467+
5468+
expect(std.out).toContain("✨ Uploading Worker bundle");
5469+
}
5470+
);
5471+
});
5472+
});
5473+
54285474
describe("source maps", () => {
54295475
const bundleString = (entry: FormDataEntryValue | null) =>
54305476
toString(entry).then((str) =>

‎packages/wrangler/src/__tests__/pages/pages.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe("pages", () => {
6363
--inspector-port Port for devtools to connect to [number]
6464
--proxy The port to proxy (where the static assets are served) [deprecated] [number]
6565
--script-path The location of the single Worker script if not using functions [default: _worker.js] [deprecated] [string]
66-
--no-bundle Whether to run bundling on \`_worker.js\` [boolean] [default: false]
66+
--no-bundle Whether to run bundling on \`_worker.js\` [boolean]
6767
-b, --binding Bind variable/secret (KEY=VALUE) [array]
6868
-k, --kv KV namespace to bind (--kv KV_BINDING) [array]
6969
--d1 D1 database to bind (--d1 D1_BINDING) [array]
@@ -150,7 +150,7 @@ describe("pages", () => {
150150
--commit-message The commit message to attach to this deployment [string]
151151
--commit-dirty Whether or not the workspace should be considered dirty for this deployment [boolean]
152152
--skip-caching Skip asset caching which speeds up builds [boolean]
153-
--no-bundle Whether to run bundling on \`_worker.js\` before deploying [boolean] [default: false]
153+
--no-bundle Whether to run bundling on \`_worker.js\` before deploying [boolean]
154154
--upload-source-maps Whether to upload any server-side sourcemaps with this deployment [boolean] [default: false]"
155155
`);
156156
});

‎packages/wrangler/src/config/validation-pages.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const supportedPagesConfigFields = [
1919
"compatibility_date",
2020
"compatibility_flags",
2121
"send_metrics",
22+
"no_bundle",
2223
"limits",
2324
"placement",
2425
"vars",

‎packages/wrangler/src/pages/deploy.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ export function Options(yargs: CommonYargsArgv) {
7777
},
7878
"no-bundle": {
7979
type: "boolean",
80-
default: false,
80+
default: undefined,
81+
conflicts: "bundle",
8182
description: "Whether to run bundling on `_worker.js` before deploying",
8283
},
8384
config: {
@@ -349,6 +350,8 @@ export const Handler = async (args: PagesDeployArgs) => {
349350
}
350351
}
351352

353+
const enableBundling = args.bundle ?? !(args.noBundle ?? config?.no_bundle);
354+
352355
const { deploymentResponse, formData } = await deploy({
353356
directory,
354357
accountId,
@@ -358,9 +361,7 @@ export const Handler = async (args: PagesDeployArgs) => {
358361
commitHash,
359362
commitDirty,
360363
skipCaching: args.skipCaching,
361-
// TODO: Here lies a known bug. If you specify both `--bundle` and `--no-bundle`, this behavior is undefined and you will get unexpected results.
362-
// There is no sane way to get the true value out of yargs, so here we are.
363-
bundle: args.bundle ?? !args.noBundle,
364+
bundle: enableBundling,
364365
// Sourcemaps from deploy arguments will take precedence so people can try it for one-off deployments without updating their wrangler.toml
365366
sourceMaps: config?.upload_source_maps || args.uploadSourceMaps,
366367
args,

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ export function Options(yargs: CommonYargsArgv) {
147147
},
148148
"no-bundle": {
149149
type: "boolean",
150-
default: false,
150+
default: undefined,
151+
conflicts: "bundle",
151152
description: "Whether to run bundling on `_worker.js`",
152153
},
153154
binding: {
@@ -357,9 +358,7 @@ export const Handler = async (args: PagesDevArguments) => {
357358
const usingWorkerDirectory =
358359
existsSync(workerScriptPath) && lstatSync(workerScriptPath).isDirectory();
359360
const usingWorkerScript = existsSync(workerScriptPath);
360-
// TODO: Here lies a known bug. If you specify both `--bundle` and `--no-bundle`, this behavior is undefined and you will get unexpected results.
361-
// There is no sane way to get the true value out of yargs, so here we are.
362-
const enableBundling = args.bundle ?? !args.noBundle;
361+
const enableBundling = args.bundle ?? !(args.noBundle ?? config.no_bundle);
363362

364363
const functionsDirectory = "./functions";
365364
let usingFunctions = !usingWorkerScript && existsSync(functionsDirectory);
@@ -371,7 +370,7 @@ export const Handler = async (args: PagesDevArguments) => {
371370
args.compatibilityFlags ?? config.compatibility_flags ?? [],
372371
{
373372
nodeCompat: args.nodeCompat,
374-
noBundle: args.noBundle ?? config.no_bundle,
373+
noBundle: !enableBundling,
375374
}
376375
);
377376

0 commit comments

Comments
 (0)
Please sign in to comment.