Skip to content

Cloudflare adapter worker build is resolving client-side files from libraries #12813

@Lms24

Description

@Lms24

Describe the bug

When using our package @sentry/sveltekit in a minimal SvelteKit app with @sveltejs/adapter-cloudflare, a build error is logged when the adapter is invoked (see logs).

Upon closer inspection, it seems like the esbuild build within the cloudflare adapter is resolving the browser entry of our package's package.json exports instead of the server-side part. In the client-side part of our SDK package, we import { page, navigating } from "$app/stores" which esbuild can't resolve and therefore throws an error.

I already tried adding a worker exports condition in our package.json that would point to server files but this doesn't do anything. It still seems like esbuild is accessing our bowser exports entry point.

Reproduction

I created a minimal reproduction - please feel free to clone it

Logs

> Using @sveltejs/adapter-cloudflare
✘ [ERROR] Could not resolve "$app/stores"

    node_modules/@sentry/sveltekit/build/esm/client/browserTracingIntegration.js:1:33:
      1 │ import { page, navigating } from '$app/stores';~~~~~~~~~~~~~

  You can mark the path "$app/stores" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle.


error during build:
Error: Bundling with esbuild failed with 1 error
    at adapt (file:///Users/lukas/code/test-projects/gh-sentry-javascript-13976-sveltekit-cloudflare-pages/my-svelte-app/node_modules/@sveltejs/adapter-cloudflare/index.js:140:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async adapt (file:///Users/lukas/code/test-projects/gh-sentry-javascript-13976-sveltekit-cloudflare-pages/my-svelte-app/node_modules/@sveltejs/kit/src/core/adapt/index.js:38:2)
    at async finalise (file:///Users/lukas/code/test-projects/gh-sentry-javascript-13976-sveltekit-cloudflare-pages/my-svelte-app/node_modules/@sveltejs/kit/src/exports/vite/index.js:891:7)
    at async Object.handler (file:///Users/lukas/code/test-projects/gh-sentry-javascript-13976-sveltekit-cloudflare-pages/my-svelte-app/node_modules/@sveltejs/kit/src/exports/vite/index.js:921:5)
    at async PluginDriver.hookParallel (file:///Users/lukas/code/test-projects/gh-sentry-javascript-13976-sveltekit-cloudflare-pages/my-svelte-app/node_modules/rollup/dist/es/shared/node-entry.js:20652:17)
    at async Object.close (file:///Users/lukas/code/test-projects/gh-sentry-javascript-13976-sveltekit-cloudflare-pages/my-svelte-app/node_modules/rollup/dist/es/shared/node-entry.js:21627:13)
    at async build (file:///Users/lukas/code/test-projects/gh-sentry-javascript-13976-sveltekit-cloudflare-pages/my-svelte-app/node_modules/vite/dist/node/chunks/dep-Cyk9bIUq.js:65455:17)
    at async CAC.<anonymous> (file:///Users/lukas/code/test-projects/gh-sentry-javascript-13976-sveltekit-cloudflare-pages/my-svelte-app/node_modules/vite/dist/node/cli.js:828:5)

System Info

System:
    OS: macOS 14.7
    CPU: (10) arm64 Apple M1 Pro
    Memory: 103.63 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.0 - ~/.volta/tools/image/node/20.11.0/bin/node
    Yarn: 4.1.1 - ~/.volta/tools/image/yarn/4.1.1/bin/yarn
    npm: 10.4.0 - ~/.volta/tools/image/npm/10.4.0/bin/npm
    pnpm: 8.14.3 - ~/.volta/tools/image/pnpm/8.14.3/bin/pnpm
    bun: 1.1.26 - ~/.bun/bin/bun
    Watchman: 2024.07.15.00 - /opt/homebrew/bin/watchman
  Browsers:
    Brave Browser: 101.1.38.111
    Chrome Canary: 123.0.6268.0
    Edge: 129.0.2792.89
    Safari: 17.6
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.2.5 
    @sveltejs/adapter-cloudflare: ^4.7.3 => 4.7.3 
    @sveltejs/kit: ^2.0.0 => 2.7.1 
    @sveltejs/vite-plugin-svelte: ^4.0.0-next.6 => 4.0.0-next.8 
    svelte: ^5.0.0-next.1 => 5.0.0-next.265 
    vite: ^5.0.3 => 5.4.9 

Severity

annoyance

Additional Information

⬆️ RE Severity: Blocker for users who'd like to add Sentry to their SvelteKit app running on Cloudflare

I'd appreciate any pointers as to what we/library authors need to do to support a use case where

  • the package exports files for both, browser and server
  • the browser-side code imports from kit-specific modules like $app/stores
  • the cloudflare adapter is used

Thanks a lot and please let me know if I can provide more information!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions