diff --git a/.changeset/polite-dragons-compete.md b/.changeset/polite-dragons-compete.md new file mode 100644 index 000000000000..51e3dcbf1159 --- /dev/null +++ b/.changeset/polite-dragons-compete.md @@ -0,0 +1,5 @@ +--- +"create-cloudflare": patch +--- + +fix: make sure C3 works with the latest Astro versions diff --git a/packages/create-cloudflare/templates-experimental/astro/c3.ts b/packages/create-cloudflare/templates-experimental/astro/c3.ts index 1674809fd026..9794c9100d06 100644 --- a/packages/create-cloudflare/templates-experimental/astro/c3.ts +++ b/packages/create-cloudflare/templates-experimental/astro/c3.ts @@ -1,7 +1,7 @@ import { logRaw, updateStatus } from "@cloudflare/cli"; import { blue, brandColor, dim } from "@cloudflare/cli/colors"; import { runFrameworkGenerator } from "frameworks/index"; -import { loadTemplateSnippets, transformFile } from "helpers/codemod"; +import { transformFile } from "helpers/codemod"; import { runCommand } from "helpers/command"; import { usesTypescript } from "helpers/files"; import { detectPackageManager } from "helpers/packageManagers"; @@ -27,7 +27,6 @@ const configure = async (ctx: C3Context) => { }); updateAstroConfig(); - updateEnvDeclaration(ctx); }; const updateAstroConfig = () => { @@ -59,35 +58,6 @@ const updateAstroConfig = () => { }); }; -const updateEnvDeclaration = (ctx: C3Context) => { - if (!usesTypescript(ctx)) { - return; - } - - const filePath = "src/env.d.ts"; - - updateStatus(`Adding type declarations in ${blue(filePath)}`); - - transformFile(filePath, { - visitProgram: function (n) { - const snippets = loadTemplateSnippets(ctx); - const patch = snippets.runtimeDeclarationTs; - const b = recast.types.builders; - - // Preserve comments with the new body - const comments = n.get("comments").value; - n.node.comments = comments.map((c: recast.types.namedTypes.CommentLine) => - b.commentLine(c.value), - ); - - // Add the patch - n.get("body").push(...patch); - - return false; - }, - }); -}; - const config: TemplateConfig = { configVersion: 1, id: "astro", @@ -95,7 +65,21 @@ const config: TemplateConfig = { platform: "workers", displayName: "Astro", copyFiles: { - path: "./templates", + async selectVariant(ctx) { + // Note: this `selectVariant` function should not be needed + // this is just a quick workaround until + // https://github.com/cloudflare/workers-sdk/issues/7495 + // is resolved + return usesTypescript(ctx) ? "ts" : "js"; + }, + variants: { + js: { + path: "./templates/js", + }, + ts: { + path: "./templates/ts", + }, + }, }, devScript: "dev", deployScript: "deploy", diff --git a/packages/create-cloudflare/templates-experimental/astro/templates/public/.assetsignore b/packages/create-cloudflare/templates-experimental/astro/templates/js/public/.assetsignore similarity index 100% rename from packages/create-cloudflare/templates-experimental/astro/templates/public/.assetsignore rename to packages/create-cloudflare/templates-experimental/astro/templates/js/public/.assetsignore diff --git a/packages/create-cloudflare/templates-experimental/astro/templates/wrangler.toml b/packages/create-cloudflare/templates-experimental/astro/templates/js/wrangler.toml similarity index 100% rename from packages/create-cloudflare/templates-experimental/astro/templates/wrangler.toml rename to packages/create-cloudflare/templates-experimental/astro/templates/js/wrangler.toml diff --git a/packages/create-cloudflare/templates-experimental/astro/templates/ts/public/.assetsignore b/packages/create-cloudflare/templates-experimental/astro/templates/ts/public/.assetsignore new file mode 100644 index 000000000000..c2634cc3650a --- /dev/null +++ b/packages/create-cloudflare/templates-experimental/astro/templates/ts/public/.assetsignore @@ -0,0 +1,4 @@ +_worker.js +_routes.json +_headers +_redirects diff --git a/packages/create-cloudflare/templates-experimental/astro/snippets/runtimeDeclaration.ts b/packages/create-cloudflare/templates-experimental/astro/templates/ts/src/env.d.ts similarity index 100% rename from packages/create-cloudflare/templates-experimental/astro/snippets/runtimeDeclaration.ts rename to packages/create-cloudflare/templates-experimental/astro/templates/ts/src/env.d.ts diff --git a/packages/create-cloudflare/templates-experimental/astro/templates/ts/worker-configuration.d.ts b/packages/create-cloudflare/templates-experimental/astro/templates/ts/worker-configuration.d.ts new file mode 100644 index 000000000000..5b2319b3f29f --- /dev/null +++ b/packages/create-cloudflare/templates-experimental/astro/templates/ts/worker-configuration.d.ts @@ -0,0 +1,4 @@ +// Generated by Wrangler +// After adding bindings to `wrangler.toml`, regenerate this interface via `npm run cf-typegen` +interface Env { +} diff --git a/packages/create-cloudflare/templates-experimental/astro/templates/ts/wrangler.toml b/packages/create-cloudflare/templates-experimental/astro/templates/ts/wrangler.toml new file mode 100644 index 000000000000..bbe26d831746 --- /dev/null +++ b/packages/create-cloudflare/templates-experimental/astro/templates/ts/wrangler.toml @@ -0,0 +1,12 @@ +#:schema node_modules/wrangler/config-schema.json +name = "" +compatibility_date = "" +compatibility_flags = ["nodejs_compat_v2"] +main = "./dist/_worker.js/index.js" +assets = { directory = "./dist", binding = "ASSETS" } + +# Workers Logs +# Docs: https://developers.cloudflare.com/workers/observability/logs/workers-logs/ +# Configuration: https://developers.cloudflare.com/workers/observability/logs/workers-logs/#enable-workers-logs +[observability] +enabled = true diff --git a/packages/create-cloudflare/templates/astro/c3.ts b/packages/create-cloudflare/templates/astro/c3.ts index fbcacae2c35e..87252c91d582 100644 --- a/packages/create-cloudflare/templates/astro/c3.ts +++ b/packages/create-cloudflare/templates/astro/c3.ts @@ -1,9 +1,9 @@ import { logRaw, updateStatus } from "@cloudflare/cli"; import { blue, brandColor, dim } from "@cloudflare/cli/colors"; import { runFrameworkGenerator } from "frameworks/index"; -import { loadTemplateSnippets, transformFile } from "helpers/codemod"; +import { transformFile } from "helpers/codemod"; import { runCommand } from "helpers/command"; -import { usesTypescript } from "helpers/files"; +import { hasTsConfig, usesTypescript } from "helpers/files"; import { detectPackageManager } from "helpers/packageManagers"; import * as recast from "recast"; import type { TemplateConfig } from "../../src/templates"; @@ -17,7 +17,7 @@ const generate = async (ctx: C3Context) => { logRaw(""); // newline }; -const configure = async (ctx: C3Context) => { +const configure = async () => { await runCommand([npx, "astro", "add", "cloudflare", "-y"], { silent: true, startText: "Installing adapter", @@ -27,7 +27,6 @@ const configure = async (ctx: C3Context) => { }); updateAstroConfig(); - updateEnvDeclaration(ctx); }; const updateAstroConfig = () => { @@ -59,35 +58,6 @@ const updateAstroConfig = () => { }); }; -const updateEnvDeclaration = (ctx: C3Context) => { - if (!usesTypescript(ctx)) { - return; - } - - const filePath = "src/env.d.ts"; - - updateStatus(`Adding type declarations in ${blue(filePath)}`); - - transformFile(filePath, { - visitProgram: function (n) { - const snippets = loadTemplateSnippets(ctx); - const patch = snippets.runtimeDeclarationTs; - const b = recast.types.builders; - - // Preserve comments with the new body - const comments = n.get("comments").value; - n.node.comments = comments.map((c: recast.types.namedTypes.CommentLine) => - b.commentLine(c.value), - ); - - // Add the patch - n.get("body").push(...patch); - - return false; - }, - }); -}; - const config: TemplateConfig = { configVersion: 1, id: "astro", @@ -95,7 +65,21 @@ const config: TemplateConfig = { platform: "pages", displayName: "Astro", copyFiles: { - path: "./templates", + async selectVariant(ctx) { + // Note: this `selectVariant` function should not be needed + // this is just a quick workaround until + // https://github.com/cloudflare/workers-sdk/issues/7495 + // is resolved + return usesTypescript(ctx) ? "ts" : "js"; + }, + variants: { + js: { + path: "./templates/js", + }, + ts: { + path: "./templates/ts", + }, + }, }, devScript: "dev", deployScript: "deploy", diff --git a/packages/create-cloudflare/templates/astro/templates/wrangler.toml b/packages/create-cloudflare/templates/astro/templates/js/wrangler.toml similarity index 100% rename from packages/create-cloudflare/templates/astro/templates/wrangler.toml rename to packages/create-cloudflare/templates/astro/templates/js/wrangler.toml diff --git a/packages/create-cloudflare/templates/astro/snippets/runtimeDeclaration.ts b/packages/create-cloudflare/templates/astro/templates/ts/src/env.d.ts similarity index 100% rename from packages/create-cloudflare/templates/astro/snippets/runtimeDeclaration.ts rename to packages/create-cloudflare/templates/astro/templates/ts/src/env.d.ts diff --git a/packages/create-cloudflare/templates/astro/templates/ts/worker-configuration.d.ts b/packages/create-cloudflare/templates/astro/templates/ts/worker-configuration.d.ts new file mode 100644 index 000000000000..5b2319b3f29f --- /dev/null +++ b/packages/create-cloudflare/templates/astro/templates/ts/worker-configuration.d.ts @@ -0,0 +1,4 @@ +// Generated by Wrangler +// After adding bindings to `wrangler.toml`, regenerate this interface via `npm run cf-typegen` +interface Env { +} diff --git a/packages/create-cloudflare/templates/astro/templates/ts/wrangler.toml b/packages/create-cloudflare/templates/astro/templates/ts/wrangler.toml new file mode 100644 index 000000000000..70bd7c7606e2 --- /dev/null +++ b/packages/create-cloudflare/templates/astro/templates/ts/wrangler.toml @@ -0,0 +1,86 @@ +#:schema node_modules/wrangler/config-schema.json +name = "" +compatibility_date = "" +compatibility_flags = ["nodejs_compat"] +pages_build_output_dir = "./dist" + +# Automatically place your workloads in an optimal location to minimize latency. +# If you are running back-end logic in a Pages Function, running it closer to your back-end infrastructure +# rather than the end user may result in better performance. +# Docs: https://developers.cloudflare.com/pages/functions/smart-placement/#smart-placement +# [placement] +# mode = "smart" + +# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables) +# Docs: +# - https://developers.cloudflare.com/pages/functions/bindings/#environment-variables +# Note: Use secrets to store sensitive data. +# - https://developers.cloudflare.com/pages/functions/bindings/#secrets +# [vars] +# MY_VARIABLE = "production_value" + +# Bind the Workers AI model catalog. Run machine learning models, powered by serverless GPUs, on Cloudflare’s global network +# Docs: https://developers.cloudflare.com/pages/functions/bindings/#workers-ai +# [ai] +# binding = "AI" + +# Bind a D1 database. D1 is Cloudflare’s native serverless SQL database. +# Docs: https://developers.cloudflare.com/pages/functions/bindings/#d1-databases +# [[d1_databases]] +# binding = "MY_DB" +# database_name = "my-database" +# database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" + +# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model. +# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps. +# Docs: https://developers.cloudflare.com/workers/runtime-apis/durable-objects +# [[durable_objects.bindings]] +# name = "MY_DURABLE_OBJECT" +# class_name = "MyDurableObject" +# script_name = 'my-durable-object' + +# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs. +# Docs: https://developers.cloudflare.com/pages/functions/bindings/#kv-namespaces +# [[kv_namespaces]] +# binding = "MY_KV_NAMESPACE" +# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + +# Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer. +# Docs: https://developers.cloudflare.com/pages/functions/bindings/#queue-producers +# [[queues.producers]] +# binding = "MY_QUEUE" +# queue = "my-queue" + +# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files. +# Docs: https://developers.cloudflare.com/pages/functions/bindings/#r2-buckets +# [[r2_buckets]] +# binding = "MY_BUCKET" +# bucket_name = "my-bucket" + +# Bind another Worker service. Use this binding to call another Worker without network overhead. +# Docs: https://developers.cloudflare.com/pages/functions/bindings/#service-bindings +# [[services]] +# binding = "MY_SERVICE" +# service = "my-service" + +# To use different bindings for preview and production environments, follow the examples below. +# When using environment-specific overrides for bindings, ALL bindings must be specified on a per-environment basis. +# Docs: https://developers.cloudflare.com/pages/functions/wrangler-configuration#environment-specific-overrides + +######## PREVIEW environment config ######## + +# [env.preview.vars] +# API_KEY = "xyz789" + +# [[env.preview.kv_namespaces]] +# binding = "MY_KV_NAMESPACE" +# id = "" + +######## PRODUCTION environment config ######## + +# [env.production.vars] +# API_KEY = "abc123" + +# [[env.production.kv_namespaces]] +# binding = "MY_KV_NAMESPACE" +# id = ""