Skip to content

Commit

Permalink
Add header + caching to runtime type generation (#6329)
Browse files Browse the repository at this point in the history
* Add header + caching to runtime type generation

* Create perfect-pens-work.md

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

Co-authored-by: Andy Jessop <andrewdjessop@gmail.com>

---------

Co-authored-by: Andy Jessop <andrewdjessop@gmail.com>
  • Loading branch information
penalosa and andyjessop authored Sep 16, 2024
1 parent fc86af2 commit c135de4
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-pens-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

chore: Cache generated runtime types
38 changes: 36 additions & 2 deletions packages/wrangler/e2e/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { dedent } from "../src/utils/dedent";
Expand All @@ -10,7 +10,7 @@ const seed = {
name = "test-worker"
main = "src/index.ts"
compatibility_date = "2023-01-01"
compatibility_flags = ["nodejs_compat"]
compatibility_flags = ["nodejs_compat", "no_global_navigator"]
`,
"src/index.ts": dedent`
export default {
Expand Down Expand Up @@ -150,4 +150,38 @@ describe("types", () => {
expect(output.stderr).toBe("");
expect(output.status).toBe(0);
});
it("should include header with version information in the generated types", async () => {
const helper = new WranglerE2ETestHelper();
await helper.seed(seed);
await helper.run(`wrangler types --x-include-runtime="./types.d.ts"`);

const file = (
await readFile(path.join(helper.tmpPath, "./types.d.ts"))
).toString();

expect(file.split("\n")[0]).match(
/\/\/ Runtime types generated with workerd@1\.\d+\.\d \d\d\d\d-\d\d-\d\d ([a-z_]+,?)*/
);
});
it("should not regenerate types if the header matches", async () => {
const helper = new WranglerE2ETestHelper();
await helper.seed(seed);
await helper.run(`wrangler types --x-include-runtime`);

const runtimeTypesFile = path.join(
helper.tmpPath,
"./.wrangler/types/runtime.d.ts"
);
const file = (await readFile(runtimeTypesFile)).toString();

const header = file.split("\n")[0];

await writeFile(runtimeTypesFile, header + "\n" + "SOME_RANDOM_DATA");

await helper.run(`wrangler types --x-include-runtime`);

const file2 = (await readFile(runtimeTypesFile)).toString();

expect(file2.split("\n")[1]).toBe("SOME_RANDOM_DATA");
});
});
3 changes: 1 addition & 2 deletions packages/wrangler/src/type-generation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,7 @@ function writeDTSFile({
fs.writeFileSync(
path,
[
`// Generated by Wrangler on ${new Date()}`,
`// by running \`${wranglerCommandUsed}\``,
`// Generated by Wrangler by running \`${wranglerCommandUsed}\``,
"",
combinedTypeStrings,
].join("\n")
Expand Down
20 changes: 18 additions & 2 deletions packages/wrangler/src/type-generation/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { readFileSync } from "fs";
import { writeFile } from "fs/promises";
import { readFile, writeFile } from "fs/promises";
import { Miniflare } from "miniflare";
import { version } from "workerd";
import { logger } from "../../logger";
import { ensureDirectoryExists } from "../../utils/filesystem";
import type { Config } from "../../config/config";

Expand Down Expand Up @@ -47,6 +49,20 @@ export async function generateRuntimeTypes({

await ensureDirectoryExists(outFile);

const header = `// Runtime types generated with workerd@${version} ${compatibility_date} ${compatibility_flags.join(",")}`;

try {
const existingTypes = await readFile(outFile, "utf8");
if (existingTypes.split("\n")[0] === header) {
logger.debug("Using cached runtime types: ", header);
return { outFile };
}
} catch (e) {
if ((e as { code: string }).code !== "ENOENT") {
throw e;
}
}

const types = await generate({
compatibilityDate: compatibility_date,
// Ignore nodejs compat flags as there is currently no mechanism to generate these dynamically.
Expand All @@ -55,7 +71,7 @@ export async function generateRuntimeTypes({
),
});

await writeFile(outFile, types, "utf8");
await writeFile(outFile, `${header}\n${types}`, "utf8");

return {
outFile,
Expand Down
6 changes: 6 additions & 0 deletions packages/wrangler/src/workerd.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module "workerd" {
const path: string;
export default path;
export const compatibilityDate: string;
export const version: string;
}

0 comments on commit c135de4

Please sign in to comment.