From b756727c491101821c7f755b8405ffceac375021 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Fri, 17 Oct 2025 17:33:25 +0200 Subject: [PATCH 1/7] feat(cli): scaffold wrangler.jsonc in astro add --- .changeset/wide-ghosts-rule.md | 5 ++ packages/astro/src/cli/add/index.ts | 94 +++++++++++++++++++++++------ 2 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 .changeset/wide-ghosts-rule.md diff --git a/.changeset/wide-ghosts-rule.md b/.changeset/wide-ghosts-rule.md new file mode 100644 index 000000000000..6d110c60391b --- /dev/null +++ b/.changeset/wide-ghosts-rule.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Updates `astro add cloudflare` to scaffold a `wrangler.jsonc` configuration file diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index fbdbfef18b7a..6c3c5608e10f 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -80,6 +80,14 @@ export default async function seed() { // TODO } `, + CLOUDFLARE_WRANGLER_CONFIG: (name: string) => `{ + "main": "dist/_worker.js/index.js", + "name": ${JSON.stringify(name)}, + "assets": { + "binding": "ASSETS", + "directory": "./dist" + } +}`, }; const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record = { @@ -146,8 +154,71 @@ export async function add(names: string[], { flags }: AddOptions) { // Append forward slash to compute relative paths root.href = appendForwardSlash(root.href); + const rawConfigPath = await resolveConfigPath({ + root: rootPath, + configFile: inlineConfig.configFile, + fs: fsMod, + }); + let configURL = rawConfigPath ? pathToFileURL(rawConfigPath) : undefined; + + if (configURL) { + logger.debug('add', `Found config at ${configURL}`); + } else { + logger.info('add', `Unable to locate a config file, generating one for you.`); + configURL = new URL('./astro.config.mjs', root); + await fs.writeFile(fileURLToPath(configURL), STUBS.ASTRO_CONFIG, { encoding: 'utf-8' }); + } + + let packageJson: + | { type: 'exists'; data: { name: string; dependencies?: any; devDependencies?: any } } + | { type: 'unknown' } + | { type: 'does-not-exist' } = { type: 'unknown' }; + + async function getPackageJson() { + if (packageJson.type === 'exists') { + return packageJson.data; + } + + if (packageJson.type === 'does-not-exist') { + return null; + } + + const pkgURL = new URL('./package.json', configURL); + if (existsSync(pkgURL)) { + packageJson = { + type: 'exists', + data: await fs.readFile(fileURLToPath(pkgURL)).then((res) => JSON.parse(res.toString())), + }; + return packageJson.data; + } + + packageJson = { type: 'does-not-exist' }; + return null; + } + switch (installResult) { case UpdateResult.updated: { + if (integrations.find((integration) => integration.id === 'cloudflare')) { + const wranglerConfigURL = new URL('./wrangler.jsonc', configURL); + if (!existsSync(wranglerConfigURL)) { + logger.info( + 'SKIP_FORMAT', + `\n ${magenta(`Astro will scaffold ${green('./wrangler.jsonc')}.`)}\n`, + ); + + if (await askToContinue({ flags })) { + const data = await getPackageJson(); + + await fs.writeFile( + wranglerConfigURL, + STUBS.CLOUDFLARE_WRANGLER_CONFIG(data?.name ?? 'example'), + 'utf-8', + ); + } + } else { + logger.debug('add', 'Using existing wrangler configuration'); + } + } if (integrations.find((integration) => integration.id === 'tailwind')) { const dir = new URL('./styles/', new URL(userConfig.srcDir ?? './src/', root)); const styles = new URL('./global.css', dir); @@ -245,21 +316,6 @@ export async function add(names: string[], { flags }: AddOptions) { break; } - const rawConfigPath = await resolveConfigPath({ - root: rootPath, - configFile: inlineConfig.configFile, - fs: fsMod, - }); - let configURL = rawConfigPath ? pathToFileURL(rawConfigPath) : undefined; - - if (configURL) { - logger.debug('add', `Found config at ${configURL}`); - } else { - logger.info('add', `Unable to locate a config file, generating one for you.`); - configURL = new URL('./astro.config.mjs', root); - await fs.writeFile(fileURLToPath(configURL), STUBS.ASTRO_CONFIG, { encoding: 'utf-8' }); - } - let mod: ProxifiedModule | undefined; try { mod = await loadFile(fileURLToPath(configURL)); @@ -328,11 +384,9 @@ export async function add(names: string[], { flags }: AddOptions) { break; } case UpdateResult.none: { - const pkgURL = new URL('./package.json', configURL); - if (existsSync(fileURLToPath(pkgURL))) { - const { dependencies = {}, devDependencies = {} } = await fs - .readFile(fileURLToPath(pkgURL)) - .then((res) => JSON.parse(res.toString())); + const data = await getPackageJson(); + if (data) { + const { dependencies = {}, devDependencies = {} } = data; const deps = Object.keys(Object.assign(dependencies, devDependencies)); const missingDeps = integrations.filter( (integration) => !deps.includes(integration.packageName), From f7c8fcc334e07256799839a8e204cdedec27213f Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 20 Oct 2025 08:40:30 +0200 Subject: [PATCH 2/7] feat: assetsignore --- packages/astro/src/cli/add/index.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index 6c3c5608e10f..456a7d5445ca 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -80,7 +80,8 @@ export default async function seed() { // TODO } `, - CLOUDFLARE_WRANGLER_CONFIG: (name: string) => `{ + CLOUDFLARE_WRANGLER_CONFIG: (name: string) => `\ +{ "main": "dist/_worker.js/index.js", "name": ${JSON.stringify(name)}, "assets": { @@ -88,6 +89,7 @@ export default async function seed() { "directory": "./dist" } }`, + CLOUDFLARE_ASSETSIGNORE: `_worker.js`, }; const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record = { @@ -218,6 +220,24 @@ export async function add(names: string[], { flags }: AddOptions) { } else { logger.debug('add', 'Using existing wrangler configuration'); } + + const dir = new URL(userConfig.publicDir ?? './public/', root); + const assetsignore = new URL('./.assetsignore', dir); + if (!existsSync(assetsignore)) { + logger.info( + 'SKIP_FORMAT', + `\n ${magenta(`Astro will scaffold ${green('./public/.assetsignore')}.`)}\n`, + ); + + if (await askToContinue({ flags })) { + if (!existsSync(dir)) { + await fs.mkdir(dir); + } + await fs.writeFile(assetsignore, STUBS.CLOUDFLARE_ASSETSIGNORE, 'utf-8'); + } + } else { + logger.debug('add', `Using existing .assetsignore`); + } } if (integrations.find((integration) => integration.id === 'tailwind')) { const dir = new URL('./styles/', new URL(userConfig.srcDir ?? './src/', root)); From 8f27576695cd2fe7c60718a67f469951262d787a Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 20 Oct 2025 14:39:43 +0200 Subject: [PATCH 3/7] Update .changeset/wide-ghosts-rule.md --- .changeset/wide-ghosts-rule.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.changeset/wide-ghosts-rule.md b/.changeset/wide-ghosts-rule.md index 6d110c60391b..865f802dd767 100644 --- a/.changeset/wide-ghosts-rule.md +++ b/.changeset/wide-ghosts-rule.md @@ -1,5 +1,7 @@ --- -'astro': patch +'astro': minor --- -Updates `astro add cloudflare` to scaffold a `wrangler.jsonc` configuration file +Updates `astro add cloudflare` to scaffold more configuration files + +Running `astro add cloudflare` will now emit `wrangler.jsonc` and `public/.assetsignore`, allowing your Astro project to work out of the box as a worker. From 65a9658a6c85ac362295fe3978de1120fa4d117f Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Tue, 21 Oct 2025 11:04:48 +0200 Subject: [PATCH 4/7] Update packages/astro/src/cli/add/index.ts --- packages/astro/src/cli/add/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index 456a7d5445ca..2efd380442f3 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -89,7 +89,7 @@ export default async function seed() { "directory": "./dist" } }`, - CLOUDFLARE_ASSETSIGNORE: `_worker.js`, + CLOUDFLARE_ASSETSIGNORE: `_worker.js\n_routes.json`, }; const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record = { From c27129e9478656edbabe94e3f5fdb681d4f288fe Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Tue, 21 Oct 2025 11:42:17 +0200 Subject: [PATCH 5/7] Update packages/astro/src/cli/add/index.ts Co-authored-by: Oliver Speir <115520730+OliverSpeir@users.noreply.github.com> --- packages/astro/src/cli/add/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index 2efd380442f3..d4094e196cc0 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -84,6 +84,7 @@ export default async function seed() { { "main": "dist/_worker.js/index.js", "name": ${JSON.stringify(name)}, + "compatibility_date": ${JSON.stringify(new Date().toISOString().slice(0,10))}, "assets": { "binding": "ASSETS", "directory": "./dist" From 6631e1af75ce035a23315d999a72cc027991071d Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 22 Oct 2025 13:18:19 +0200 Subject: [PATCH 6/7] Update packages/astro/src/cli/add/index.ts Co-authored-by: Oliver Speir <115520730+OliverSpeir@users.noreply.github.com> --- packages/astro/src/cli/add/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index d4094e196cc0..f9e17f5209c4 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -85,6 +85,10 @@ export default async function seed() { "main": "dist/_worker.js/index.js", "name": ${JSON.stringify(name)}, "compatibility_date": ${JSON.stringify(new Date().toISOString().slice(0,10))}, + "compatibility_flags": [ + "nodejs_compat", + "global_fetch_strictly_public" + ], "assets": { "binding": "ASSETS", "directory": "./dist" From 895153fd17a27ece4d071f1990041e276fa625e3 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 22 Oct 2025 13:18:27 +0200 Subject: [PATCH 7/7] Update packages/astro/src/cli/add/index.ts Co-authored-by: Oliver Speir <115520730+OliverSpeir@users.noreply.github.com> --- packages/astro/src/cli/add/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index f9e17f5209c4..ed6b98a02516 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -92,7 +92,10 @@ export default async function seed() { "assets": { "binding": "ASSETS", "directory": "./dist" - } + }, + "observability": { + "enabled": true + } }`, CLOUDFLARE_ASSETSIGNORE: `_worker.js\n_routes.json`, };