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

Mark node: as external for Node.js compatibility #10544

Merged
merged 29 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8daecc5
make `cloudflare:email` as external
chientrm Jul 18, 2023
dc77c14
make cloudflare:email as external
chientrm Jul 18, 2023
2da8e64
Merge branch 'master' of https://github.com/sveltejs/kit
chientrm Jul 18, 2023
4a3404e
add changeset
chientrm Jul 18, 2023
01f3912
format
chientrm Jul 18, 2023
48e3f6b
fix changeset
chientrm Jul 18, 2023
823b599
add for adapter-cloudflare-worker, too
chientrm Jul 18, 2023
11e3504
add changeset for adapter-cloudflare-worker
chientrm Jul 18, 2023
cb679a9
Update .changeset/eighty-fans-rescue.md
chientrm Jul 21, 2023
dd59513
Update .changeset/friendly-dragons-hide.md
chientrm Jul 21, 2023
5f91d0e
Update .changeset/eighty-fans-rescue.md
benmccann Jul 24, 2023
ad515c3
Update .changeset/friendly-dragons-hide.md
benmccann Jul 24, 2023
6b3c109
Update packages/adapter-cloudflare/index.js
chientrm Jul 27, 2023
0ca2569
Update index.js
chientrm Jul 27, 2023
5e4bdd9
Update .changeset/friendly-dragons-hide.md
benmccann Jul 27, 2023
3ba4b44
Update .changeset/eighty-fans-rescue.md
benmccann Jul 27, 2023
9e77535
Merge branch 'sveltejs:master' into master
chientrm Aug 12, 2023
2d50b6a
mark `node:` as external
chientrm Aug 12, 2023
c572a21
remove old changesets
chientrm Aug 12, 2023
73faf43
options.nodeCompat
chientrm Aug 12, 2023
925d8ec
add docs
chientrm Aug 13, 2023
390ba88
Update documentation/docs/25-build-and-deploy/70-adapter-cloudflare-w…
benmccann Jan 18, 2024
7df233d
Merge branch 'main' into cloudflare-node-compat
benmccann Jan 18, 2024
b3a771f
switch it to just the list of APIs that Cloudflare supports with thei…
chientrm Jan 18, 2024
df03b36
read `nodejs_compat` flag from the wrangler.toml to avoid making the …
chientrm Jan 18, 2024
7692979
changeset
chientrm Jan 18, 2024
ea0ecb9
remove `nodeCompat` option, always assume compatibility
Rich-Harris Jan 18, 2024
65b23fa
Apply suggestions from code review
Rich-Harris Jan 18, 2024
62dd0f5
Update .changeset/pretty-geese-drum.md
Rich-Harris Jan 18, 2024
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/pretty-geese-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-cloudflare': patch
---

fix: add `options.nodeCompat` to allow Node.js compatibility
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default {
routes: {
include: ['/*'],
exclude: ['<all>']
}
},
nodeCompat: undefined
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
})
}
};
Expand All @@ -45,6 +46,8 @@ The `routes` option allows you to customise the [`_routes.json`](https://develop
- `<prerendered>` contains a list of prerendered pages
- `<all>` (the default) contains all of the above

The `nodeCompat` option allows you to enable [Node.js compatibility](https://developers.cloudflare.com/workers/runtime-apis/nodejs/), and defaults to `undefined` mean `disabled`. If `nodeCompat` is `true`, you will also need to [Enable Node.js from the Cloudflare dashboard](https://developers.cloudflare.com/workers/runtime-apis/nodejs/#enable-nodejs-from-the-cloudflare-dashboard).

Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
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', ...]`.

## Deployment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ export default {
};
```

If you would like to enable [Node.js compatibility](https://developers.cloudflare.com/workers/runtime-apis/nodejs/#enable-nodejs-from-the-cloudflare-dashboard), you can do like so:

```js
// @errors: 2307
/// file: svelte.config.js
import adapter from '@sveltejs/adapter-cloudflare-workers';

export default {
kit: {
adapter: adapter({ nodeCompat: true })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why pass it in if it's already specified in the wrangler.toml? https://developers.cloudflare.com/workers/runtime-apis/nodejs/#enable-nodejs-with-workers

I think it'd be nicer to read it from the wrangler.toml to avoid making the user specify it twice

Copy link
Contributor Author

@chientrm chientrm Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When deploy to Cloudflare Pages, the file wrangler.toml is not available.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but this is the Cloudflare Workers adapter 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, sounds legit 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oke, I'm going to make it read the wrangler.toml, not sure how could I do that. Maybe just string.includes('node_compat')? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, there is a toml.parse already.

}
};
```

You will also need to [Enable Node.js with Workers](https://developers.cloudflare.com/workers/runtime-apis/nodejs/#enable-nodejs-with-workers).
benmccann marked this conversation as resolved.
Show resolved Hide resolved

## Bindings

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:
Expand Down
12 changes: 11 additions & 1 deletion packages/adapter-cloudflare-workers/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { Adapter } from '@sveltejs/kit';
import './ambient.js';

export default function plugin(options: { config?: string }): Adapter;
export default function plugin(options?: AdapterOptions): Adapter;

export interface AdapterOptions {
config?: string;
/**
* Enable Node.js compatibility
* https://developers.cloudflare.com/workers/runtime-apis/nodejs/
* @default undefined
*/
nodeCompat?: boolean;
}
10 changes: 7 additions & 3 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { fileURLToPath } from 'node:url';
*/

/** @type {import('.').default} */
export default function ({ config = 'wrangler.toml' } = {}) {
export default function (options = { config: 'wrangler.toml' }) {
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
return {
name: '@sveltejs/adapter-cloudflare-workers',

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

const files = fileURLToPath(new URL('./files', import.meta.url).href);
const tmp = builder.getBuildDirectory('cloudflare-workers-tmp');
Expand Down Expand Up @@ -62,7 +62,11 @@ export default function ({ config = 'wrangler.toml' } = {}) {
entryPoints: [`${tmp}/entry.js`],
outfile: main,
bundle: true,
external: ['__STATIC_CONTENT_MANIFEST', 'cloudflare:*'],
external: [
'__STATIC_CONTENT_MANIFEST',
'cloudflare:*',
...(options.nodeCompat ? ['node:*'] : [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitelisting node:* may be too aggressive. I think if we do that and someone uses an API like node:fs then they won't find out their app is broken until they go to deploy it and it'd be better for that to occur during build. How about we switch it to just the list of APIs that Cloudflare supports with their node compatibility?

'node:assert', 'node:async_hooks', 'node:buffer', 'node:crypto', node:diagnostics_channel', node:events', node:path', node:process', node:stream', node:string_decoder', node:util'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep that sounds legit. Cloudflare node_compat doesn't have modules like fs, ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is fixed in my latest commit

Copy link
Contributor Author

@chientrm chientrm Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May as well I remove AdapterOptions.nodeCompat and always include those externals? 🤔

],
format: 'esm',
loader: {
'.wasm': 'copy'
Expand Down
6 changes: 6 additions & 0 deletions packages/adapter-cloudflare/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export interface AdapterOptions {
*/
exclude?: string[];
};
/**
* Enable Node.js compatibility
* https://developers.cloudflare.com/workers/runtime-apis/nodejs/
* @default undefined
*/
nodeCompat?: boolean;
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
}

export interface RoutesJSONSpec {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function (options = {}) {
loader: {
'.wasm': 'copy'
},
external: ['cloudflare:*']
external: ['cloudflare:*', ...(options.nodeCompat ? ['node:*'] : [])]
});
}
};
Expand Down