Skip to content
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

[breaking] drop Wrangler 1 support in favour of Wrangler 2 #4887

Merged
merged 7 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/little-pots-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
---

[breaking] support Wrangler 2, drop Wrangler 1
2 changes: 2 additions & 0 deletions packages/adapter-cloudflare-workers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

SvelteKit adapter that creates a Cloudflare Workers site using a function for dynamic server rendering.

**Requires [Wrangler v2](https://developers.cloudflare.com/workers/wrangler/get-started/).** Wrangler v1 is no longer supported.

_**Comparisons**_

- `adapter-cloudflare` – supports all SvelteKit features; builds for
Expand Down
98 changes: 34 additions & 64 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { posix } from 'path';
import { posix, dirname } from 'path';
import { execSync } from 'child_process';
import esbuild from 'esbuild';
import toml from '@iarna/toml';
import { fileURLToPath } from 'url';

/**
* @typedef {{
* main: string;
* site: {
* bucket: string;
* }
* }} WranglerConfig
*/

/** @type {import('.')} */
export default function (options = {}) {
return {
name: '@sveltejs/adapter-cloudflare-workers',

async adapt(builder) {
const { site, build } = validate_config(builder);

// @ts-ignore
const { bucket } = site;

// @ts-ignore
const entrypoint = site['entry-point'] || 'workers-site';

// @ts-ignore
const main_path = build.upload.main;
const { main, site } = validate_config(builder);

const files = fileURLToPath(new URL('./files', import.meta.url).href);
const tmp = builder.getBuildDirectory('cloudflare-workers-tmp');

builder.rimraf(bucket);
builder.rimraf(entrypoint);
builder.rimraf(site.bucket);
builder.rimraf(dirname(main));

builder.log.info('Installing worker dependencies...');
builder.copy(`${files}/_package.json`, `${tmp}/package.json`);
Expand Down Expand Up @@ -59,72 +59,48 @@ export default function (options = {}) {
platform: 'browser',
...options,
entryPoints: [`${tmp}/entry.js`],
outfile: `${entrypoint}/${main_path}`,
outfile: main,
bundle: true,
external: ['__STATIC_CONTENT_MANIFEST', ...(options?.external || [])],
format: 'esm'
});

writeFileSync(
`${entrypoint}/package.json`,
JSON.stringify({ main: main_path, type: 'module' })
);

builder.log.minor('Copying assets...');
builder.writeClient(bucket);
builder.writeStatic(bucket);
builder.writePrerendered(bucket);
builder.writeClient(site.bucket);
builder.writeStatic(site.bucket);
builder.writePrerendered(site.bucket);
}
};
}

/** @param {import('@sveltejs/kit').Builder} builder */
/**
* @param {import('@sveltejs/kit').Builder} builder
* @returns {WranglerConfig}
*/
function validate_config(builder) {
if (existsSync('wrangler.toml')) {
/** @type {WranglerConfig} */
let wrangler_config;

try {
wrangler_config = toml.parse(readFileSync('wrangler.toml', 'utf-8'));
wrangler_config = /** @type {WranglerConfig} */ (
toml.parse(readFileSync('wrangler.toml', 'utf-8'))
);
} catch (err) {
err.message = `Error parsing wrangler.toml: ${err.message}`;
throw err;
}

// @ts-ignore
if (!wrangler_config.site || !wrangler_config.site.bucket) {
if (!wrangler_config.site?.bucket) {
throw new Error(
'You must specify site.bucket in wrangler.toml. Consult https://developers.cloudflare.com/workers/platform/sites/configuration'
);
}

// @ts-ignore
if (!wrangler_config.build || !wrangler_config.build.upload) {
if (!wrangler_config.main) {
throw new Error(
'You must specify build.upload options in wrangler.toml. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-cloudflare-workers'
);
}

// @ts-ignore
if (wrangler_config.build.upload.format !== 'modules') {
throw new Error('build.upload.format in wrangler.toml must be "modules"');
}

// @ts-ignore
const main_file = wrangler_config.build?.upload?.main;
const main_file_ext = main_file?.split('.').slice(-1)[0];
if (main_file_ext && main_file_ext !== 'mjs') {
// @ts-ignore
const upload_rules = wrangler_config.build?.upload?.rules;
// @ts-ignore
const matching_rule = upload_rules?.find(({ globs }) =>
// @ts-ignore
globs.find((glob) => glob.endsWith(`*.${main_file_ext}`))
'You must specify main option in wrangler.toml. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-cloudflare-workers'
);
if (!matching_rule) {
throw new Error(
'To support a build.upload.main value not ending in .mjs, an upload rule must be added to build.upload.rules. Consult https://developers.cloudflare.com/workers/cli-wrangler/configuration/#build'
);
}
}

return wrangler_config;
Expand All @@ -139,22 +115,16 @@ function validate_config(builder) {
Sample wrangler.toml:

name = "<your-site-name>"
type = "javascript"
account_id = "<your-account-id>"
workers_dev = true
route = ""
zone_id = ""
route = "<your-route>"

[build]
command = ""
main = "./.cloudflare/worker.js"
site.bucket = "./.cloudflare/public"

[build.upload]
format = "modules"
main = "./worker.mjs"
build.command = "npm run build"

[site]
bucket = "./.cloudflare/assets"
entry-point = "./.cloudflare/worker"`
compatibility_date = "2021-11-12"
workers_dev = true`
.replace(/^\t+/gm, '')
.trim()
);
Expand Down
17 changes: 8 additions & 9 deletions packages/create-svelte/templates/default/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
name = "svelte-kit-demo"
type = "webpack"
account_id = "32a8245cd45a24083dd0acae1d482048"
workers_dev = true
route = ""
zone_id = ""
route = "cloudflare-workers-demo.svelte.dev/*"

main = "./.cloudflare/worker.js"
site.bucket = "./.cloudflare/public"

[site]
bucket = "./.cloudflare/assets"
entry-point = "./.cloudflare/worker"
build.command = "npm run build"

compatibility_date = "2021-11-12"
workers_dev = true

[build]
upload = { format = "service-worker" }