From 9c6c3fb5dedeeb96112830381dcf7ff5b49bbb6e Mon Sep 17 00:00:00 2001 From: Sunil Pai Date: Mon, 4 Jul 2022 17:35:12 +0100 Subject: [PATCH] feat: `config.no_bundle` as a configuration option to prevent bundling (#1403) --- .changeset/eleven-apes-grow.md | 7 +++++++ .../wrangler/src/__tests__/configuration.test.ts | 9 +++++++++ packages/wrangler/src/__tests__/publish.test.ts | 15 ++++++++++++++- packages/wrangler/src/config/environment.ts | 7 +++++++ packages/wrangler/src/config/validation.ts | 8 ++++++++ packages/wrangler/src/dev.tsx | 3 +-- packages/wrangler/src/index.tsx | 3 +-- 7 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 .changeset/eleven-apes-grow.md diff --git a/.changeset/eleven-apes-grow.md b/.changeset/eleven-apes-grow.md new file mode 100644 index 000000000000..e56262d59a51 --- /dev/null +++ b/.changeset/eleven-apes-grow.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +feat: `config.no_bundle` as a configuration option to prevent bundling + +As a configuration parallel to `--no-bundle` (introduced in https://github.com/cloudflare/wrangler2/pull/1300 as `--no-build`, renamed in https://github.com/cloudflare/wrangler2/pull/1399 to `--no-bundle`), this introduces a configuration field `no_bundle` to prevent bundling of the worker before it's published. It's inheritable, which means it can be defined inside environments as well. diff --git a/packages/wrangler/src/__tests__/configuration.test.ts b/packages/wrangler/src/__tests__/configuration.test.ts index d78943cd793a..f233895e8906 100644 --- a/packages/wrangler/src/__tests__/configuration.test.ts +++ b/packages/wrangler/src/__tests__/configuration.test.ts @@ -62,6 +62,7 @@ describe("normalizeAndValidateConfig()", () => { data_blobs: undefined, workers_dev: undefined, zone_id: undefined, + no_bundle: undefined, minify: undefined, node_compat: undefined, }); @@ -848,6 +849,7 @@ describe("normalizeAndValidateConfig()", () => { }, ], }, + no_bundle: true, minify: true, node_compat: true, }; @@ -920,6 +922,7 @@ describe("normalizeAndValidateConfig()", () => { define: { DEF1: 1777, }, + no_bundle: "INVALID", minify: "INVALID", node_compat: "INVALID", } as unknown as RawEnvironment; @@ -986,6 +989,7 @@ describe("normalizeAndValidateConfig()", () => { - Expected \\"main\\" to be of type string but got 1333. - Expected \\"usage_model\\" field to be one of [\\"bundled\\",\\"unbound\\"] but got \\"INVALID\\". - The field \\"define.DEF1\\" should be a string but got 1777. + - Expected \\"no_bundle\\" to be of type boolean but got \\"INVALID\\". - Expected \\"minify\\" to be of type boolean but got \\"INVALID\\". - Expected \\"node_compat\\" to be of type boolean but got \\"INVALID\\"." `); @@ -2192,6 +2196,7 @@ describe("normalizeAndValidateConfig()", () => { cwd: "CWD", watch_dir: "WATCH_DIR", }, + no_bundle: true, minify: true, node_compat: true, }; @@ -2234,6 +2239,7 @@ describe("normalizeAndValidateConfig()", () => { cwd: "ENV_CWD", watch_dir: "ENV_WATCH_DIR", }, + no_bundle: false, minify: false, node_compat: false, }; @@ -2255,6 +2261,7 @@ describe("normalizeAndValidateConfig()", () => { cwd: "CWD", watch_dir: "WATCH_DIR", }, + no_bundle: true, minify: true, node_compat: true, env: { @@ -2515,6 +2522,7 @@ describe("normalizeAndValidateConfig()", () => { cwd: 1555, watch_dir: 1666, }, + no_bundle: "INVALID", minify: "INVALID", node_compat: "INVALID", } as unknown as RawEnvironment; @@ -2550,6 +2558,7 @@ describe("normalizeAndValidateConfig()", () => { - Expected \\"name\\" to be of type string, alphanumeric and lowercase with dashes only but got 111. - Expected \\"main\\" to be of type string but got 1333. - Expected \\"usage_model\\" field to be one of [\\"bundled\\",\\"unbound\\"] but got \\"INVALID\\". + - Expected \\"no_bundle\\" to be of type boolean but got \\"INVALID\\". - Expected \\"minify\\" to be of type boolean but got \\"INVALID\\". - Expected \\"node_compat\\" to be of type boolean but got \\"INVALID\\"." `); diff --git a/packages/wrangler/src/__tests__/publish.test.ts b/packages/wrangler/src/__tests__/publish.test.ts index 3c7841cd9a38..625abbb59cf2 100644 --- a/packages/wrangler/src/__tests__/publish.test.ts +++ b/packages/wrangler/src/__tests__/publish.test.ts @@ -6026,7 +6026,7 @@ addEventListener('fetch', event => {});` }); describe("--no-bundle", () => { - it("should not transform the source code before publishing it", async () => { + it("(cli) should not transform the source code before publishing it", async () => { writeWranglerToml(); const scriptContent = ` import X from '@cloudflare/no-such-package'; // let's add an import that doesn't exist @@ -6036,6 +6036,19 @@ addEventListener('fetch', event => {});` await runWrangler("publish index.js --no-bundle --dry-run --outdir dist"); expect(fs.readFileSync("dist/index.js", "utf-8")).toMatch(scriptContent); }); + + it("(config) should not transform the source code before publishing it", async () => { + writeWranglerToml({ + no_bundle: true, + }); + const scriptContent = ` + import X from '@cloudflare/no-such-package'; // let's add an import that doesn't exist + const xyz = 123; // a statement that would otherwise be compiled out + `; + fs.writeFileSync("index.js", scriptContent); + await runWrangler("publish index.js --dry-run --outdir dist"); + expect(fs.readFileSync("dist/index.js", "utf-8")).toMatch(scriptContent); + }); }); }); diff --git a/packages/wrangler/src/config/environment.ts b/packages/wrangler/src/config/environment.ts index f32d5acb135a..96f5e07bdeaf 100644 --- a/packages/wrangler/src/config/environment.ts +++ b/packages/wrangler/src/config/environment.ts @@ -183,6 +183,12 @@ interface EnvironmentInheritable { upload?: DeprecatedUpload; }; + /** + * Skip internal build steps and directly publish script + * @inheritable + */ + no_bundle: boolean | undefined; + /** * Minify the script before uploading. * @inheritable @@ -191,6 +197,7 @@ interface EnvironmentInheritable { /** * Add polyfills for node builtin modules and globals + * @inheritable */ node_compat: boolean | undefined; diff --git a/packages/wrangler/src/config/validation.ts b/packages/wrangler/src/config/validation.ts index 6ce4092023d6..449c6d496d11 100644 --- a/packages/wrangler/src/config/validation.ts +++ b/packages/wrangler/src/config/validation.ts @@ -1030,6 +1030,14 @@ function normalizeAndValidateEnvironment( } ), zone_id: rawEnv.zone_id, + no_bundle: inheritable( + diagnostics, + topLevelEnv, + rawEnv, + "no_bundle", + isBoolean, + undefined + ), minify: inheritable( diagnostics, topLevelEnv, diff --git a/packages/wrangler/src/dev.tsx b/packages/wrangler/src/dev.tsx index c35ac61ce40e..82e6b5ec5dfc 100644 --- a/packages/wrangler/src/dev.tsx +++ b/packages/wrangler/src/dev.tsx @@ -82,7 +82,6 @@ export function devOptions(yargs: Argv): Argv { .option("bundle", { describe: "Run wrangler's compilation step before publishing", type: "boolean", - default: true, hidden: true, }) .option("no-bundle", { @@ -454,7 +453,7 @@ export async function startDev(args: ArgumentsCamelCase) { return (