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

C3: add vite+react templates with tests #8013

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

Conversation

petebacondarwin
Copy link
Contributor

@petebacondarwin petebacondarwin commented Feb 3, 2025

Fixes #0000

Adds experimental C3 templates for a React SPA using the new Cloudflare Vite plugin rather than Wrangler for local development.


  • Tests
    • TODO (before merge)
    • Tests included
    • Tests not necessary because:
  • E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required
    • Not required because: only adds a C3 template
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Documentation not necessary because: experimental templates are not documented

Copy link

changeset-bot bot commented Feb 3, 2025

🦋 Changeset detected

Latest commit: fb1da7a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
create-cloudflare Patch

Not sure what this means? Click here to learn what changesets are.

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

Copy link
Contributor

github-actions bot commented Feb 3, 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/13458024202/npm-package-wrangler-8013

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

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

Or you can use npx with this latest build directly:

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

cloudflare-workers-bindings-extension:

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

create-cloudflare:

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

@cloudflare/kv-asset-handler:

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

miniflare:

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

@cloudflare/pages-shared:

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

@cloudflare/unenv-preset:

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

@cloudflare/vite-plugin:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13458024202/npm-package-cloudflare-vite-plugin-8013

@cloudflare/vitest-pool-workers:

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

@cloudflare/workers-editor-shared:

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

@cloudflare/workers-shared:

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

@cloudflare/workflows-shared:

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

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.20250214.0
workerd 1.20250214.0 1.20250214.0
workerd --version 1.20250214.0 2025-02-14

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

@petebacondarwin petebacondarwin force-pushed the pbd/add-vite-c3-templates branch 2 times, most recently from 6e2abcc to 25eed83 Compare February 18, 2025 16:27
@petebacondarwin petebacondarwin changed the title Pbd/add vite c3 templates C3: add vite+react templates with tests Feb 18, 2025
@petebacondarwin petebacondarwin marked this pull request as ready for review February 18, 2025 16:31
@petebacondarwin petebacondarwin requested review from a team as code owners February 18, 2025 16:31
@@ -107,6 +107,8 @@ function getFrameworkTests(opts: {
input: [keys.enter],
},
],
unsupportedOSs: ["win32"],
unsupportedPms: ["yarn"],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Create-vite basically tries to parse the npm_config_user_agent environment variable - expecting it to look like: yarn/1.22.22 node/20.0.0 etc - to grab the version of yarn.

Because we are running create-cloudflare via pnpx and "faking" the package manager it doesn't work. Something (I think the bit of pnpx/npx that actually creates the child process) is stripping the version so npm_config_user_agent ends up just looking like: yarn (no version).

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think there's any way to fix this?

@@ -25,7 +25,7 @@ export const detectPackageManager = () => {
if (process.env.TEST_PM && process.env.TEST_PM_VERSION) {
name = process.env.TEST_PM as PmName;
version = process.env.TEST_PM_VERSION;
process.env.npm_config_user_agent = name;
process.env.npm_config_user_agent = `${name}/${version}`;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was an attempt to get yarn working with create-vite but something appears to be stripping the version between us running pnpx and create-vite being run.

input: [keys.enter],
},
],
unsupportedOSs: ["win32"],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The current create-vite template doesn't pass on Windows either, so this is not a regression. But at some point we should look into fixing that.

},
verifyPreview: {
route: "/",
previewArgs: ["--host=127.0.0.1"],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is necessary because by default vite preview is only listening on localhost, which doesn't appear to include 127.0.0.1 (at least on some OSes).

@@ -742,6 +762,7 @@ const runCli = async (
NO_DEPLOY ? "--no-deploy" : "--deploy",
"--no-open",
"--no-git",
"--no-auto-update",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is probably not really needed but seemed like a safe thing to do to ensure that we are actually running the instance of C3 that we expected.

{
value: "react-swc-ts",
lang: "ts",
label: "TypeScript + SWC",
Copy link
Contributor

Choose a reason for hiding this comment

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

what does SWC stand for? and are we sure users will have this context?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is how it appears in the create-vite CLI so I think it's OK.

Name from API is: {name}
</button>
<p>
Edit <code>api/index.ts</code> to change the name
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this be <code>api/index.js</code>?

@@ -0,0 +1,9 @@
{
"name": "<TBD>",
"main": "api/index.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

indentation here is odd 🙃 . no biggie just calling it out. prolly prettier will fix, if you haven't run it already

@@ -74,7 +74,8 @@
"@types/react-transition-group>@types/react": "^18",
"@cloudflare/elements>@types/react": "^18",
"capnpc-ts>typescript": "4.2.4",
"@types/node": "$@types/node"
"@types/node": "$@types/node",
"vitest>vite": "^5.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

what is this > magic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is standard pnpm magic, which means override only vite version but only those that are dependencies of vitest.

Copy link
Contributor

@CarmenPopoviciu CarmenPopoviciu left a comment

Choose a reason for hiding this comment

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

looks like smth is broken when running this pre-release C3 version with the two js templates. ts templates are working just fine 🚀

Copy link
Contributor

@jamesopstad jamesopstad left a comment

Choose a reason for hiding this comment

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

Few minor suggestions. Not approving yet because the JS templates fail with this error:

ERROR TypeError: Cannot read properties of undefined (reading 'value')

@@ -107,6 +107,8 @@ function getFrameworkTests(opts: {
input: [keys.enter],
},
],
unsupportedOSs: ["win32"],
unsupportedPms: ["yarn"],
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think there's any way to fix this?

*
* This is rather than having a wrangler.toml in the e2e test's fixture folder,
* This is rather than having a wrangler configuration file in the e2e test's fixture folder,
* which overwrites any that comes from the framework's template.
*/
const addTestVarsToWranglerToml = async (projectPath: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe not for this PR, but the name of this variable and all the references to wrangler.toml should be updated to be format agnostic.

{
value: "react-swc-ts",
lang: "ts",
label: "TypeScript + SWC",
Copy link
Contributor

Choose a reason for hiding this comment

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

This is how it appears in the create-vite CLI so I think it's OK.

@@ -0,0 +1,54 @@
import { useState } from "react";
Copy link
Contributor

Choose a reason for hiding this comment

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

I think if we're only updating particular files in the template then we need to match the formatting of the Vite template. The prettier config should be:

{
  "semi": false,
  "singleQuote": true,
}

https://github.com/vitejs/vite/blob/main/.prettierrc.json

tsconfig.references.push({ path: "./tsconfig.worker.json" });
}
writeJSON("tsconfig.json", tsconfig);
s.stop(`${brandColor(`updated`)} ${dim(`\`angular.json\``)}`);
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
s.stop(`${brandColor(`updated`)} ${dim(`\`angular.json\``)}`);
s.stop(`${brandColor(`updated`)} ${dim(`\`tsconfig.json\``)}`);

await installPackages(["@cloudflare/vite-plugin"], {
dev: true,
startText: "Installing the Cloudflare Vite plugin",
doneText: `${brandColor(`updated`)} ${dim("wrangler@latest")}`,
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
doneText: `${brandColor(`updated`)} ${dim("wrangler@latest")}`,
doneText: `${brandColor(`installed`)} ${dim("@cloudflare/vite-plugin")}`,

transformPackageJson: async () => ({
scripts: {
deploy: `${npm} run build && wrangler deploy`,
preview: `${npm} run build && vite preview`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why deviate from the Vite template here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like you would need the build before running the preview, right?

await runFrameworkGenerator(ctx, [
ctx.project.name,
"--template",
variant?.value,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this possibly undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

turns out it is not

@petebacondarwin petebacondarwin force-pushed the pbd/add-vite-c3-templates branch from 82250e4 to fb1da7a Compare February 21, 2025 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

5 participants