Skip to content

Commit 8fc76f5

Browse files
committed
pr feedback
1 parent 3336a7b commit 8fc76f5

File tree

5 files changed

+76
-22
lines changed

5 files changed

+76
-22
lines changed

.changeset/nasty-kiwis-talk.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
"wrangler": minor
33
---
44

5-
Include runtime types in the output of `wrangler types`
5+
Include runtime types in the output of `wrangler types` by default
66

77
`wrangler types` will now produce one file that contains both `Env` types and runtime types based on your compatibility date and flags. This is located at `worker-configuration.d.ts` by default.
88

9-
This behaviour was previously gated behind `--experimental-include-runtime`. That flag is no longer necessary and has been removed. It has been replaced by `--include-runtime` and `include-env`, both of which are set to `true` by default.
9+
This behaviour was previously gated behind `--experimental-include-runtime`. That flag is no longer necessary and has been removed. It has been replaced by `--include-runtime` and `--include-env`, both of which are set to `true` by default. If you were previously using `--x-include-runtime`, you can drop that flag and remove the separate `runtime.d.ts` file.
10+
11+
If you were previously using `@cloudflare/workers-types` we recommend you run uninstall and run `wrangler types` instead. Note that `@cloudflare/workers-types` will continue to be published without any changes.

packages/wrangler/e2e/types.test.ts

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { readFileSync } from "node:fs";
2-
import { readFile, writeFile } from "node:fs/promises";
2+
import { writeFile } from "node:fs/promises";
33
import path from "node:path";
44
import { describe, expect, it } from "vitest";
55
import { dedent } from "../src/utils/dedent";
@@ -11,6 +11,8 @@ const seed = {
1111
main = "src/index.ts"
1212
compatibility_date = "2023-01-01"
1313
compatibility_flags = ["nodejs_compat", "no_global_navigator"]
14+
[vars]
15+
MY_VAR = "my-var-value"
1416
`,
1517
"src/index.ts": dedent`
1618
export default {
@@ -61,20 +63,66 @@ describe("types", () => {
6163
expect(file).contains("interface Env");
6264
});
6365

66+
it("should be able to generate an Env type only", async () => {
67+
const helper = new WranglerE2ETestHelper();
68+
await helper.seed(seed);
69+
const output = await helper.run(`wrangler types --include-runtime=false`);
70+
expect(output.stdout).not.toContain("Generating runtime types...");
71+
const file = readFileSync(
72+
path.join(helper.tmpPath, "./worker-configuration.d.ts"),
73+
"utf8"
74+
);
75+
expect(file).toMatchInlineSnapshot(`
76+
"// Generated by Wrangler by running \`wrangler types --include-runtime=false\` (hash: 0def6160a7296b129655a838f6922e65)
77+
interface Env {
78+
MY_VAR: "my-var-value";
79+
}
80+
"
81+
`);
82+
});
83+
6484
it("should include header with version information in the generated types", async () => {
6585
const helper = new WranglerE2ETestHelper();
6686
await helper.seed(seed);
6787
await helper.run(`wrangler types "./types.d.ts" `);
6888

69-
const file = (await readFile(path.join(helper.tmpPath, "./types.d.ts")))
70-
.toString()
71-
.split("\n");
89+
const lines = readFileSync(
90+
path.join(helper.tmpPath, "./types.d.ts"),
91+
"utf8"
92+
).split("\n");
93+
94+
expect(lines[0]).toMatchInlineSnapshot(
95+
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\` (hash: 0def6160a7296b129655a838f6922e65)"`
96+
);
97+
expect(lines[1]).match(
98+
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/
99+
);
100+
});
101+
102+
it("should include header with wrangler command that generated it", async () => {
103+
const helper = new WranglerE2ETestHelper();
104+
await helper.seed({
105+
...seed,
106+
"wranglerA.toml": dedent`
107+
name = "test-worker"
108+
main = "src/index.ts"
109+
compatibility_date = "2023-01-01"
110+
`,
111+
});
112+
await helper.run(
113+
"wrangler types -c wranglerA.toml --env-interface MyCloudflareEnv ./cflare-env.d.ts"
114+
);
115+
116+
const lines = readFileSync(
117+
path.join(helper.tmpPath, "./cflare-env.d.ts"),
118+
"utf8"
119+
).split("\n");
72120

73-
expect(file[0]).toMatchInlineSnapshot(
74-
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\` (hash: e82ba4d7b995dd9ca6fb0332d81f889b)"`
121+
expect(lines[0]).toMatchInlineSnapshot(
122+
`"// Generated by Wrangler by running \`wrangler types -c wranglerA.toml --env-interface MyCloudflareEnv ./cflare-env.d.ts\` (hash: 7b21143a37ab4f704c94ef59fd186bf8)"`
75123
);
76-
expect(file[1]).match(
77-
/\/\/ Runtime types generated with workerd@1\.\d+\.\d \d\d\d\d-\d\d-\d\d ([a-z_]+,?)*/
124+
expect(lines[1]).match(
125+
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/
78126
);
79127
});
80128

@@ -84,7 +132,7 @@ describe("types", () => {
84132
await helper.run(`wrangler types`);
85133

86134
const typesPath = path.join(helper.tmpPath, "worker-configuration.d.ts");
87-
const file = (await readFile(typesPath)).toString().split("\n");
135+
const file = readFileSync(typesPath, "utf8").split("\n");
88136

89137
await writeFile(
90138
typesPath,
@@ -99,7 +147,7 @@ describe("types", () => {
99147

100148
await helper.run(`wrangler types`);
101149

102-
const file2 = (await readFile(typesPath)).toString();
150+
const file2 = readFileSync(typesPath, "utf8");
103151

104152
// regenerates env types
105153
expect(file2).toContain("interface Env {");

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@ import { generateEnvTypes } from ".";
55
import type { Config } from "../config";
66
import type { Entry } from "../deployment-bundle/entry";
77

8+
// Checks the default location for a generated types file and compares if the
9+
// recorded Env hash, workerd version or compat date and flags have changed
10+
// compared to the current values in the config. Prompts user to re-run wrangler
11+
// types if so.
812
export const checkTypesDiff = async (config: Config, entry: Entry) => {
913
if (!entry.file.endsWith(".ts")) {
1014
return;
1115
}
12-
let maybeExistingTypesFile: string[];
16+
let maybeExistingTypesFileLines: string[];
1317
try {
14-
// Note: this checks the default location only
15-
maybeExistingTypesFile = readFileSync(
18+
// Checking the default location only
19+
maybeExistingTypesFileLines = readFileSync(
1620
"./worker-configuration.d.ts",
1721
"utf-8"
1822
).split("\n");
1923
} catch {
2024
return;
2125
}
22-
const existingEnvHeader = maybeExistingTypesFile.find((line) =>
26+
const existingEnvHeader = maybeExistingTypesFileLines.find((line) =>
2327
line.startsWith("// Generated by Wrangler by running")
2428
);
2529
const maybeExistingHash =
@@ -49,7 +53,7 @@ export const checkTypesDiff = async (config: Config, entry: Entry) => {
4953

5054
const newHash = newEnvHeader?.match(/hash: (?<hash>.*)\)/)?.groups?.hash;
5155

52-
const existingRuntimeHeader = maybeExistingTypesFile.find((line) =>
56+
const existingRuntimeHeader = maybeExistingTypesFileLines.find((line) =>
5357
line.startsWith("// Runtime types generated with")
5458
);
5559
const newRuntimeHeader = `// Runtime types generated with workerd@${version} ${config.compatibility_date} ${config.compatibility_flags.sort().join(",")}`;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ export async function generateRuntimeTypes({
4949
const header = `// Runtime types generated with workerd@${version} ${compatibility_date} ${compatibility_flags.sort().join(",")}`;
5050

5151
try {
52-
const file = (await readFile(outFile, "utf8")).split("\n");
53-
const existingHeader = file.find((line) =>
52+
const lines = (await readFile(outFile, "utf8")).split("\n");
53+
const existingHeader = lines.find((line) =>
5454
line.startsWith("// Runtime types generated with workerd@")
5555
);
56-
const existingTypesStart = file.findIndex(
56+
const existingTypesStart = lines.findIndex(
5757
(line) => line === "// Begin runtime types"
5858
);
5959
if (existingHeader === header && existingTypesStart !== -1) {
6060
logger.debug("Using cached runtime types: ", header);
6161

6262
return {
6363
runtimeHeader: header,
64-
runtimeTypes: file.slice(existingTypesStart + 1).join("\n"),
64+
runtimeTypes: lines.slice(existingTypesStart + 1).join("\n"),
6565
};
6666
}
6767
} catch (e) {

packages/wrangler/src/type-generation/runtime/log-runtime-types-message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function logRuntimeTypesMessage(
5454
logAction("Install types@node");
5555
logger.log(
5656
chalk.dim(
57-
`Since you have the \`nodejs_compat\` flag, you should install Node.js types by running "npm i --save-dev @types/node${isWorkersTypesInstalled ? '@20.8.3".\nFor more details: https://developers.cloudflare.com/workers/languages/typescript/#known-issues' : '".'}`
57+
`Since you have the \`nodejs_compat\` flag, you should install Node.js types by running "npm i --save-dev ${isWorkersTypesInstalled ? '@types/node@20.8.3".\nFor more details: https://developers.cloudflare.com/workers/languages/typescript/#known-issues' : '@types/node".'}`
5858
)
5959
);
6060
logger.log("");

0 commit comments

Comments
 (0)