Skip to content

Commit f76da43

Browse files
authored
Support ctx.exports in wrangler types (#10676)
* Support ctx.exports in wrangler types * Create green-penguins-compare.md * fix e2es
1 parent b59e3e1 commit f76da43

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Support `ctx.exports` in wrangler types

packages/wrangler/e2e/types.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ describe("types", () => {
7474
);
7575
expect(file).toMatchInlineSnapshot(`
7676
"/* eslint-disable */
77-
// Generated by Wrangler by running \`wrangler types --include-runtime=false\` (hash: 7915eccca244b8d5c107e358ee5929e8)
77+
// Generated by Wrangler by running \`wrangler types --include-runtime=false\` (hash: 5c82572f95137bbfb775d02fdf441070)
7878
declare namespace Cloudflare {
79+
interface GlobalProps {
80+
mainModule: typeof import("./src/index");
81+
}
7982
interface Env {
8083
MY_VAR: "my-var-value";
8184
}
@@ -96,7 +99,7 @@ describe("types", () => {
9699
).split("\n");
97100

98101
expect(lines[1]).toMatchInlineSnapshot(
99-
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\` (hash: 7915eccca244b8d5c107e358ee5929e8)"`
102+
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\` (hash: 5c82572f95137bbfb775d02fdf441070)"`
100103
);
101104
expect(lines[2]).match(
102105
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/
@@ -123,7 +126,7 @@ describe("types", () => {
123126
).split("\n");
124127

125128
expect(lines[1]).toMatchInlineSnapshot(
126-
`"// Generated by Wrangler by running \`wrangler types -c wranglerA.toml --env-interface MyCloudflareEnv ./cflare-env.d.ts\` (hash: fb913b8eb739817c9664fa1da8bdf499)"`
129+
`"// Generated by Wrangler by running \`wrangler types -c wranglerA.toml --env-interface MyCloudflareEnv ./cflare-env.d.ts\` (hash: e981fccb455c04c58ced00f3442b83ac)"`
127130
);
128131
expect(lines[2]).match(
129132
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/
@@ -205,8 +208,11 @@ describe("types", () => {
205208
);
206209
expect(file).toMatchInlineSnapshot(`
207210
"/* eslint-disable */
208-
// Generated by Wrangler by running \`wrangler types --include-runtime=false\` (hash: e5830092ae2a4ea3789f0d1c1fc31ff8)
211+
// Generated by Wrangler by running \`wrangler types --include-runtime=false\` (hash: a5369fe39c0909514a78cea884a50943)
209212
declare namespace Cloudflare {
213+
interface GlobalProps {
214+
mainModule: typeof import("./src/index");
215+
}
210216
interface Env {
211217
BEEP: "BOOP";
212218
ASDf: "ADSfadsf";

packages/wrangler/src/__tests__/type-generation.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ const bindingsConfigMock: Omit<
132132
},
133133
],
134134
},
135+
migrations: [
136+
{
137+
tag: "v1",
138+
new_classes: ["RandomDo"],
139+
},
140+
{
141+
tag: "v2",
142+
new_sqlite_classes: ["DurableReexport"],
143+
renamed_classes: [
144+
{
145+
from: "RandomDo",
146+
to: "DurableDirect",
147+
},
148+
],
149+
},
150+
],
135151
workflows: [],
136152
containers: undefined,
137153
r2_buckets: [
@@ -444,6 +460,10 @@ describe("generate types", () => {
444460
"Generating project types...
445461
446462
declare namespace Cloudflare {
463+
interface GlobalProps {
464+
mainModule: typeof import(\\"./index\\");
465+
durableNamespaces: \\"DurableDirect\\" | \\"DurableReexport\\";
466+
}
447467
interface Env {
448468
TEST_KV_NAMESPACE: KVNamespace;
449469
SOMETHING: \\"asdasdfasdf\\";
@@ -538,6 +558,10 @@ describe("generate types", () => {
538558
"Generating project types...
539559
540560
declare namespace Cloudflare {
561+
interface GlobalProps {
562+
mainModule: typeof import(\\"./index\\");
563+
durableNamespaces: \\"DurableDirect\\" | \\"DurableReexport\\";
564+
}
541565
interface Env {
542566
TEST_KV_NAMESPACE: KVNamespace;
543567
SOMETHING: \\"asdasdfasdf\\";
@@ -697,6 +721,10 @@ describe("generate types", () => {
697721
Generating project types...
698722
699723
declare namespace Cloudflare {
724+
interface GlobalProps {
725+
mainModule: typeof import(\\"./index\\");
726+
durableNamespaces: \\"DurableDirect\\" | \\"DurableReexport\\";
727+
}
700728
interface Env {
701729
TEST_KV_NAMESPACE: KVNamespace;
702730
SOMETHING: \\"asdasdfasdf\\";
@@ -784,9 +812,12 @@ describe("generate types", () => {
784812
expect(fs.readFileSync("./worker-configuration.d.ts", "utf-8"))
785813
.toMatchInlineSnapshot(`
786814
"/* eslint-disable */
787-
// Generated by Wrangler by running \`wrangler\` (hash: 9dfd5cc18984b8cc3421a9e726587833)
815+
// Generated by Wrangler by running \`wrangler\` (hash: d172d29d98306b9c96cfcaf16aef9056)
788816
// Runtime types generated with workerd@
789817
declare namespace Cloudflare {
818+
interface GlobalProps {
819+
mainModule: typeof import(\\"./index\\");
820+
}
790821
interface Env {
791822
SOMETHING: \\"asdasdfasdf\\";
792823
ANOTHER: \\"thing\\";
@@ -890,6 +921,9 @@ describe("generate types", () => {
890921
"Generating project types...
891922
892923
declare namespace Cloudflare {
924+
interface GlobalProps {
925+
mainModule: typeof import(\\"./index\\");
926+
}
893927
interface Env {
894928
}
895929
}

packages/wrangler/src/type-generation/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from "../config";
1212
import { createCommand } from "../core/create-command";
1313
import { getEntry } from "../deployment-bundle/entry";
14+
import { getClassNamesWhichUseSQLite } from "../dev/class-names-sqlite";
1415
import { getVarsForDev } from "../dev/dev-vars";
1516
import { CommandLineArgsError, UserError } from "../errors";
1617
import { logger } from "../logger";
@@ -658,7 +659,11 @@ export async function generateEnvTypes(
658659
modulesTypeStructure,
659660
stringKeys,
660661
config.compatibility_date,
661-
config.compatibility_flags
662+
config.compatibility_flags,
663+
entrypoint
664+
? generateImportSpecifier(fullOutputPath, entrypoint.file)
665+
: undefined,
666+
[...getClassNamesWhichUseSQLite(config.migrations).keys()]
662667
);
663668
const hash = createHash("sha256")
664669
.update(consoleOutput)
@@ -713,7 +718,9 @@ function generateTypeStrings(
713718
modulesTypeStructure: string[],
714719
stringKeys: string[],
715720
compatibilityDate: string | undefined,
716-
compatibilityFlags: string[] | undefined
721+
compatibilityFlags: string[] | undefined,
722+
entrypointModule: string | undefined,
723+
configuredDurableObjects: string[]
717724
): { fileContent: string; consoleOutput: string } {
718725
let baseContent = "";
719726
let processEnv = "";
@@ -726,7 +733,7 @@ function generateTypeStrings(
726733
// StringifyValues ensures that json vars are correctly types as strings, not objects on process.env
727734
processEnv = `\ntype StringifyValues<EnvType extends Record<string, unknown>> = {\n\t[Binding in keyof EnvType]: EnvType[Binding] extends string ? EnvType[Binding] : string;\n};\ndeclare namespace NodeJS {\n\tinterface ProcessEnv extends StringifyValues<Pick<Cloudflare.Env, ${stringKeys.map((k) => `"${k}"`).join(" | ")}>> {}\n}`;
728735
}
729-
baseContent = `declare namespace Cloudflare {\n\tinterface Env {${envTypeStructure.map((value) => `\n\t\t${value}`).join("")}\n\t}\n}\ninterface ${envInterface} extends Cloudflare.Env {}${processEnv}`;
736+
baseContent = `declare namespace Cloudflare {${entrypointModule ? `\n\tinterface GlobalProps {\n\t\tmainModule: typeof import("${entrypointModule}");${configuredDurableObjects.length > 0 ? `\n\t\tdurableNamespaces: ${configuredDurableObjects.map((d) => `"${d}"`).join(" | ")};` : ""}\n\t}` : ""}\n\tinterface Env {${envTypeStructure.map((value) => `\n\t\t${value}`).join("")}\n\t}\n}\ninterface ${envInterface} extends Cloudflare.Env {}${processEnv}`;
730737
} else {
731738
baseContent = `export {};\ndeclare global {\n${envTypeStructure.map((value) => `\tconst ${value}`).join("\n")}\n}`;
732739
}

0 commit comments

Comments
 (0)