Skip to content

Commit

Permalink
Add header + caching to runtime type generation
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Jul 23, 2024
1 parent 3fd94e7 commit f3d6711
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
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 @@ -130,4 +130,38 @@ describe("types", () => {
`📣 It looks like you have some Node.js compatibility turned on in your project. You might want to consider adding Node.js typings with "npm i --save-dev @types/[email protected]". Please see the docs for more details: https://developers.cloudflare.com/workers/languages/typescript/#transitive-loading-of-typesnode-overrides-cloudflareworkers-types`
);
});
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,12 +49,26 @@ 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,
compatibilityFlags: compatibility_flags,
});

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 f3d6711

Please sign in to comment.