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

adapter-cloudflare-workers: Configure custom wrangler.toml file name #7104

Merged
merged 5 commits into from Oct 2, 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/eleven-seahorses-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare-workers': minor
---

Add config option to set custom `wrangler.toml` file name
14 changes: 14 additions & 0 deletions packages/adapter-cloudflare-workers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ Then, you can build your app and deploy it:
wrangler publish
```

## Custom config

If you would like to use a config file other than `wrangler.toml`, you can do like so:

```js
import adapter from '@sveltejs/adapter-cloudflare-workers';

export default {
kit: {
adapter: adapter({ config: '<your-wrangler-name>.toml' })
}
};
```

## Environment variables

The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object, containing KV/DO namespaces etc, is passed to SvelteKit via the `platform` property along with `context` and `caches`, meaning you can access it in hooks and endpoints:
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare-workers/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Adapter } from '@sveltejs/kit';

export default function plugin(): Adapter;
export default function plugin(options: { config?: string }): Adapter;
19 changes: 10 additions & 9 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { fileURLToPath } from 'url';
*/

/** @type {import('.').default} */
export default function () {
export default function ({ config = 'wrangler.toml' } = {}) {
// TODO remove for 1.0
if (arguments.length > 0) {
throw new Error(
Expand All @@ -27,7 +27,7 @@ export default function () {
name: '@sveltejs/adapter-cloudflare-workers',

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

const files = fileURLToPath(new URL('./files', import.meta.url).href);
const tmp = builder.getBuildDirectory('cloudflare-workers-tmp');
Expand Down Expand Up @@ -81,31 +81,32 @@ export default function () {

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

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

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'
`You must specify site.bucket in ${config_file}. Consult https://developers.cloudflare.com/workers/platform/sites/configuration`
);
}

if (!wrangler_config.main) {
throw new Error(
'You must specify main option in wrangler.toml. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-cloudflare-workers'
`You must specify main option in ${config_file}. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-cloudflare-workers`
);
}

Expand Down Expand Up @@ -134,5 +135,5 @@ function validate_config(builder) {
.trim()
);

throw new Error('Missing a wrangler.toml file');
throw new Error(`Missing a ${config_file} file`);
}