Skip to content

Commit

Permalink
Add cloudflare adapter detection and path generation (#15603)
Browse files Browse the repository at this point in the history
Before submitting a pull request, please take a look at our

[Contributing](https://github.com/getsentry/sentry-javascript/blob/master/CONTRIBUTING.md)
guidelines and verify:

- [x] If you've added code that should be tested, please add tests.
- [x] Ensure your code lints and the test suite passes (`yarn lint`) &
(`yarn test`).

This PR expands upon #14672 by adding detection of the
@sveltejs/adapter-cloudflare adapter and defining it's output directory
(cloudflare as opposed to output). I've tested it with a Cloudflare
Pages project and it was successful in building and uploading
sourcemaps, whereas relying on 'other' caused an infinite loop and an
OOM when trying to upload sourcemaps.
  • Loading branch information
rileyg98 authored Mar 6, 2025
1 parent e68cbec commit e3692a4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
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

0 comments on commit e3692a4

Please sign in to comment.