Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make wrangler types always generate a d.ts file for module worders #5087

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .changeset/weak-tools-judge.md
Original file line number Diff line number Diff line change
@@ -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 {
}
```
56 changes: 42 additions & 14 deletions packages/wrangler/src/__tests__/type-generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
11 changes: 7 additions & 4 deletions packages/wrangler/src/type-generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ function writeDTSFile({

let combinedTypeStrings = "";
if (formatType === "modules") {
combinedTypeStrings += `interface ${envInterface} {\n${envTypeStructure
.map((value) => `\t${value}`)
.join("\n")}\n}\n${modulesTypeStructure.join("\n")}`;
combinedTypeStrings += `interface ${envInterface} {${envTypeStructure
.map((value) => `\n\t${value}`)
.join("")}\n}\n${modulesTypeStructure.join("\n")}`;
} else {
combinedTypeStrings += `export {};\ndeclare global {\n${envTypeStructure
.map((value) => `\tconst ${value}`)
Expand All @@ -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,
[
Expand Down
Loading