From a7343426618930fb4ede812480b7f7cee7366b43 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Sat, 24 Feb 2024 18:07:15 +0000 Subject: [PATCH 1/2] fix: make `wrangler types` always generate a `d.ts` file for module workers --- .changeset/weak-tools-judge.md | 23 ++++++++ .../src/__tests__/type-generation.test.ts | 56 ++++++++++++++----- packages/wrangler/src/type-generation.ts | 9 ++- 3 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 .changeset/weak-tools-judge.md diff --git a/.changeset/weak-tools-judge.md b/.changeset/weak-tools-judge.md new file mode 100644 index 000000000000..e9fc0c3a6d70 --- /dev/null +++ b/.changeset/weak-tools-judge.md @@ -0,0 +1,23 @@ +--- +"wrangler": patch +--- + +fix: make `wrangler types` always generate a `d.ts` file for module workers + +Currently if a config file doesn't define any binding nor module, running +`wrangler types` against such file would not produce a `d.ts` file. + +Producing a `d.ts` file can however still be beneficial as it would define a correct +env interface (even if empty) that can be expanded/referenced by user code (this can +be particularly convenient for scaffolding tools that may want to always generate an +env interface). + +Example: +Before `wrangler types --env-interface MyEnv` run with an empty `wrangler.toml` file +would not generate any file, after these change it would instead generate a file with +the following content: + +``` +interface MyEnv { +} +``` diff --git a/packages/wrangler/src/__tests__/type-generation.test.ts b/packages/wrangler/src/__tests__/type-generation.test.ts index f813ec2ceeb4..c29ff1ee8cd7 100644 --- a/packages/wrangler/src/__tests__/type-generation.test.ts +++ b/packages/wrangler/src/__tests__/type-generation.test.ts @@ -234,21 +234,49 @@ describe("generateTypes()", () => { expect(fs.existsSync("./worker-configuration.d.ts")).toBe(true); }); - it("should not create DTS file if there is nothing in the config to generate types from", async () => { - fs.writeFileSync("./index.ts", "export default { async fetch () {} };"); - fs.writeFileSync( - "./wrangler.toml", - TOML.stringify({ - compatibility_date: "2022-01-12", - name: "test-name", - main: "./index.ts", - }), - "utf-8" - ); + describe("when nothing was found", () => { + it("should not create DTS file for service syntax workers", async () => { + fs.writeFileSync( + "./index.ts", + 'addEventListener("fetch", event => { event.respondWith(() => new Response("")); })' + ); + fs.writeFileSync( + "./wrangler.toml", + TOML.stringify({ + compatibility_date: "2022-01-12", + name: "test-name", + main: "./index.ts", + }), + "utf-8" + ); - await runWrangler("types"); - expect(fs.existsSync("./worker-configuration.d.ts")).toBe(false); - expect(std.out).toMatchInlineSnapshot(`""`); + await runWrangler("types"); + expect(fs.existsSync("./worker-configuration.d.ts")).toBe(false); + expect(std.out).toMatchInlineSnapshot(`""`); + }); + + it("should create a DTS file with an empty env interface for module syntax workers", async () => { + fs.writeFileSync("./index.ts", "export default { async fetch () {} };"); + fs.writeFileSync( + "./wrangler.toml", + TOML.stringify({ + compatibility_date: "2022-01-12", + name: "test-name", + main: "./index.ts", + }), + "utf-8" + ); + + await runWrangler("types"); + expect(fs.readFileSync("./worker-configuration.d.ts", "utf-8")).toMatch( + /interface Env \{\s*\}/ + ); + expect(std.out).toMatchInlineSnapshot(` + "interface Env { + } + " + `); + }); }); it("should create a DTS file at the location that the command is executed from", async () => { diff --git a/packages/wrangler/src/type-generation.ts b/packages/wrangler/src/type-generation.ts index aae055461407..470552cac725 100644 --- a/packages/wrangler/src/type-generation.ts +++ b/packages/wrangler/src/type-generation.ts @@ -305,8 +305,8 @@ function writeDTSFile({ let combinedTypeStrings = ""; if (formatType === "modules") { - combinedTypeStrings += `interface ${envInterface} {\n${envTypeStructure - .map((value) => `\t${value}`) + combinedTypeStrings += `interface ${envInterface} {${envTypeStructure + .map((value, i) => `${i === 0 ? "\n" : ""}\t${value}`) .join("\n")}\n}\n${modulesTypeStructure.join("\n")}`; } else { combinedTypeStrings += `export {};\ndeclare global {\n${envTypeStructure @@ -316,7 +316,10 @@ function writeDTSFile({ const wranglerCommandUsed = ["wrangler", ...process.argv.slice(2)].join(" "); - if (envTypeStructure.length || modulesTypeStructure.length) { + const typesHaveBeenFound = + envTypeStructure.length || modulesTypeStructure.length; + + if (formatType === "modules" || typesHaveBeenFound) { fs.writeFileSync( path, [ From 9a9974dbcb299af9bb3833fe120da84cce4b0853 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Mon, 26 Feb 2024 17:52:28 +0000 Subject: [PATCH 2/2] Update packages/wrangler/src/type-generation.ts Co-authored-by: Pete Bacon Darwin --- packages/wrangler/src/type-generation.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/wrangler/src/type-generation.ts b/packages/wrangler/src/type-generation.ts index 470552cac725..54b85228add7 100644 --- a/packages/wrangler/src/type-generation.ts +++ b/packages/wrangler/src/type-generation.ts @@ -306,8 +306,8 @@ function writeDTSFile({ let combinedTypeStrings = ""; if (formatType === "modules") { combinedTypeStrings += `interface ${envInterface} {${envTypeStructure - .map((value, i) => `${i === 0 ? "\n" : ""}\t${value}`) - .join("\n")}\n}\n${modulesTypeStructure.join("\n")}`; + .map((value) => `\n\t${value}`) + .join("")}\n}\n${modulesTypeStructure.join("\n")}`; } else { combinedTypeStrings += `export {};\ndeclare global {\n${envTypeStructure .map((value) => `\tconst ${value}`)