-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add header + caching to runtime type generation
- Loading branch information
Showing
4 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
@@ -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 { | ||
|
@@ -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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |