Repository: withastro/astro
File: packages/integrations/cloudflare/src/index.ts (TypeScript)

Code before:
						edgeMiddleware: false,
						buildOutput: 'server',
					},
					previewEntrypoint: '@astrojs/cloudflare/entrypoints/preview',
					supportedAstroFeatures: {
						serverOutput: 'stable',
						hybridOutput: 'stable',
						staticOutput: 'unsupported',
						i18nDomains: 'experimental',
						sharpImageService: {
							support: 'limited',
							message:
								'Cloudflare does not support sharp at runtime. However, you can configure `imageService: "compile"` to optimize images with sharp on prerendered pages during build time.',
							// For explicitly set image services, we suppress the warning about sharp not being supported at runtime,
							// inferring the user is aware of the limitations.
							suppress: args?.imageService ? 'all' : 'default',
						},
						envGetSecret: 'stable',
					},
				});

				// Assign .dev.vars to process.env so astro:env can find these vars
				const devVarsPath = new URL('.dev.vars', config.root);
				if (existsSync(devVarsPath)) {
					const data = readFileSync(devVarsPath, 'utf-8');
					const parsed = parse(data);
					Object.assign(process.env, parsed);
				}

			},
			'astro:build:setup': ({ vite, target }) => {
				if (target === 'server') {
					vite.resolve ||= {};
					vite.resolve.alias ||= {};

					const aliases = [
						{
							find: 'react-dom/server',
							replacement: 'react-dom/server.browser',
						},
					];

					if (Array.isArray(vite.resolve.alias)) {
						vite.resolve.alias = [...vite.resolve.alias, ...aliases];
					} else {
						for (const alias of aliases) {
							(vite.resolve.alias as Record<string, string>)[alias.find] = alias.replacement;
						}
					}

					// Support `workerd` and `worker` conditions for the ssr environment

Review comment (suggestion):
This function can throw a runtime error, we should catch it and maybe:
- handle it gracefully with a log
- or throw and `AstroError`

Code after:
						edgeMiddleware: false,
						buildOutput: 'server',
					},
					previewEntrypoint: '@astrojs/cloudflare/entrypoints/preview',
					supportedAstroFeatures: {
						serverOutput: 'stable',
						hybridOutput: 'stable',
						staticOutput: 'unsupported',
						i18nDomains: 'experimental',
						sharpImageService: {
							support: 'limited',
							message:
								'Cloudflare does not support sharp at runtime. However, you can configure `imageService: "compile"` to optimize images with sharp on prerendered pages during build time.',
							// For explicitly set image services, we suppress the warning about sharp not being supported at runtime,
							// inferring the user is aware of the limitations.
							suppress: args?.imageService ? 'all' : 'default',
						},
						envGetSecret: 'stable',
					},
				});

				// Assign .dev.vars to process.env so astro:env can find these vars
				const devVarsPath = new URL('.dev.vars', config.root);
				if (existsSync(devVarsPath)) {
					try {
						const data = readFileSync(devVarsPath, 'utf-8');
						const parsed = parse(data);
						Object.assign(process.env, parsed);
					} catch {
						logger.error(`Unable to parse .dev.vars, variables will not be available to your application.`);
					}
				}
			},
			'astro:build:setup': ({ vite, target }) => {
				if (target === 'server') {
					vite.resolve ||= {};
					vite.resolve.alias ||= {};

					const aliases = [
						{
							find: 'react-dom/server',
							replacement: 'react-dom/server.browser',
						},
					];

					if (Array.isArray(vite.resolve.alias)) {
						vite.resolve.alias = [...vite.resolve.alias, ...aliases];
					} else {
						for (const alias of aliases) {
							(vite.resolve.alias as Record<string, string>)[alias.find] = alias.replacement;
						}