Skip to content

Commit 0777b0f

Browse files
cimninebenmccann
andcommitted
Pass external to esbuild in cloudflare adapters
Co-authored-by: Ben McCann <[email protected]>
1 parent 0595e92 commit 0777b0f

File tree

6 files changed

+51
-11
lines changed

6 files changed

+51
-11
lines changed

documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ export default {
2828
routes: {
2929
include: ['/*'],
3030
exclude: ['<all>']
31-
}
31+
},
32+
external: [ 'fs' ]
3233
})
3334
}
3435
};
3536
```
3637

3738
## Options
3839

40+
The adapter accepts two options: `routes` and `external`.
41+
42+
### `routes`
43+
3944
The `routes` option allows you to customise the [`_routes.json`](https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file) file generated by `adapter-cloudflare`.
4045

4146
- `include` defines routes that will invoke a function, and defaults to `['/*']`
@@ -47,6 +52,10 @@ The `routes` option allows you to customise the [`_routes.json`](https://develop
4752

4853
You can have up to 100 `include` and `exclude` rules combined. Generally you can omit the `routes` options, but if (for example) your `<prerendered>` paths exceed that limit, you may find it helpful to manually create an `exclude` list that includes `'/articles/*'` instead of the auto-generated `['/articles/foo', '/articles/bar', '/articles/baz', ...]`.
4954

55+
### `external`
56+
57+
`external` is equivalent to the respective [_external_ option of esbuild](https://esbuild.github.io/api/#external). You can use it to mark a file or package as external to exclude it from your build. Typically, this can be used for packages that do import _NodeJS_ modules such as `fs`.
58+
5059
## Deployment
5160

5261
Please follow the [Get Started Guide](https://developers.cloudflare.com/pages/get-started) for Cloudflare Pages to begin.

documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ Then, you can build your app and deploy it:
6161
wrangler publish
6262
```
6363

64-
## Custom config
64+
## Adapter configuration
6565

66-
If you would like to use a config file other than `wrangler.toml`, you can do like so:
66+
The adapter configuration accepts two options: `config` and `external`.
6767

6868
```js
6969
// @errors: 2307
@@ -72,11 +72,24 @@ import adapter from '@sveltejs/adapter-cloudflare-workers';
7272

7373
export default {
7474
kit: {
75-
adapter: adapter({ config: '<your-wrangler-name>.toml' })
75+
adapter: adapter({
76+
config: '<your-wrangler-name>.toml',
77+
external: [ 'fs' ]
78+
})
7679
}
7780
};
7881
```
7982

83+
### Custom config
84+
85+
If you would like to use a config file other than the default `wrangler.toml`, set `config` to the name of the file. The path is relative to the root of your project.
86+
87+
### `external`
88+
89+
`external` is equivalent to the respective [_external_ option of esbuild](https://esbuild.github.io/api/#external). You can use it to mark a file or package as external to exclude it from your build. Typically, this can be used for packages that do import _NodeJS_ modules such as `fs`.
90+
91+
The values `__STATIC_CONTENT_MANIFEST` and `cloudflare:*` are always in the list of excluded packages and this can't be changed.
92+
8093
## Bindings
8194

8295
The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object contains your project's [bindings](https://developers.cloudflare.com/workers/platform/environment-variables/), which consist of KV/DO namespaces, etc. It is passed to SvelteKit via the `platform` property, along with `context` and `caches`, meaning that you can access it in hooks and endpoints:
@@ -120,4 +133,4 @@ When deploying to workers, the server generated by SvelteKit is bundled into a s
120133

121134
### Accessing the file system
122135

123-
You can't access the file system through methods like `fs.readFileSync` in Serverless/Edge environments. If you need to access files that way, do that during building the app through [prerendering](https://kit.svelte.dev/docs/page-options#prerender). If you have a blog for example and don't want to manage your content through a CMS, then you need to prerender the content (or prerender the endpoint from which you get it) and redeploy your blog everytime you add new content.
136+
You can't access the file system through methods like `fs.readFileSync` in Serverless/Edge environments. If you need to access files that way, do that during building the app through [prerendering](https://kit.svelte.dev/docs/page-options#prerender). If you have a blog for example and don't want to manage your content through a CMS, then you need to prerender the content (or prerender the endpoint from which you get it) and redeploy your blog everytime you add new content.
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
import { Adapter } from '@sveltejs/kit';
22
import './ambient.js';
33

4-
export default function plugin(options: { config?: string }): Adapter;
4+
export default function plugin(options?: AdapterOptions): Adapter;
5+
6+
export interface AdapterOptions {
7+
/**
8+
* List of packages that should not be bundled.
9+
*/
10+
external?: string[];
11+
12+
/**
13+
* The name of the wrangler config file to use.
14+
* Defaults to `wrangler.toml`.
15+
*/
16+
config?: string;
17+
}

packages/adapter-cloudflare-workers/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import { fileURLToPath } from 'node:url';
1515
*/
1616

1717
/** @type {import('.').default} */
18-
export default function ({ config = 'wrangler.toml' } = {}) {
18+
export default function (options = {}) {
1919
return {
2020
name: '@sveltejs/adapter-cloudflare-workers',
2121

2222
async adapt(builder) {
23-
const { main, site } = validate_config(builder, config);
23+
const { main, site } = validate_config(builder, options.config ?? 'wrangler.toml');
2424

2525
const files = fileURLToPath(new URL('./files', import.meta.url).href);
2626
const tmp = builder.getBuildDirectory('cloudflare-workers-tmp');
@@ -62,11 +62,11 @@ export default function ({ config = 'wrangler.toml' } = {}) {
6262
entryPoints: [`${tmp}/entry.js`],
6363
outfile: main,
6464
bundle: true,
65-
external: ['__STATIC_CONTENT_MANIFEST', 'cloudflare:*'],
6665
format: 'esm',
6766
loader: {
6867
'.wasm': 'copy'
69-
}
68+
},
69+
external: ['__STATIC_CONTENT_MANIFEST', 'cloudflare:*', ...(options.external ?? [])]
7070
});
7171

7272
builder.log.minor('Copying assets...');

packages/adapter-cloudflare/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import './ambient.js';
44
export default function plugin(options?: AdapterOptions): Adapter;
55

66
export interface AdapterOptions {
7+
/**
8+
* List of packages that should not be bundled.
9+
*/
10+
external?: string[];
11+
712
/**
813
* Customize the automatically-generated `_routes.json` file
914
* https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file

packages/adapter-cloudflare/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default function (options = {}) {
5858
loader: {
5959
'.wasm': 'copy'
6060
},
61-
external: ['cloudflare:*']
61+
external: ['cloudflare:*', ...(options.external ?? [])]
6262
});
6363
}
6464
};

0 commit comments

Comments
 (0)