Skip to content

feat: support Cloudflare Workers Builds #13733

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions .changeset/plenty-cougars-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/adapter-cloudflare': minor
'@sveltejs/adapter-auto': minor
---

feat: support Cloudflare Workers Builds by detecting the `WORKERS_CI` environment variable
2 changes: 1 addition & 1 deletion packages/adapter-auto/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const adapters = [
},
{
name: 'Cloudflare Pages',
test: () => !!process.env.CF_PAGES,
test: () => !!process.env.WORKERS_CI || !!process.env.CF_PAGES,
module: '@sveltejs/adapter-cloudflare',
version: '7'
},
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-auto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async function get_adapter() {

console.log(`Successfully installed ${match.module}.`);
console.warn(
`\nIf you plan on staying on this deployment platform, consider replacing @sveltejs/adapter-auto with ${match.module}. This will give you faster and more robust installs, and more control over deployment configuration.\n`
`\nIf you plan on staying on this deployment platform, consider replacing @sveltejs/adapter-auto with ${match.module}. This will give you faster, more robust installs, and more control over deployment configuration.\n`
);
} catch (e) {
throw new Error(
Expand Down
58 changes: 32 additions & 26 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export default function (options = {}) {
);
}

const wrangler_config = validate_config(options.config);
const building_for_cloudflare_pages = is_building_for_cloudflare_pages(wrangler_config);
const { wrangler_config, building_for_cloudflare_pages } = validate_config(options.config);

let dest = builder.getBuildDirectory('cloudflare');
let worker_dest = `${dest}/_worker.js`;
Expand Down Expand Up @@ -266,52 +265,59 @@ _redirects

/**
* @param {string} config_file
* @returns {import('wrangler').Unstable_Config}
* @returns {{
* wrangler_config: import('wrangler').Unstable_Config,
* building_for_cloudflare_pages: boolean
* }}
*/
function validate_config(config_file = undefined) {
const wrangler_config = unstable_readConfig({ config: config_file });

// we don't support workers sites
const wrangler_file = wrangler_config.configPath || 'your wrangler.jsonc file';

// we don't support Workers Sites
if (wrangler_config.site) {
throw new Error(
`You must remove all \`site\` keys in ${wrangler_config.configPath}. Consult https://svelte.dev/docs/kit/adapter-cloudflare#Migrating-from-Workers-Sites-to-Workers-Static-Assets`
`You must remove all \`site\` keys in ${wrangler_file}. Consult https://svelte.dev/docs/kit/adapter-cloudflare#Migrating-from-Workers-Sites-to-Workers-Static-Assets`
);
}

// we don't need to validate the config if we're building for Cloudflare Pages
// because the `main` and `assets` values cannot be changed
if (is_building_for_cloudflare_pages(wrangler_config)) {
return wrangler_config;
return {
wrangler_config,
building_for_cloudflare_pages: true
};
}

// probably deploying to Cloudflare Workers
if (wrangler_config.main || wrangler_config.assets) {
if (!wrangler_config.assets?.directory) {
throw new Error(
`You must specify the \`assets.directory\` key in ${wrangler_config.configPath}. Consult https://developers.cloudflare.com/workers/static-assets/binding/#directory`
);
}
if (!wrangler_config.assets?.directory) {
throw new Error(
`You must specify the \`assets.directory\` key in ${wrangler_file}. Consult https://developers.cloudflare.com/workers/static-assets/binding/#directory`
);
}

if (!wrangler_config.assets?.binding) {
throw new Error(
`You must specify the \`assets.binding\` key in ${wrangler_config.configPath}. Consult https://developers.cloudflare.com/workers/static-assets/binding/#binding`
);
}
if (!wrangler_config.assets?.binding) {
throw new Error(
`You must specify the \`assets.binding\` key in ${wrangler_file}. Consult https://developers.cloudflare.com/workers/static-assets/binding/#binding`
);
}

return wrangler_config;
return {
wrangler_config,
building_for_cloudflare_pages: false
};
}

/**
* @param {import('wrangler').Unstable_Config} wrangler_config
* @returns {boolean}
*/
function is_building_for_cloudflare_pages(wrangler_config) {
return (
!!process.env.CF_PAGES ||
!wrangler_config.configPath ||
!!wrangler_config.pages_build_output_dir ||
!wrangler_config.main ||
!wrangler_config.assets
);
if (!!process.env.WORKERS_CI || wrangler_config.main || wrangler_config.assets) {
return false;
}
return true;
}

/** @param {string} str */
Expand Down
Loading