Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate types for the next experimental template #7756

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

vicb
Copy link
Contributor

@vicb vicb commented Jan 14, 2025

Instead of copying an empty cloudflare-env.d.ts, the file is generated when the template is configured.

Then it contains the existing bindgins:

// Generated by Wrangler by running `wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts`

interface CloudflareEnv {
	NEXTJS_ENV: string;
	ASSETS: Fetcher;
}

Notes:

  • I've moved the call to transformPackageJson before configure so that the latest can execute the added scripts
  • If this get merged, we probably should update other templates (we should check usesTypescript(...) for template supporting JS).

  • Tests
    • TODO (before merge)
    • Tests included
    • Tests not necessary because: already tested & checked locally
  • E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required
    • Not required because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Documentation not necessary because: not user facing change

@vicb vicb requested review from a team as code owners January 14, 2025 09:33
Copy link

changeset-bot bot commented Jan 14, 2025

⚠️ No Changeset found

Latest commit: 21c4d55

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vicb vicb added the e2e Run e2e tests on a PR label Jan 14, 2025
Copy link
Contributor

github-actions bot commented Jan 14, 2025

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-wrangler-7756

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/7756/npm-package-wrangler-7756

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-wrangler-7756 dev path/to/script.js
Additional artifacts:

cloudflare-workers-bindings-extension:

wget https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-cloudflare-workers-bindings-extension-7756 -O ./cloudflare-workers-bindings-extension.0.0.0-v08528926a.vsix && code --install-extension ./cloudflare-workers-bindings-extension.0.0.0-v08528926a.vsix

create-cloudflare:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-create-cloudflare-7756 --no-auto-update

@cloudflare/kv-asset-handler:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-cloudflare-kv-asset-handler-7756

miniflare:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-miniflare-7756

@cloudflare/pages-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-cloudflare-pages-shared-7756

@cloudflare/unenv-preset:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-cloudflare-unenv-preset-7756

@cloudflare/vitest-pool-workers:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-cloudflare-vitest-pool-workers-7756

@cloudflare/workers-editor-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-cloudflare-workers-editor-shared-7756

@cloudflare/workers-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-cloudflare-workers-shared-7756

@cloudflare/workflows-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12765538678/npm-package-cloudflare-workflows-shared-7756

Note that these links will no longer work once the GitHub Actions artifact expires.


[email protected] includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20241230.1
workerd 1.20241230.0 1.20241230.0
workerd --version 1.20241230.0 2024-12-30

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this more simply?

@@ -27,6 +32,14 @@ const configure = async () => {
startText: "Adding the Cloudflare adapter",
doneText: `${brandColor(`installed`)} ${dim(packages.join(", "))}`,
});

const { npm } = detectPackageManager();
await runCommand([npm, "run", NPM_TYPE_GEN_SCRIPT], {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like a simpler change that doesn't require changes to how C3 hooks are run, is just to put the command that generates the types into a variable. Then run it here and use it to update the package.json below.

E.g.

Suggested change
await runCommand([npm, "run", NPM_TYPE_GEN_SCRIPT], {
const GENERATE_TYPES = ["wrangler", "types", "--env-interface", "CloudflareEnv", "cloudflare-env.d.ts"];
await runCommand([npx, ...GENERATE_TYPES], {

Copy link
Contributor Author

@vicb vicb Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work too but I prefer using the actual script.

Do you think that calling updatePackageScripts() before configure might ever cause issues?

(One of the reason is that we have hacks for yarn that does not support npx)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the benefit in making this change to how C3 hooks run when a simple change to the c3.ts file would suffice. The update to the package.json is simply to make user facing scripts ready to go, not to support internal scripts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. What I mentionned

// yarn cannot `yarn create@some-version` and doesn't have an npx equivalent
// So to retain the ability to lock versions we run it with `npx` and spoof
// the user agent so scaffolding tools treat the invocation like yarn
const cmd = [...(npm === "yarn" ? ["npx"] : dlx), cli, ...args];
const env = npm === "yarn" ? { npm_config_user_agent: "yarn" } : {};

The update to the package.json is simply to make user facing scripts ready to go, not to support internal scripts.

the .d.ts is supposed to be generated/updated by running the "user facing script", that is what this PR implements.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That comment is not relevant here, I believe. We are not running the CLI generator script in this command, which has to be run outside of a node package.
Here we are just running wrangler types command inside the package, which works fine with yarn.

@@ -49,7 +62,7 @@ export default {
scripts: {
deploy: `opennextjs-cloudflare && wrangler deploy`,
preview: `opennextjs-cloudflare && wrangler dev`,
"cf-typegen": `wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts`,
[NPM_TYPE_GEN_SCRIPT]: `wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[NPM_TYPE_GEN_SCRIPT]: `wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts`,
"cf-typegen: GENERATE_TYPES.join(" "),

Ironically the tests for "// Generated by wrangler" was passing for files not generated by wrangler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e Run e2e tests on a PR no-changeset-required
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

2 participants