Skip to content

feat(cloudflare): add SvelteKit + Cloudflare Vite plugin#622

Open
theoephraim wants to merge 3 commits intomainfrom
fix/sveltekit-cloudflare
Open

feat(cloudflare): add SvelteKit + Cloudflare Vite plugin#622
theoephraim wants to merge 3 commits intomainfrom
fix/sveltekit-cloudflare

Conversation

@theoephraim
Copy link
Copy Markdown
Member

Summary

  • Adds varlockSvelteKitCloudflarePlugin exported from @varlock/cloudflare-integration/sveltekit for SvelteKit projects deployed to Cloudflare Workers via @sveltejs/adapter-cloudflare.
  • The existing varlockCloudflareVitePlugin wraps @cloudflare/vite-plugin, which doesn't currently play nicely with SvelteKit (cloudflare/workers-sdk#8922). The new variant skips that wrapper while still injecting a cloudflare:workers env loader into the SvelteKit SSR entry (@sveltejs/kit/.../runtime/server/index.js, which adapter-cloudflare's generated _worker.js imports).
  • The injection is guarded by navigator.userAgent === 'Cloudflare-Workers' and uses a dynamic import so SvelteKit's Node-side postbuild steps (prerender, fallback) don't fail resolving cloudflare:workers.
  • Extracts the shared env-loader body (__VARLOCK_ENV / chunked bindings → globalThis.__varlockLoadedEnv) into shared-ssr-entry-code.ts and reuses it from both plugin variants. The existing Workers-only plugin keeps its synchronous static import — no runtime behavior change there.

Why two variants instead of a flag

The two paths have effectively no overlap at runtime (different entry module, different dev story, different Vite plugin composition). A separate subpath export keeps the docs, errors, and types clean.

Test plan

  • bun run build in packages/integrations/cloudflare
  • npm run build on the sveltekit-on-cloudflare example — SSR entry contains the guarded loader, static import 'cloudflare:workers' is preserved as external
  • bun run lint:fix clean
  • End-to-end deploy of the SvelteKit example via varlock-wrangler deploy and verify env resolves at runtime in a real Worker
  • Sanity-check existing Cloudflare Workers flow still works (snippet output is byte-identical to before this PR)

🤖 Generated with Claude Code

Adds `varlockSvelteKitCloudflarePlugin` exported at
`@varlock/cloudflare-integration/sveltekit` for SvelteKit projects
deploying to Cloudflare Workers via `@sveltejs/adapter-cloudflare`.

The existing `varlockCloudflareVitePlugin` wraps `@cloudflare/vite-plugin`,
which doesn't currently support SvelteKit. The new plugin skips that
wrapper while still injecting a `cloudflare:workers` env loader into the
SvelteKit SSR entry — guarded by a `navigator.userAgent` check and a
dynamic import so SvelteKit's Node-side postbuild steps don't fail
resolving `cloudflare:workers`.

The shared env-loader body (reading `__VARLOCK_ENV` / chunked bindings
from `cloudflare:workers` env and stashing it on
`globalThis.__varlockLoadedEnv`) is extracted to
`shared-ssr-entry-code.ts` and reused by both variants.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 16, 2026

🦋 Changeset detected

Latest commit: ca9209f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@varlock/cloudflare-integration Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 16, 2026

Open in StackBlitz

npm i https://pkg.pr.new/varlock@622
npm i https://pkg.pr.new/@varlock/cloudflare-integration@622
npm i https://pkg.pr.new/@varlock/akeyless-plugin@622
npm i https://pkg.pr.new/@varlock/doppler-plugin@622

commit: ca9209f

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Apr 16, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
varlock-website be4cdcf Commit Preview URL

Branch Preview URL
Apr 16 2026, 06:54 AM

- Add a new SvelteKit integration page. For most adapters it routes
  users to the Vite integration; for Cloudflare Workers it documents
  `varlockSvelteKitCloudflarePlugin` setup and the Workers Builds
  dashboard configuration (overriding the Deploy command, setting
  secret-zero vars in Build variables).
- Register the new page in the sidebar and the integrations overview.
- Add a small callout on the Cloudflare integration page pointing
  SvelteKit users to the SvelteKit guide instead of the two
  existing approaches.
- Add a Cloudflare Workers Builds (CI/CD) section to the Cloudflare
  integration page with the same dashboard-configuration guidance.
- Clarify on the Vite integration page that projects with SSR/server
  code on Cloudflare Workers should use the Cloudflare integration
  (not the plain Vite plugin) so secrets aren't bundled into the
  worker.
- Update the package README to list the SvelteKit variant.
- Drop the "new" badge on the Bun entry; add one on SvelteKit.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@theoephraim theoephraim force-pushed the fix/sveltekit-cloudflare branch from 9d11aa2 to be4cdcf Compare April 16, 2026 06:52
Two Vitest worker processes (e.g. cloudflare-vite + wrangler tests) could
race on the same `.packed/<pkg>.tgz` output path, producing a corrupted
tarball and failing `bun install` with ZlibError. Add a per-package
file-based mutex (atomic mkdir) around the pack step so the second
worker reuses the first worker's tarball.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
// Pack the package
const fullPackageDir = join(REPO_ROOT, packageDir);
console.log(`[pack] Packing ${name}...`);
execSync(`bun pm pack --destination ${PACKED_DIR}`, {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants