Skip to content

Add cloudflare adapter detection and path generation #15603

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

Merged
merged 4 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/sveltekit/src/vite/detectAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Package } from '@sentry/core';
/**
* Supported @sveltejs/adapters-[adapter] SvelteKit adapters
*/
export type SupportedSvelteKitAdapters = 'node' | 'auto' | 'vercel' | 'other';
export type SupportedSvelteKitAdapters = 'node' | 'auto' | 'vercel' | 'cloudflare' | 'other';

/**
* Tries to detect the used adapter for SvelteKit by looking at the dependencies.
Expand All @@ -21,6 +21,8 @@ export async function detectAdapter(debug?: boolean): Promise<SupportedSvelteKit
adapter = 'vercel';
} else if (allDependencies['@sveltejs/adapter-node']) {
adapter = 'node';
} else if (allDependencies['@sveltejs/adapter-cloudflare']) {
adapter = 'cloudflare';
} else if (allDependencies['@sveltejs/adapter-auto']) {
adapter = 'auto';
}
Expand Down
4 changes: 4 additions & 0 deletions packages/sveltekit/src/vite/svelteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export async function getAdapterOutputDir(svelteConfig: Config, adapter: Support
if (adapter === 'node') {
return getNodeAdapterOutputDir(svelteConfig);
}
if (adapter === 'cloudflare') {
// Cloudflare outputs to outDir\cloudflare as the output dir
return path.join(svelteConfig.kit?.outDir || '.svelte-kit', 'cloudflare');
}

// Auto and Vercel adapters simply use config.kit.outDir
// Let's also use this directory for the 'other' case
Expand Down
6 changes: 5 additions & 1 deletion packages/sveltekit/test/vite/detectAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('detectAdapter', () => {
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

it.each(['auto', 'vercel', 'node'])(
it.each(['auto', 'vercel', 'node', 'cloudflare'])(
'returns the adapter name (adapter %s) and logs it to the console',
async adapter => {
pkgJson.dependencies[`@sveltejs/adapter-${adapter}`] = '1.0.0';
Expand Down Expand Up @@ -68,12 +68,16 @@ describe('detectAdapter', () => {
pkgJson.dependencies['@sveltejs/adapter-auto'] = '1.0.0';
pkgJson.dependencies['@sveltejs/adapter-vercel'] = '1.0.0';
pkgJson.dependencies['@sveltejs/adapter-node'] = '1.0.0';
pkgJson.dependencies['@sveltejs/adapter-cloudflare'] = '1.0.0';

const detectedAdapter = await detectAdapter();
expect(detectedAdapter).toEqual('vercel');

delete pkgJson.dependencies['@sveltejs/adapter-vercel'];
const detectedAdapter2 = await detectAdapter();
expect(detectedAdapter2).toEqual('node');
delete pkgJson.dependencies['@sveltejs/adapter-node'];
const detectedAdapter3 = await detectAdapter();
expect(detectedAdapter3).toEqual('cloudflare');
});
});
5 changes: 5 additions & 0 deletions packages/sveltekit/test/vite/svelteConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ describe('getAdapterOutputDir', () => {
expect(outputDir).toEqual('customBuildDir');
});

it('returns the output directory of the Cloudflare adapter', async () => {
const outputDir = await getAdapterOutputDir({ kit: { outDir: 'customOutDir' } }, 'cloudflare');
expect(outputDir).toEqual('customOutDir/cloudflare');
});

it.each(['vercel', 'auto', 'other'] as SupportedSvelteKitAdapters[])(
'returns the config.kit.outdir directory for adapter-%s',
async adapter => {
Expand Down
Loading