Skip to content

Commit e1df3d8

Browse files
emily-shenvicb
andauthored
use generated types in c3 (#8886)
* add option to use generated types * update templates * update tests * changeset * fix next test * don't conflate installing workers-types and generating types * next on pages * pr feedback * fixups * f * use workersTypes enum instead * set env interface and types path in one place * set defaults in test too * pr feedback * update tests * tidy up * Update packages/create-cloudflare/src/workers.ts Co-authored-by: Victor Berchet <[email protected]> --------- Co-authored-by: Victor Berchet <[email protected]>
1 parent 53ba97d commit e1df3d8

File tree

72 files changed

+364
-375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+364
-375
lines changed

.changeset/slimy-eels-wink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-cloudflare": minor
3+
---
4+
5+
use `wrangler types` to generate types instead of using the `@cloudflare/workers-types` package

packages/create-cloudflare/e2e-tests/frameworks.test.ts

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { detectPackageManager } from "helpers/packageManagers";
1313
import { retry } from "helpers/retry";
1414
import { sleep } from "helpers/sleep";
15+
import * as jsonc from "jsonc-parser";
1516
import { fetch } from "undici";
1617
import { beforeAll, describe, expect } from "vitest";
1718
import { deleteProject, deleteWorker } from "../scripts/common";
@@ -41,12 +42,9 @@ import type { Writable } from "stream";
4142

4243
type FrameworkTestConfig = RunnerConfig & {
4344
testCommitMessage: boolean;
45+
nodeCompat: boolean;
4446
unsupportedPms?: string[];
4547
unsupportedOSs?: string[];
46-
verifyBuildCfTypes?: {
47-
outputFile: string;
48-
envInterfaceName: string;
49-
};
5048
verifyBuild?: {
5149
outputDir: string;
5250
script: string;
@@ -80,8 +78,12 @@ describe.concurrent(
8078
});
8179

8280
Object.entries(frameworkTests).forEach(([frameworkKey, testConfig]) => {
83-
const frameworkConfig = getFrameworkConfig(frameworkKey);
84-
81+
const frameworkConfig = {
82+
workersTypes: "generated" as const,
83+
typesPath: "worker-configuration.d.ts",
84+
envInterfaceName: "Env",
85+
...getFrameworkConfig(frameworkKey),
86+
};
8587
test({ experimental }).runIf(
8688
shouldRunTest(frameworkConfig.id, testConfig),
8789
)(
@@ -176,7 +178,8 @@ describe.concurrent(
176178
project.path,
177179
logStream,
178180
);
179-
await verifyBuildCfTypesScript(testConfig, project.path, logStream);
181+
182+
await verifyTypes(testConfig, frameworkConfig, project.path);
180183
await verifyBuildScript(testConfig, project.path, logStream);
181184
} catch (e) {
182185
console.error("ERROR", e);
@@ -352,57 +355,53 @@ const verifyPreviewScript = async (
352355
}
353356
};
354357

355-
const verifyBuildCfTypesScript = async (
356-
{ verifyBuildCfTypes }: FrameworkTestConfig,
358+
const verifyTypes = async (
359+
{ nodeCompat }: FrameworkTestConfig,
360+
{
361+
workersTypes,
362+
typesPath = "./worker-configuration.d.ts",
363+
envInterfaceName = "Env",
364+
}: TemplateConfig,
357365
projectPath: string,
358-
logStream: Writable,
359366
) => {
360-
if (!verifyBuildCfTypes) {
367+
if (workersTypes === "none") {
361368
return;
362369
}
363370

364-
const { outputFile, envInterfaceName } = verifyBuildCfTypes;
365-
366-
const outputFileContentPre = readFile(join(projectPath, outputFile));
367-
const outputFileContentPreLines = outputFileContentPre.split("\n");
371+
const outputFileContent = readFile(join(projectPath, typesPath)).split("\n");
368372

369-
// the file contains the "Generated by Wrangler" comment without a timestamp
370-
expect(outputFileContentPreLines).toContain("// Generated by Wrangler");
371-
372-
// the file contains the env interface
373-
// the file still contains the env interface
374-
const hasEnvInterfacePre = outputFileContentPreLines.some(
373+
const hasEnvInterface = outputFileContent.some(
375374
(line) =>
376375
// old type gen - some framework templates pin older versions of wrangler
377376
line === `interface ${envInterfaceName} {` ||
378377
// new after importable env change
379378
line === `interface ${envInterfaceName} extends Cloudflare.Env {}`,
380379
);
381-
expect(hasEnvInterfacePre).toBe(true);
382-
383-
// Run the `cf-typegen` script to generate types for bindings in fixture
384-
const buildTypesProc = spawnWithLogging(
385-
[pm, "run", "cf-typegen"],
386-
{ cwd: projectPath },
387-
logStream,
388-
);
389-
await waitForExit(buildTypesProc);
390-
391-
const outputFileContentPost = readFile(join(projectPath, outputFile));
392-
const outputFileContentPostLines = outputFileContentPost.split("\n");
380+
expect(hasEnvInterface).toBe(true);
393381

394-
// the file doesn't contain the "Generated by Wrangler" comment anymore
395-
expect(outputFileContentPostLines).not.toContain("// Generated by Wrangler");
382+
// if the runtime types were installed, they wont be in this file
383+
if (workersTypes === "generated") {
384+
expect(outputFileContent[2]).match(
385+
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/,
386+
);
387+
}
396388

397-
// the file still contains the env interface
398-
const hasEnvInterfacePost = outputFileContentPostLines.some(
399-
(line) =>
400-
// old type gen - some framework templates pin older versions of wrangler
401-
line === `interface ${envInterfaceName} {` ||
402-
// new after importable env change
403-
line === `interface ${envInterfaceName} extends Cloudflare.Env {}`,
404-
);
405-
expect(hasEnvInterfacePost).toBe(true);
389+
const tsconfigPath = join(projectPath, "tsconfig.json");
390+
const tsconfigTypes = jsonc.parse(readFile(tsconfigPath)).compilerOptions
391+
?.types;
392+
if (workersTypes === "generated") {
393+
expect(tsconfigTypes).toContain(typesPath);
394+
}
395+
if (workersTypes === "installed") {
396+
expect(
397+
tsconfigTypes.some((x: string) =>
398+
x.includes("@cloudflare/workers-types"),
399+
),
400+
).toBe(true);
401+
}
402+
if (nodeCompat) {
403+
expect(tsconfigTypes).toContain(`node`);
404+
}
406405
};
407406

408407
const verifyBuildScript = async (

packages/create-cloudflare/e2e-tests/frameworks/framework-test-config-experimental.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default function getFrameworkTestConfigExperimental() {
2525
route: "/",
2626
expectedText: "Hello world",
2727
},
28+
nodeCompat: true,
2829
},
2930
};
3031
}

0 commit comments

Comments
 (0)