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 28 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 Node.js compatibility to adapter-cloudflare and adapter-cloudflare-workers
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ 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 add "nodejs_compat" flag to `wrangler.toml`:

```toml
/// file: wrangler.toml
compatibility_flags = [ "nodejs_compat" ]
```

## 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
6 changes: 5 additions & 1 deletion packages/adapter-cloudflare-workers/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
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;
}
22 changes: 20 additions & 2 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { fileURLToPath } from 'node:url';
* site: {
* bucket: string;
* }
* compatibility_flags?: string[];
* }} WranglerConfig
*/

Expand All @@ -20,7 +21,7 @@ export default function ({ config = 'wrangler.toml' } = {}) {
name: '@sveltejs/adapter-cloudflare-workers',

async adapt(builder) {
const { main, site } = validate_config(builder, config);
const { main, site, compatibility_flags } = 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 @@ -61,6 +62,23 @@ export default function ({ config = 'wrangler.toml' } = {}) {
})};\n\nexport const prerendered = new Map(${JSON.stringify(prerendered_entries)});\n`
);

const external = ['__STATIC_CONTENT_MANIFEST', 'cloudflare:*'];
if (compatibility_flags && compatibility_flags.includes('nodejs_compat')) {
external.push(
'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'
);
}

await esbuild.build({
platform: 'browser',
conditions: ['worker', 'browser'],
Expand All @@ -69,7 +87,7 @@ export default function ({ config = 'wrangler.toml' } = {}) {
entryPoints: [`${tmp}/entry.js`],
outfile: main,
bundle: true,
external: ['__STATIC_CONTENT_MANIFEST', 'cloudflare:*'],
external,
format: 'esm',
loader: {
'.wasm': 'copy'
Expand Down
17 changes: 16 additions & 1 deletion packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ export default function (options = {}) {
}
});

const external = [
'cloudflare:*',
'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'
];

await esbuild.build({
platform: 'browser',
conditions: ['worker', 'browser'],
Expand All @@ -66,7 +81,7 @@ export default function (options = {}) {
loader: {
'.wasm': 'copy'
},
external: ['cloudflare:*']
external
});
}
};
Expand Down
Loading