Repository navigation
fix: align the SSR environment config with @cloudflare/vite-plugin
#17503
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
89f69c9
53d696c
4ee0a00
57f1895
e27a01e
a1b11e6
ca5e89c
21cba94
e13c52d
3db42d3
bcacf09
794b303
8c73e47
cc99ea2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/adapter-cloudflare': patch | ||
| --- | ||
|
|
||
| fix: build the server with the environment config from `@cloudflare/vite-plugin` so that CommonJS `require` calls of Node builtins work in workerd |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** @import { Plugin } from 'vite' */ | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { cloudflare } from '@cloudflare/vite-plugin'; | ||
|
|
||
| const useful_cloudflare_plugins = new Set([ | ||
| // resolves the plugin config that the other plugins read | ||
| 'vite-plugin-cloudflare', | ||
| // alias Node.js builtins to the polyfills workerd uses and leave the rest as imports | ||
| 'vite-plugin-cloudflare:nodejs-als', | ||
| 'vite-plugin-cloudflare:nodejs-compat', | ||
| 'vite-plugin-cloudflare:nodejs-compat-warnings' | ||
| ]); | ||
|
|
||
| /** | ||
| * Applies the Worker environment config that `@cloudflare/vite-plugin` generates | ||
| * so that the server output suits workerd | ||
| * @param {string | undefined} config_path | ||
| * @returns {Plugin[]} | ||
| */ | ||
| export function plugin_config(config_path) { | ||
| const plugins = cloudflare({ | ||
| configPath: config_path, | ||
| viteEnvironment: { name: 'ssr' }, | ||
| config(config) { | ||
| // the plugin requires `main` to exist but our Worker entrypoint is generated after the build | ||
| config.main = fileURLToPath(new URL('./files/worker.js', import.meta.url)); | ||
| } | ||
| }); | ||
|
|
||
| const cloudflare_config_hook = plugins.find( | ||
| (plugin) => plugin.name === 'vite-plugin-cloudflare:config' | ||
| )?.config; | ||
|
|
||
| if (typeof cloudflare_config_hook !== 'function') { | ||
| throw new Error( | ||
| "Failed to find the vite-plugin-cloudflare:config plugin's config hook. This should not happen." | ||
| ); | ||
| } | ||
|
|
||
| return [ | ||
| ...plugins | ||
| .filter((plugin) => useful_cloudflare_plugins.has(plugin.name)) | ||
| .map((plugin) => /** @satisfies {Plugin} */ ({ ...plugin, apply: 'build' })), | ||
| { | ||
| name: 'vite-plugin-sveltekit-adapter-cloudflare-environment', | ||
| apply: 'build', | ||
| async config(user_config, env) { | ||
| const ssr = (await cloudflare_config_hook.call(this, user_config, env))?.environments?.ssr; | ||
|
|
||
| if (ssr === undefined) { | ||
| throw new Error( | ||
| 'Failed to get the SSR environment config from @cloudflare/vite-plugin. This should not happen.' | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| environments: { | ||
| ssr: { | ||
| keepProcessEnv: ssr.keepProcessEnv, | ||
| define: ssr.define, | ||
| resolve: { | ||
| noExternal: ssr.resolve?.noExternal, | ||
| // `browser` would resolve `esm-env` to `BROWSER = true` and similar | ||
| conditions: ssr.resolve?.conditions?.filter((condition) => condition !== 'browser'), | ||
| // we resolve `cloudflare:workers` to a stub that runs in Node | ||
| builtins: ssr.resolve?.builtins?.filter((id) => id !== 'cloudflare:workers') | ||
| }, | ||
| build: { | ||
| target: ssr.build?.target, | ||
| rolldownOptions: { | ||
| platform: ssr.build?.rolldownOptions?.platform, | ||
| resolve: ssr.build?.rolldownOptions?.resolve | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| ]; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| "files/worker.js", | ||
| "index.js", | ||
| "utils.js", | ||
| "config.js", | ||
| "index.d.ts" | ||
| ], | ||
| "scripts": { | ||
|
|
@@ -43,6 +44,7 @@ | |
| "tsc-native": "node node_modules/@typescript/native/bin/tsc" | ||
| }, | ||
| "dependencies": { | ||
| "@cloudflare/vite-plugin": "^1.64.0", | ||
| "@cloudflare/workers-types": "^5.20260925.1", | ||
| "@rolldown/pluginutils": "^1.0.1" | ||
| }, | ||
|
|
@@ -56,6 +58,6 @@ | |
| }, | ||
| "peerDependencies": { | ||
| "@sveltejs/kit": "^3.0.0-next.0", | ||
| "wrangler": "^4.118.0" | ||
| "wrangler": "^4.151.0" | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bumps the |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // A CommonJS module that require()s a Node builtin, like node-forge does. | ||
| // Bundling this must not emit `createRequire(import.meta.url)` because | ||
| // `import.meta.url` is undefined in workerd https://github.com/sveltejs/kit/issues/15960 | ||
| const crypto = require('node:crypto'); | ||
|
|
||
| module.exports.token = () => crypto.randomBytes(8).toString('hex'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "name": "cjs-dep", | ||
| "version": "0.0.1", | ||
| "main": "./index.cjs" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| { | ||
| "$schema": "../node_modules/wrangler/config-schema.json", | ||
| "main": "../dist/index.js", | ||
| "compatibility_date": "2026-06-24", | ||
| "compatibility_date": "2026-09-01", | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The workers test app now uses a compatibility date after 2026-08-04, from which |
||
| "assets": { | ||
| "directory": "../dist/public", | ||
| "binding": "ASSETS" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { token } from 'cjs-dep'; | ||
|
|
||
| export function GET() { | ||
| return new Response(token()); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| "utils.js", | ||
| "utils.spec.js", | ||
| "vitest.config.js", | ||
| "config.js", | ||
| "globals.d.ts", | ||
| "test/utils.js" | ||
| ] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.